Inheritance Concept

Importants Points  About Inheritance In Java

  1. Constructors, Static Initialization Block and Instance Initialization Block of super class will not be inheriting to its sub class. But they are executed while creating an object to sub class. For example,
 

 

class A{
    int i;

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

    {
        System.out.println("Class A instance block");
    }

    A() {
        System.out.println("Class A Constructor");
    }
}

class B extends A{

    int j;

}

class MainClass{
    public static void main(String[] args){
        B b = new B();
    }
}

 

In the above example, Class B is extending Class A. In the MainClass, We are creating an object to Class B. While creating this object, static, instance block and constructor of Class A are also executed. The output of the above program will be,

 

Output :
Class A static
Class A instance block
Class A Constructor

Related Articles

post a comment