Skip to main content

Inheritance(IS-A relationship)

                                                                           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-:
  1. Whatever methods parent has by default available to the child and hence and child reference, we can call both parent and child class methods.
  2. Whatever method child has by default not available to the parent and hence and parent reference we can't call child specific methods.
  3. 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.
  4. Parent reference can be used to hold child object, but child reference can't be used to hold parent object.



                   Without  inheritance

            With inheritance

class HomeLone{

    200 method

}

class carLone{

  200 method

}

class PersonalLone{

   200 method

}

class Lone{

  150 method common method

}

class HomeLone extends Lone{

    25 method

}

class carLone extends Lone{

  20 method

}

class PersonalLone extends Lone{

   5 method

}


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{
       
}
                                        Not Valid 
 
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.

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...