12 How to load ".class" file by using Inheritance

In this time .class file is loading but static blocks are not executing but the byte code is loading from Secondary memory to Primary memory.

Inheritance: means extracting functionalities from one class to another class.

Every class requires 11 methods. But every time writing this increases
Boiler Plate of code i.e Duplicate code.
Program size will increases
Compile time will be increases
Performance will decreases. 


For using this code again we go for the concept like Inheritance.

Whenever we load subclass automatically Super class will be loaded in following manner:

1) Super class static loading phase
2) Super class static initialization phase
3) Sub class static loading phase
4) Sub class static initialization phase
5) Sub class main method


Now Suppose we have two file Super class A.java and Sub class B.java

A.java

class A{
static int a=m1();
static{
System.out.println("Super class static block of A");
}
static int m1(){
System.out.println("Super static m1 method");
return 111;
}
public static void main(String...ram){
System.out.println("A class main method");
}
}

B.java

class B extends A{
static int b=m2();

static{
System.out.println("Sub class static block of B");
}

static int m2(){
System.out.println("Sub static m2 method");
return 111;
}

public static void main(String...ram){
System.out.println("B class main method");
}
}



As you can whatever thing I have written theoretical same thing happen programmatically.
Previous
Next Post »