Java Super Keyword

Java Super Keyword:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

The super keyword is used to access the members of a superclass in a subclass. Unlike the this keyword, the super keyword cannot be used to refer to the current object of a class but is instead used to invoke the overridden method and hidden variables in a subclass. In other words, the super keyword is used whenever you need to refer to a superclass from a subclass.

The super keyword is used for two purposes-

  • If the superclass instance variable name is the same as that of the child class or subclass instance variable name, then the super keyword is used only inside the subclass to access the superclass instance variable.
  • Java uses the super keyword for explicitly calling the superclass constructor within the child class constructor.
super keyword java
class A
{
	A()
	{
		System.out.println("Superclass");
	}
	
}

class B extends A
{
	B()
	{
		super(); // calls superclass default constructor
		System.out.println("Subclass");
	}
	
}


public class Super_keyword {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		B b = new B();

	}

}

Basic Components of Computer System
Computer Input Devices
Computer Output Devices
Computer Memory
Secondary Storage Devices
Characteristics of Computer
Applications of Computer
Java (programming language)– Wikipedia

Comments (No)

Leave a Reply