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

this keyword in Java programming language

final keyword in Java programming language

static keyword in Java programming language

 

 

Suggested for you

Keywords in Java language

Keywords in C++ language

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.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago