super keyword in java

Java super keyword with Example

In this tutorial, we will learn about the java super keyword with examples.

What is super keyword?

In Java, the super keyword is a reference variable that is used to refer to an object’s immediate parent class. When you create an instance of a subclass, an instance of the parent class is implicitly created, which is referred to by the super reference variable. super can be used to refer to an immediate parent class instance variable.

Java super keyword Usage 

  1. super() can be used to refer to an immediate parent class instance variable.
  2. super() can be used to call the method of the immediate parent class.
  3. super() can be used to call the constructor of the immediate parent class.

1: Super() can be used to refer to an immediate parent class instance variable.

We can use the super keyword to access the parent class data member or field. It is used when the fields in the parent and child classes are the same.

class Sample{
String color="white";
}
class Basic extends Sample{
String color="Green";
void print(){
System.out.println(color);//prints color of Basic class
System.out.println(super.color);//prints color of Sample class
}
}
class Test{
public static void main(String args[]){
Basic v=new Basic();
b.print();
 }}

Output:

White
Green

Color is a property shared by both the Sample and Basic classes in the preceding example. By default, if we print the color property, it will print the color of the current class. The super keyword is required to access the parent property.

2: super() can be used to call the method of the immediate parent class.

The super keyword can also be used to call a method in the parent class. If the subclass contains the same method as the parent class, it should be used. In other words, it is called when a method is overridden.

class Sample{
void eat(){System.out.println("eating");}
}
class Basic extends Sample{
void eat(){System.out.println("eating food");}
void drink(){System.out.println("drinking");}
void Samp(){
super.eat();
drink();
 }
 }
class Test{
public static void main(String args[]){
Basic b=new Basic();
b.Samp();
}}

Output:

eating
drinking

  • Because priority is given to local in the preceding example, if we call the eat() method from the Basic class, it will call the eat() method of the Basic class by default.

To invoke the parent class method, we must use the super keyword.

3: super() can be used to call the constructor of the immediate parent class.

If there is no super() or this method, the compiler will automatically add it to each class constructor().

The super keyword can also be used to call the constructor of the parent class.

Let’s look at a simple example:

class sample{
Sample(){System.out.println("DevelopersDome");}
}
class Basic extends Sample{
Basic(){
super();
System.out.println("Basic");
}
}
class Test{
public static void main(String args[]){
Basic b = new Basic();
}}

Output:

DevelopersDome
Basic

Example: Super Keyword

class Person{
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
}
}
class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
this.salary=salary;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
class TestSuper5{
public static void main(String[] args){
Emp S=new Emp(1,"Sagar",2000f);
S.display();
}}

Output:

1 Sagar 2000

You may like:

Instanceof Java with Example

Java final Keyword with Example

Hope this article will guide you to recognize all about the Java super Keyword with Example that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply