Usage of super keyword in Java programming language

Usage of super keyword in Java programming language

In this article, we will discuss the usage of super keyword in Java programming language

In this post, we are going to learn how to use super keyword in java variable methods and constructor etc

The super keyword is used as the reference variable in java language and it refers to immediate parent class object

 

super is one of the java keywords, this keyword is used various way in java.

1 super keyword is used to refer immediate parent class instance variable.
2. super() is used to invoke immediate parent class constructor.
3. super is used to invoke immediate parent class method

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

We can use the super keyword to access the variables or data members or fields of the immediate parent class when the parent and child class both are the same type of fields

before using super keyword

class Vehicle{
int speed =70;      //oarent class variable
}
class bike1 extends Vehicle
{
int speed=100;     //child class variable
void display()
{

System.out.println(speed);    //now prints speed of bike1

}

public static void main(String args[])
{
bike1 b=new bike1();  //create the object
b.display();                //call the method
}
}

 


After use super key words

class Vehicle{
int speed =70;
}
class bike1 extends Vehicle
{
int speed=100;
void display()
{

System.out.println(super.speed); //now print speed of Vehicle

//here used super keyword

}

public static void main(String args[])
{
bike1 b=new bike1();
b.display();
}
}

In the above program. vehicle and bike1 both classes have common variables speed.

In the first example, when we print speed it will display speed of chile class(current class)  – no need to use super keyword

In the second example, when we print speed it will display the speed of parent class(immediate parent class)  – we need to use super keyword

 

2. super() is used to invoke the immediate parent class method.

We can use the super keyword to access the method of the immediate parent class when the parent and child class both are the same method

before using super keyword

class person //parent class
{
void message()  //method of  parent class
{
System.out.print(“welcome”);

}
}

//child class

class student12 extends person
{
void display() //method of child class
{
System.out.print(“welcome to java”);
}
public static void main(String args[]){
student12 obj=new student12(); //create object
obj.display();  //call the method
}
}

When the above code is executed, it produces the following result

After use super key words

class person
{
void message()
{
System.out.println(“welcome”);

}
}

class student123 extends person
{
void message()
{
System.out.println(“welcome to java”);
}

void display()
{
message();  // will invoke current class message() method
super.message(); // will invoke parent class message() method
}


public static void main(String args[]){
student123 obj=new student123();
obj.display();
}

}

When the above code is executed, it produces the following result

 

In the above program. person and student123 both classes have common method void display().

In the first example, when we call the method display it will print property of display() method in chile class(current class)  – no need to use super keyword

In the second example, when we call the method display() it will print property of display() method in parent class(immediate parent class)  – we need to use super keyword

 

 


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

We can use this keyword to access the constructor of the immediate parent class when the parent and child class both are the same constructor

class vehicle1{
vehicle1() {
System.out.println(“Vehicle of four wheller”);
}

}
class bike11 extends vehicle1{
bike11(){
super();                        //will invoke parent class constructor
System.out.println(“bike is two weeler”);

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

Example

When the above code is executed, it produces the following result

 

Suggested for you

Final keyword in Java

This keyword in Java

Keywords in  C language

Keywords in Python language

 

 

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.