Core Java Articles


Total available count: 12
Subject - Java Technologies
Subsubject - Core Java

Usage of Instance block, Static block and Super class variables in Inheritance

1. Super class variables:

Usage of Parent class variables or Super class variables (project level if all variables contain same name) in child class is explained in below example.

Example:

class Parent
{
	int a=10;
	int b=20;
}
class Child extends Parent
{
	int a=100;
	int b=200;
	void m1(int a,int b)
	{
		System.out.println(super.a+super.b);   //super class variables of Parent class
		System.out.println(this.a+this.b);      //Instance variables of this class
		System.out.println(a+b);             //Local class variables
	}
	public static void main(String[] args)
	{
        new Child().m1(1000,2000);
	}
}

 

Output:
30
300
3000

In this example, we use ‘super ‘and ‘this’ keywords for accessing parent class variables and instance variables of child class. The keyword ‘super’ is referred to parent class and ‘this’ keyword referred to child class instance variables. To access variables of local variables in child class, we don’t need any special keywords and directly use variable names. All these can be explained in above example. 

2. Parent Instance Block usage in child class:

We all know about instance block and it is explained in previous articles and you can go through it. Now see the usage of instance block of parent class in child class. It will be explained in below program.

Example:

class Parent
{      
	{
		System.out.println("This is Parent Instance Block");
	}	
}
class Child extends Parent
{
	{	
 		System.out.println("This is Child Instance Block");     
    }
	
	public static void main(String[] args)
	{
        new Child();   //if remove this line then instance block will not execute

	}
}

 

Output:
This is Parent Instance Block
This is Child Instance Block

3. Parent static Block usage in child class:

We all know about static block and it is explained in previous articles and you can go through it. Now see the usage of static block and instance block of parent class in child class. It will be explained in below program.

Example:

class Parent
{      
	{
		System.out.println("This is Parent Class Instance Block");
	}	
	Parent()
		{
		System.out.println("This is Parent Class 0-args Constructor Block");
		}
   static	
	   {
		System.out.println("This is Parent Class static Block");
		}
}
class Child extends Parent
{
	{	
 		System.out.println("This is Child Instance Block");     
    }
	Child()
		{
		     //super(); is appended by compiler 
		System.out.println("This is child Class 0-args Constructor Block");
		}
		static	
	   {
		System.out.println("This is Child Class static Block");
		}
	
	public static void main(String[] args)
	{
        new Child();  
	}
}

 

Output:
This is Parent Class static Block
This is Child Class static Block
This is Parent Class Instance Block
This is Parent Class 0-args Constructor Block
This is Child Instance Block
This is child Class 0-args Constructor Block

I hope you understand the usage of static block and instance block of parent class in child class. Let us see another example to understand little more about this.
Example:

class Parent
{      
	{
		System.out.println("This is Parent Class Instance Block");
	}	
	Parent()
		{
		System.out.println("This is Parent Class 0-args Constructor Block");
		}
   static	
	   {
		System.out.println("This is Parent Class static Block");
		}
}
class Child extends Parent
{
	{	
 		System.out.println("This is Child Instance Block");     
    }
	Child()
		{
		//super(); is appended by compiler 
		System.out.println("This is child Class 0-args Constructor Block");
		}
		static	
	   {
		System.out.println("This is Child Class static Block");
		}
	
	public static void main(String[] args)
	{
        new Child();
		new Child();
	}
}

 

Output:
This is Parent Class static Block
This is Child Class static Block
This is Parent Class Instance Block
This is Parent Class 0-args Constructor Block
This is Child Instance Block
This is child Class 0-args Constructor Block
This is Parent Class Instance Block
This is Parent Class 0-args Constructor Block
This is Child Instance Block
This is child Class 0-args Constructor Block




Next 1 article(s)

1
Usage of 'this' and 'super' keywords

Comments