Inheritance
- It is also known as IS-A relationship.
- The main advantage of IS-A relationship is code reusability.
- By using extends keyword, we can implement IS-A relationship.
class Parent{
public static void m1(){
}
}
class child{
public static void m2(){
}
}
public class Test{
public static void main(String... args){
Parent p=new Parent();
p.m1();
p.m2(); // compile time error-:can't find symbol m2() location class Parent
Child c=new Child();
c.m2();
c.m1();
Parent p1=new Child();
p1.m1();
p1.m2();// compile time error-:can't find symbol m2() location class Parent
Child cc=new Child();
cc.m1();
cc.m2();
Child cp=new Parent();// compile time error-:incompatible type found P required C
}
Conclusion-:
- Whatever methods parent has by default available to the child and hence and child reference, we can call both parent and child class methods.
- Whatever method child has by default not available to the parent and hence and parent reference we can't call child specific methods.
- Parent reference can be used to hold child object but by using that reference we can't call child specific methods, but we can call the method present in parent class.
- Parent reference can be used to hold child object, but child reference can't be used to hold parent object.
Note -: The most common method which are applicable for any type of child, we have to define in parent class.
- The specific method which are applicable for a particular child, we have to define in child class.
- Total Java API is implemented based on inheritance concept.
- The most common methods which are applicable for any Java object are defined in object class. Every class in Java is a child class of object either directly or indirectly so that object class method by default available to every Java class without read writing. This is due to this object class access root for all Java classes.
- Throwable class define the most common method which are required for every exception and error classes, hence this class access root for Java exception hirerchy.
Multiple Inheritance-:
A Java class can't extend more than one class at a time, hence Java won't provide support for multiple inheritance in classes.
E.g.-:
class A extends B,C{
//compile time error
}
Note-:
If our class doesn't extend any other class, then only our class is a direct child class of Object.
E.g.-:
class A
{
}
class A is child of Object
If our class extends any other class, then our class is an indirect child class of Object.
class A extends B{
}
class A is child of B and B is child of Object
Multi level inheritance-:
A---------------->B-------------->Object (Multi level class)
either directly or indirectly, Java won't provide support for inheritance with respect to classes.
Why Java won't provide support for multiple inheritance?
There may be chance of ambiguity problem, hence Java won't provide support for multiple inheritance.
But interface can extend any number of interface simultaneously, hence Java provide support for multiple inheritance with respect to interfaces.
E.g-:
interfaces A{
}
interface B{
}
interface C extends A,B{
}
Why ambiguity problem won't be there in interfaces?
Even though multiple method declaration are available, but implementation is unique and hence there is no chance of ambiguity problem in interfaces.
Note-: Strictly speaking, through interfaces, we won't get any inherit.
Cyclic inheritance-: cyclic inheritance is not allowed in Java offcourse it's not required.
E.g.-:
class A extends A{
}
class A extends B{
}
class B extends A{
}
In cases, compile time error cyclic inheritance involving in A.