Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

super keyword in Java programming language

Posted on February 25, 2017June 29, 2020

Table of Contents

  • Super keyword in Java programming language
    • Super key word in Java
      • Usage of keyword
      • super keyword is used to refer to immediate parent class instance variable.
      • super is used to execute immediate parent class method.
      • super() is used to execute immediate parent class  constructor.
      • Related post

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).

super keyword in Java programming language
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

super keyword in Java programming language
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
super keyword in Java programming language
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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes