java

super keyword in Java programming language

Super keyword in Java programming language

In this tutorial, we will discuss the concept of super keyword in Java programming language.

Super key word in Java

  • The super keyword is a reference variable that is used to call immediate parent class object.
  • It is used inside a child class method to call a method or datamember defined in the parant class.
  • A private method of the super class can not be called.
  • Only public and protected methods can be called by the super keywords.
  • It is also used by class constructor to execute constructors of its immediate parant class.

 

Usage of keyword

  • super is used to refer to immediate parent class instance variable.
  • super() is used to execute immediate parent class  constructor.
  • super is used to execute immediate parent class method.
We can use super keyword to access the data member or field of immediate parent class and it is used if parent  and child classes have the same fields(same name).

Diagram

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

We can use super to access the variable(data member or feild) of the parent class and chile class have same name

Program 1

Example

In the above program, we can see that there are two classes available class A and class B. both classes have a common property which is named “a”(a is an integer variable assigned  different values of each every classes). we can access the current class property by default(a) but to access the parent class property. we need to use the super keyword.

Program 2
Example

In the above two programs, super keyword was used to  call immediate parent class object from another(child) class.

bus and vehicle both classes(parent and child classes)can have a commen property which is named speed.

if we want to access to the speed property of the parent class, we must use the super keyword

when we need to access the speed property of the current class it can be suceeded by default

 

super is used to execute immediate parent class method.

It can be used to invoke parent class method. when we used super keyword to invoke both the child class and parent class both should  contain the same methods. as it is used when the method is overridden

Program 1

class Transport{
public static void main(String args[]){
Bus b=new Bus();//create object
b.travel();//call the travel metod 
}
}
class Vehicle{
void drive(){
   System.out.println("vehicle is drived....");
}
}

class Bus extends Vehicle{
void drive(){
   System.out.println("Bus is drived fast");
}

void brake(){
   System.out.println("Bus is breaked suddenly");
}

void travel(){
   super.drive();//used super keyword to call parant class method
   drive();//call current class metod same as parent class metod
   brake();//call current class metod 
}
}

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

vehicle is drived…

Bus is drived fast

Bus is braked suddenly

 

In the above program, Vehicle and Bus both class have drive() method, when we  call the drive() method from Bus class it will execute the drive() method of the bus class as default. But when we use super keyword with drive() method it is executed the parent class method

super() is used to execute immediate parent class  constructor.

The super() can be used to invoke the parent class constructure

Program 1

class Transport1{
public static void main(String args[]){
Bus b=new Bus();//create object to call constructer
}
}
class Vehicle{
Vehicle(){//create constructer
   System.out.println("Many type of veicles here");
}

}

class Bus extends Vehicle{
Bus(){//create constructer
super();//call to imediate parent class constructer
   System.out.println("Bus is a passenger vehicle"); 
}
}

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

Many type of veicles here

Bus is a passenger vehicle

 

 

Related post

Keyword in Java language
Final keyword in Java

This keyword in Java

 

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.