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.
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
1 super keyword is used to refer to immediate parent class instance variable. – variable level
Syntax
super.base-class data member name
2. super() is used to invoke immediate parent class constructor. – constructor level
syntax
super.constructor_name(); super.display();
syntax
3. super is used to invoke immediate parent class method – method level
super.method_name(); super.display();
Super can be used to access immediate parent only
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
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
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
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
Usage of this statement in the Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…