Pages

Sunday, February 5, 2017

Inheritance


  • Inheritance is the process of writing new class by inheriting commonly used state and behavior of existing class.
  • Existing class is called as Super class or Base class or Parent class.
  • Newly defined class is called as Sub class or Derived class or Child class.
  • The main purpose of Inheritance is code re usability i.e Inheritance can be used to re-use the properties and operations of the existing class in the newly defined class.

Types of Inheritance:

1. Simple Inheritance: 
 In this type, there is one super class and one sub class.
 Ex: 
       class A{...}
       class B extends A{...}

2. Multi Level Inheritance.
In this type:
  • One super class can have only one direct sub class and many indirect sub classes.
  • One sub class can have only one direct super class and many indirect super classes.
  Ex:
       class A{...}
       class B extends A{...}
       class C extends B{...}

3. Hierarchical Inheritance:
 In this type:
  • One super class can have many direct sub classes.
  • One sub class can have only one direct super class.
  Ex:
      class A{...}
      class B extends A{...}
      class C extends A{...}

4. Multiple Inheritance:
In this type:

  • One sub class can have many direct super classes.
  • One super class can have only one direct sub class.
Java doesn't support Multiple Inheritance with classes.
Java supports Multiple Inheritance only with interfaces.

Example 1

class Lab3 
{
    public static void main(String[] args) 
    {
        Hello hello = new Hello();
        System.out.println(hello.a);
        System.out.println(hello.b);

        Hai hai = new Hai();
        System.out.println(hai.a);
    }
}

class Hai
{
    int a = 10;
}

class Hello extends Hai
{
    int b = 20;
}

/*
10
20
10
*/

Example 2


class Lab5 
{
    public static void main(String[] args) 
    {
        Hai hai = new Hai();
        hai.m1();
        hai.m2();
    }
}

class Hello
{
    int a = 10;
    void m1(){
        System.out.println("Hello : m1 -> :"+a);
    }
}


class Hai extends Hello
{
    int b = 20;
    void m2(){
        System.out.println("Hai : m2 -> :"+b);
    }
}

/*
output:
Hello : m1 -> :10
Hai : m2 -> :20
*/

Example 3 : Multi Level Inheritance

class Lab6 
{
    public static void main(String[] args) 
    {
        C obj = new C();
        obj.m1();
        obj.m2();
        obj.m3();
    }
}

class A
{
    void m1(){
        System.out.println("A -> m1()");
    }
}

class B extends A
{
    void m2(){
        System.out.println("B -> m2()");
    }

}

class C extends B
{
    void m3(){
        System.out.println("C -> m3()");
    }

}

/*
output:
A -> m1()
B -> m2()
C -> m3()
*/

Example 4 : Hierarchical Inheritance

class Lab7{
    public static void main(String args[]){

        B bObj = new B();
        bObj.m1();
        bObj.m2();

        C cObj = new C();
        cObj.m1();
        cObj.m3();
    }
}

class A
{
    void m1(){
        System.out.println("A -> m1()");
    }
}

class B extends A
{
    void m2(){
        System.out.println("B -> m2()");
    }

}

class C extends A
{
    void m3(){
        System.out.println("C -> m3()");
    }

}

/*

A -> m1()
B -> m2()
A -> m1()
C -> m3()

*/

Example 5: Multiple Inheritance is not supported in Java with classes

class Lab8
{
    public static void main(String[] args) 
    {
        System.out.println("Hello World!");
    }
}

class A{}
class B{}
class C extends A, B{}

/*
Lab8.java:11: error: '{' expected
class C extends A, B{}
                 ^
1 error
*/

Example 6:Every class extends java.lang.Object class by default.

class Lab10 
{
    public static void main(String[] args) 
    {
        Hello h1 = new Hello();
        System.out.println(h1.toString());


        Hai h2 = new Hai();
        System.out.println(h2.toString());
    }
}
class Hello{}
class Hai extends Object{}

/*
F:\JLC Programs\OOPS\Inheritance\classes>java Lab10
Hello@15db9742
Hai@6d06d69c
*/

Example 7: Cyclical Inheritance is not allowed

class Lab12 
{
    public static void main(String[] args) 
    {
        System.out.println("Hello guys!");
    }
}

class A extends A{}

/*
Lab12.java:9: error: cyclic inheritance involving A
class A extends A
^
1 error
*/

Example 8:

class Lab16 
{
    public static void main(String[] args) 
    {
        Hello hello = new Hello();
        hello.show();
    }
}

class Hai
{
    public int a=10;
    protected int b = 20;
    int c = 30;
}

class Hello extends Hai
{
    void show(){
        System.out.println("public - "+a);
        System.out.println("protected - "+b);
        System.out.println("default - "+c);
    }
}

/*
output:

public - 10
protected - 20
default - 30
*/

Inheritance and Blocks

Example 9 : When sub class has to be loaded, parent class is loaded prior to sub class loading

class Lab18 
{
    public static void main(String[] args) 
    {
        new Hai();
    }
}

class Hello
{
    static int a = 10;
    static{
        System.out.println("Hello S.B a: "+a);
    }
}

class Hai extends Hello
{
    static int b = 20;
    static {
        System.out.println("Hai S.B a:"+a );
        System.out.println("Hai S.B b:"+b);
    }
}

/*
Hello S.B a: 10
Hai S.B a:10
Hai S.B b:20
*/

Example 10: When a super class is called, the sub class is not loaded.

class Lab20 
{
    public static void main(String[] args) 
    {
        System.out.println(Hai.a);
    }
}

class Hai
{
    static int a = 10;
    static{
        System.out.println("Hai SIB a:"+a);
    }
}

class Hello extends Hai
{
    static{
        System.out.println("Hello SIB a:"+a);
    }
}

/*
Hai SIB a:10
10
*/

Example 11: Since the static variable belongs to the parent class, Compiler replaces Hello with Hai. Therefore sub class is not loaded, only the parent class is loaded.

class Lab21
{
    public static void main(String[] args) 
    {
        System.out.println(Hello.a); //Compiler replaces with Hai.a. Therefore, Hello is not loaded
    }
}

class Hai
{
    static int a = 10;
    static{
        System.out.println("Hai SIB a:"+a);
    }
}

class Hello
{
    static{
        System.out.println("Hello SIB a:"+a);
    }
}

/*
Hai SIB a:10
10
*/

Example 12:Order of IIB execution in super class and sub class

class Lab22 
{
    public static void main(String[] args) 
    {
        Hai h = new Hai();
    }
}

class Hello
{
    {
        System.out.println("Hello IIB");
    }
}

class Hai extends Hello
{
    {
        System.out.println("Hai IIB");
    }
}

/*When the above code is executed, this is what compiler does
1. Default no-arg constructor is added by the compiler
2. super() will be the first statement inside the constructor.
3. When Hai object is created, Hai() constructor is called first.
4. super() inside Hai() calls super() of Hello().
5. super() from Hello calls Object class constructor.
6. control comes back to Hello(). Hello IIB is executed.
7. control comes back to Hai(). Hai IIB is executed.


class Hello
{
    Hello(){
        super(); //calls Object class constructor

        //Hello IIB is added below...
        {
            System.out.println("Hello IIB");
        }
    }

}

class Hai
{
    Hai(){
        super(); //calls Hello() constructor

        //Hai IIB is added below...
        {
            System.out.println("Hai IIB");
        }

    }
}

Output:
Hello IIB
Hai IIB
*/

Example 13:

Combination of Static blocks and Instance blocks
class Lab24 
{
    public static void main(String[] args) 
    {
        new Hai();
    }
}

class Hello
{
    {
        System.out.println("Hello IIB");
    }

    static{
        System.out.println("Hello SIB");
    }
}


class Hai extends Hello
{
    {
        System.out.println("Hai IIB");
    }

    static{
        System.out.println("Hai SIB");
    }
}

/*
Hello SIB
Hai SIB
Hello IIB
Hai IIB
*/

Example 14:

class Hai 
{
    public static void main(String[] args) 
    {
        System.out.println("Hai -> main");
    }
    
    static{
        System.out.println("Hai S.B");
    }
}

class Lab25 extends Hai
{
    static{
        System.out.println("Lab25 S.B");
    }
}

/*
Save as Lab25 and run as "java Lab25" : will return the below output.
output:
Hai S.B
Lab25 S.B

If executed as "java Hai"
output:
Hai S.B
Hai -> main
*/

Example 15:

class Lab26 
{
    public static void main(String[] args) 
    {
        Hello.show(); //compiler replaces with Hai.show();
    }
}

class Hai
{
    static void show(){
        System.out.println("Hai -> show()");
    }

    static{
        System.out.println("Hai -> SIB");
    }
}

class Hello extends Hai
{
    static{
        System.out.println("Hello -> SIB");
    }

}
/*
Hai -> SIB
Hai -> show()

*/

Example 16:

When constructors are involved in inheritance.
class Lab28
{
    public static void main(String[] args) 
    {
        new B();
    }
}

class A
{
    A(){
        System.out.println("A()");
    }
}

class B extends A
{
    B(){
        System.out.println("B()");
    }
}

/*
output:

A()
B()
*/

Example 17:

Combination of SIB, IIB, Constructors in Inheritance and their order of execution
class Lab30{
    public static void main(String args[]){
        new C();
    }
}

class A
{
    A()
    {
        System.out.println("A()");
    }

    static{
        System.out.println("A SIB");
    }

    {
        System.out.println("A IIB");
    }
}

class B extends A
{
    B()
    {
        System.out.println("B()");
    }

    static{
        System.out.println("B SIB");
    }

    {
        System.out.println("B IIB");
    }

}

class C extends B
{
    C()
    {
        System.out.println("C()");
    }

    static{
        System.out.println("C SIB");
    }

    {
        System.out.println("C IIB");
    }

}

/*
A SIB
B SIB
C SIB
A IIB
A()
B IIB
B()
C IIB
C()
*/

Summary

  1. Java does not support Multiple Inheritance with classes.
  2. Java supports Multiple Inheritance with interfaces.
  3. Java does not support Hybrid Inheritance which is using multiple inheritance.
  4. Use extends keyword to inherit super class functionalities in sub class.
  5. Only super class members can be accessed by using super class object.
  6. You can access both super and sub class members by using sub class object.
  7. All the super class members can be accessed from sub class directly.
  8. Sub class members cannot be accessed from super class directly.
  9. Object is the default parent class for all Java classes.
  10. When a class is not extending any class then Object class becomes direct super class.
    Example :        class Employee{} //Object is the direct super class.
  11. When a class extends another class, the Object class becomes indirect super class.
    Example:
    class Employee{}
    class Manager extends Manager{} //Object is the indirect super class.
  12. Java doesn't support Cyclic Inheritance.
  13. Cyclic Inheritance happens in two cases:
    1. class A extends A{}
    2. class A extends B{}
      class B extends A{}
  14. final classes cannot be sub classed.
  15. private members of super class will not be inherited to sub class.
  16. When JVM loads the class then:
    1. It checks whether super class is loaded or not.
    2. If super class is not loaded then it loads super class then sub class.
    3. If super class is already loaded, the it loads the sub class directly.
  17. When you access static members of super class using sub class name then subclass will not loaded.
  18. When JVM creates the Object then:
    1. It allocates the memory of instance variables of super class.
    2. It allocates the memory of instance variables of sub class.
    3. It executes the instance blocks and constructors of super class.
    4. It executes the instance blocks and constructors of sub class.
  19. When super class contains main() method then it will be inherited to sub class also i.e you can execute java program either with super class name or subclass name.
  20. Constructors of super class will not be inherited to subclass.
  21. You can use same name fro super class sub class and local variables.
  22. When you access any variable directly then following things will happen:
    1. Checks whether that variable is declared in the local scope or not.
    2. If found int he local scope, that local variable will be used.
    3. If not found in local scope, then checks whether that variable is declared in the class scope or not.
    4. If found, then class level variable will be used.
    5. If not found in the class scope then checks whether that variable is inherited from super classes or not.
    6. If found, that inherited variable will be used.
  23. When you have same name for local variables, class level variables and super class variables then do the following:
    1. Refer the local variable directly.
    2. Refer the class level variable using this keyword.
    3. Refer the super class level variable using super keyword.

No comments:

Post a Comment