Categories: Keyword in Java

Usage of super in Java programming language

Usage of super in Java programming language

In this tutorial, We will discuss Usage of super in  Java programming language

super is one of the Java keywords in Java. It is a reference variable mainly used to refer to objects in the immediate parent class. This keyword is used in various ways in Java.

  • The super keyword is a reference variable that is used to call objects in the immediate parent class.
  • It is used inside a child class method to recall data members defined in the parent class.
  • If the method is private, super cannot be used to call from the parent class.
  • Only public and protected methods can be called by the super keywords.
  • It is also used by class constructor to executes in the immediate parent class.

Usage of super in Java programming language

  • super is used to refer to immediate parent class instance variable
  • super() is used to execute immediate parent class instance constructor
  • super is used to execute immediate parent class method

 

We can use super keyword to access the data member or field of the immediate parent class and it is used if parent class and child class have the same fields

Syntax

super.base class feature

Access data member

1 super keyword is used to refer to immediate parent class instance variable. – variable level

Syntax

super.base-class data member name

Access constructor

2. super() is used to invoke immediate parent class constructor. – constructor level

syntax

super.constructor_name(); 

super.display();

Access Method

syntax

3. super is used to invoke immediate parent class method – method level

super.method_name();

super.display();

 

Usage of the super keyword in Java – Diagram

Super can be used to access immediate parent only

Usage of super keyword

 

Super keyword – Usage 1

1. Super keyword is used to immediate parent class instance variable

Example

Program 1

class AA {//parant class
  int a=15;
}
class BB extends AA{
  int a=20;
  
  void display()
  {
  System.out.println(a);
  //This statement display property of class BB
  //or property of child class
  
  System.out.println(super.a);
  //super keyword display imidiate parant class property
  //or property of parent class
  
  }
  public static void main(String args[]){
  BB value=new BB();
  value.display();
  }

}

 

When the above code is compiled and executed, it will produce the following results

20
15

 

At the above program, super keyword helps to access the property of immediate parent class

Program 1

class bus{//base class, immediate parent class of car class
    int speed=100;//global variable inside bus class
}
class car extends bus{
    int speed=200;//global variable inside car class
    
    void speed_car(){ //method_1 inside of car class
    System.out.println("The speed of car :"+speed);
    }
    
    void speed_bus(){ //method_2 inside of car class
    System.out.println("The speed of car :"+super.speed);
    }
    
    public static void main(String []args){
        car C=new car();// create object
        C.speed_car();//call method
        C.speed_bus();
        
    }
}

 

When the above code is compiled and executed, it will produce the following results

The speed of car :200

The speed of bus :100

 

Super keyword – Usage 2

The super keyword can be used to invoked parent class method

class Employee_Details{//Base class 
void get_detail(){//method inside base class
System.out.println("This is an employee detail class");
}

}

class Clark extends Employee_Details{//sub class
void get_detail(){//method inside subclass
System.out.println("This is a Clark class");
}

void display(){//that display() method only in Clark class
get_detail(); 
//To call or invoked current class get_detail() method

super.get_detail(); 
//To call or invoked parent class get_detail() method
//super keyword used to invoked parent class method
}

}

class Test_2{//testing class
public static void main(String args[]){
Clark c=new Clark();//create object for Clark class
c.display();//call the display() method

}
}

 

When the above code is compiled and executed, it will produce the following results

This is a Clark class
This is an employee detail class

we already have known about method overriding of Java in the previous tutorial.

When a child class declares a method which is already present in the parent class with the same name. this is called method overriding in Java language

In the above program, base class and subclass both have same methods get details().

when we call the method from child class object, the child class overridden parent class method.

But, by using the super keyword (super.method_Name) we can be called the parent class method

 

Super keyword – Usage 3

The super keyword can be used to invoked parent class constructor

class Vehicle{//parent class
Vehicle(){//constructor for parent class
System.out.println("We purchase a vehicle");
}

}
class Van extends Vehicle{//chile class
Van(){////constructor for child class
super();
System.out.println("It may be a van");
}

}

class Test_Vehicle{
public static void main(String args[]){
Van v=new Van();
}
}

 

When the above code is compiled and executed, it will produce the following results

We purchase a vehicle
It may be a van

 

Super keyword – Usage 4

Parameterized super() call to execute parameterized constructor of the parent class

class Vehicle //parent class
    {
Vehicle(){//no arg-constructor in parent class
System.out.println("We purchase a vehicle ");
        }
Vehicle(String str)//arg-constructor in child class
        {
System.out.println("the vehicle consists of 50 seats ");
        }
}

class bus extends Vehicle //child class
{
bus()//constructor in child class
        {
super("hello");//super keyword with argument
//super() must be added to the first statement of constructure
//if not, comes out compilation error
System.out.println("It may be a bus ");
        }
}

class test_class1
{
public static void main(String args[]){
bus b=new bus();
}
}

When the above code is compiled and executed, it will produce the following results

the vehicle consists of 50 seats
It may be a bus

There are other Java language keywords that are similar to this keyword

abstract keyword in Java

boolean Keyword in Java

break keyword in Java

Byte keyword in Java 

case keyword in Java 

catch keyword in Java 

char keyword in Java 

class keyword in Java 

continue keyword in Java 

default keyword in Java 

do keyword in Java

double keyword in Java

else keyword in Java

enum keyword in Java

extends keyword in Java

final keyword in Java

finally keyword in Java

float keyword in Java

for keyword in Java

if keyword in Java

static keyword in Java

super keyword in Java

transient keyword in Java

public keyword in Java

private keyword in Java

protected keyword in Java

 

Suggested for you

Usage of this statement in the Java language

Keywords in Java language

Keywords in C language

Keywords in Python language

One dimension Array in Java

Two dimension Array in Java

Three dimension Array in Java

Keywords in C language

Keywords in Python language

 

The enum keyword in Java programming language
Usage of final in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago