Table of Contents
In this tutorial, we will discuss the concept of super keyword in Java programming language.
We can use super to access the variable(data member or feild) of the parent class and chile class have same name
Program 1
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.
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.