×

Html & Html5

The World's best Software Development Study Portal

Inheritance

Inheritance is the main property of OOPs (Object Oriented Programming Language) by which we can follow the properties of the parent class into child or base classes. And we can again reuse the methods of those parent class into our new classes. And even you can add new functions,methods to your new classes , by which runtime polymorphism you can achieved.

Syntax Of Inheritance

The “extends” keyword we use here just to describe in the Java that the new class which we have created is derived from the existing class. So here in the following example our Superclass-name is the parent class and Subclass-name is extending the Superclass-name.

Syntax:

class subclass-nameextendSuperclass-name{

//methods and fields

}



TYPES OF INHERITANCE

There are basically 5 main types of Inheritance which are as follows :-
  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance

Single Inheritance

In this type of Inheritance we generally derived any one class from the parent class , so that new built class also have the properties of the parent class .Let us see by the following program.

Example

class Birds

{

void fly() {System.out.println("Flying...");}

}

class Zoo extends Birds

//So here we have extend the Zoo class into the Bird class

{

void chirp() {System.out.println("Chirping...");}

}

class Saurabh{

public static void main (String args[])

{

Zoo z=new Zoo();// here we have created the object of Zoo class

z.chirp();// here we are calling the method of Zoo class

z.fly();// here we are calling the method of Bird class

}

}

Output:



Multilevel Inheritance

In this type of Inheritance we can generally derived more class from the parent class ,and even from those derived classes we can derived more classes, so that new built classes also have the properties of the parent class .Let us see by the following program.

Example

class Birds

{

void fly() {System.out.println("Flying...");}

}

class Pigeon extends Birds

// so here we are extending the Pigeon class into Bird class

{

void eat() {System.out.println("Eating...");}

}

class Zoo extends Pigeon

//So here we are extending the Zoo class into the Pigeon class

{

void chirp() {System.out.println("Chirping...");}

}

class Saurabh

{

public static void main (String args[])

{

Zoo z=new Zoo();// here we have created the object of Zoo class

z.chirp();// here we are calling the method of Zoo class

z.fly();// here we are calling the method of Birds class

z.eat();// here we are calling the method of Pigeon class

}

}


Output:



Hierarchical Inheritance

In this type of Inheritance we can generally derived more classes from the parent class , so that new built classes also have the properties of the parent class.Let us see by the following program.

Example

class Birds

{

void fly() {System.out.println("Flying...");}

}

class Pigeon extends Birds

// so here we are extending the Pigeon class into Bird class

{

void eat() {System.out.println("Eating...");}

}

class Zoo extends Birds

//So here we are extending the Zoo class into the Pigeon class

{

void chirp() {System.out.println("Chirping...");}

}

class Saurabh

{

public static void main (String args[])

{

Zoo z=new Zoo();// here we have created the object of Zoo class

z.chirp();// here we are calling the method of Zoo class

z.fly();// here we are calling the method of Birds class

/*z.eat();// compile run time error because our Zoo class is been derived from Bird class not from the Pigeon class */

}

};

Output:



Java Does not support Multiple Inheritance

Example

class Birds {

void fly() {System.out.println("Flying...");}

}class Pigeon // So here we have taken new class Pigeon

{

void eat() {System.out.println("Eating...");}

}

class Zoo extends Birds ,Pigeon; // So here we are extending both Birds and Pigeon

class

{

void chirp() {System.out.println("Chirping...");}

}

class Saurabh

{

public static void main (String args[])

{

Zoo z=new Zoo ();// here we have created the object of Zoo class

z.chirp();// we are calling here the method of Zoo class

z.fly();// here we are calling the method of Birds class

}


Output:

Compile time error


Hybrid Inheritance

If we are having (Bird) class which is extending (Pigeon) class , and our (Pigeon) class is extending (Peacock) class , (Peacock) class is extending (Koyal) class and (Koyal) class is extending to (Bird) class , so from the main method of (Koyal) class we can define the method of every other class. Let us understand this by diagrammatically...

Example

class Birds

{

void fly() {System.out.println("Flying...");}

}

class Peacock extends Birds

// so here we are extending the Peacock class into Bird class

{

void dance() {System.out.println("Dance...");}

}

class Pigeon extends Peacock

//So here we are extending the Pigeon class into the Peacock class

{

void eat() {System.out.println("Eating...");}

}

class Koyal extends Pigeon

// So here we are extending the Koyal class into the Peacock class

{

void sing() {System.out.println("Singing...");}

}

class Saurabh

{

public static void main (String args[])

{

Koyal k=new Koyal();// here we have created the object of Zoo class

k.sing();// here we are calling the method of Zoo class

k.eat(); // here we are calling the method of Birds class

k.dance();

k.fly();

}

};

Output: