How to use Super Keyword in Java

Ā 

What is Super Keyword in Java? Explain How to use Super Keyword in Java – Java Tutorial

Solution:

Super is a keyword used to access the immediate parent class from a subclass.

Video Tutorial – How to use Super Keyword in Java

There are three ways by which the keyword super is used.

1. The super() is used to invoke the class variable of the immediate parent class.

2. The super() is used to access the class method of the immediate parent class.

3. The super() is used to invoke the immediate parent class constructor.

How to use Super Keyword in Java

1. The super() is used to invoke the class variable of the immediate parent class.

class A 
{ 
	int x=10; 
} 
class B extends A 
{ 
	int x=20; 
	void display() 
	{ 
		System.out.println(super.x); 
	} 
}
class superdemo
{
	public static void main(String args[]) 
	{ 
		B obj=new (); 
		obj.display();
	}
}

Output:

10

Program Explanation:

In the above program, class A is an immediate parent class of class B.

Both class A and Class B have variables x.

In class A, the value of the x variable is 10 and in class B the value of variable x is 20.

In display function if we would write System.out.println(x);

The output will be 20 but if we use super.x then the variable x of class A will be referred.

Hence the output is 10.

2. The super() is used to access the class method of the immediate parent class.

class A 
{ 
	void fun() 
	{ 
		System.out.println("Method: Class A"); 
	} 
}
class B extends A 
{ 
	void fun() 
	{
		System.out.println("Method: Class B"); 
	} 
	
	void display() 
	{ 
		super.fun(); 
	}
}

class superdemo2	
{
	public static void main(String args[]) 
	{ 
		B obj =new B(); 
		obj.display(); 
	}
}

Output:

Method: Class A

Program Explanation:

In the above program, the derived class can access the immediate parent’s class method using super.fun().

Hence the output is Method: Class A

You can change super.fun() to fun().

Then note that in this case, the output will be the invocation of subclass method fun.

3. The super() is used to invoke the immediate parent class constructor.

class A 
{ 
	A() 
	{
	System.out.println("The constructor of Class A"); 
	}
} 
class B extends A 
{ 
	B() 
	{ 
	super(); 
	System.out.println("The constructor of Class B"); 
	} 
}
class superdemo3
{ 
	public static void main(String args[])
	{
		B obj=new B();
	}
} 

Output:

The constructor of Class A

The constructor of Class B

Program Explanation:

In the above program, the constructor in class B makes a call to the constructor of the immediate parent class by using the keyword super, hence the print statement in the parent class constructor is executed, and then the print statement for the class B constructor is executed.

Summary:

In this article, we understood, What is Super Keyword in Java? Explain How to use Super Keyword in Java – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *