Inheritance:

  • The concept of getting properties of one class object to another class object.
  • Inheritance represents the IS-A relationship,also known as parent-child relationship.

Types of Inheritance:

  1. Multiple inheritance
  2. Multilevel inheritance.

How Inheritance can be implemented in java:

  • Inheritance can be implemented in JAVA using below two keywords:
  1. extends
  2. implements
  • extends is used for developing inheritance between two classes and two interfaces.
  • implements keyword is used to developed inheritance between interface and class.

Why Inheritance:

  • For Code Reusability.
  • For Method Overiding.

Syntax:

public class subclass extends superclass{

//all methods and varaibles declare here
}

Multilevel Inheritance:

Getting the properties from one class object to another class object level wise with different priorities.

Program:

public class A{

public void show(){
System.out.println("This is Super calss method");
}
}
public class B extends A{
public void display(){
System.out.println("This is sub class method");
}
}
public class C extends B{
public void add(){
System.out.println("This is another sub class method");
}
public static void main(String args[]){
C  c=new C();
c.show(); //super class method
c.display(); //sub class method
c.add(); //c Subclass method
}

}

Output:
This is Super calss method
This is sub class method
This is another sub class method

Diagram for multilevel inheritance:



Multilevel Inheritance



Multiple inheritance:

  • The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
  • Java Doesn't Support multiple Inheritance.
  •  In multiple inheritance there is every chance of multiple properties of multiple objects with  the same name available to the sub class object with same priorities leads for the ambiguity.
    also known as diamond problem. one class extending two super classes.
  • Because of multiple inheritance there is chance of the root object getting created more than once.
  • Always the root object i.e object of object class hast to be created only once.
  • Because of above mentioned reasons multiple inheritance would not be supported by java.
  • Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.

Program :

public class A{
public void show(){
System.out.println("This is super class method");
}
}
public class B {
public void show(){
System.out.println("This is sub class method");
}
}
public class C extends A,B{
public static void main(String args[]){
C c=new C();
c.show(); //which method need to invoke.
}
}

Output:
Compile time Error.

Java doesn't support multiple inheritance why because java compiler don't know which method has to invoke.Class C is derived from A and B. If A and B classes have same method and you call it from child class object ,there will be ambiguity to call method of A or B classes.that's the reason only java doesn't support multiple Inheritance.
Since compile time errors are better than run time errors, java gives compile time error if you extend 2 classes. So whether you have same method or different, there will be compile time error now.
Multiple Inheritance

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu