In this article super keyword in java we give the information about In Java, Super Keyword is a reference variable that is used to refer to parent class objects. Whenever we create an instance of a sub class, an instance of the parent class is automatically created.
Super Keyword in Java:
In Java, Super Keyword is a reference variable that is used to refer to parent class objects. Whenever we create an instance of a sub class, an instance of the parent class is automatically created. Which is called super reference variable.
In Java, Super Keyword is a reference variable that is used to refer to parent class objects.
In other words, “Super keyword is used in sub class to access superclass members.”
Whenever we create an instance of a sub class, an instance of the parent class is automatically created. Which is called super reference variable.
Use of Super Keyword –
- It is used to access the data members of the parent class (when the parent and child classes have members with the same name.)
- It is used to call the method of parent class.
- And it is used to call the default and parameterized constructor of the parent class.
Super keyword with Variables:
Using Super with variables When parent class and child class have same variable then we use super to access the variable of parent class.
// Program
class P_Class
{
int h = 120;
}
class C_Class extends P_Class
{
int h = 100;
void displayh()
{
System.out.println(“Value of H: ” + super.h);
}
}
class Test
{
public static void main(String[] args)
{
C_Class f = new C_Class();
f.displayh();
}
}
O/P:-
Value of H: 120
In the above example, both the base class and sub class have a variable named h. We have accessed h of base class using super keyword.
Super keyword with methods:
It is used when we want to call the method of parent class. It is used when base class and sub class have same method. That is, it is used when the method is overridden by the sub class.
Example:-
class Pclass
{
void putdata()
{
System.out.println(“This is Parent class method”);
}
}
class Sclass extends Pclass
{
void putdata() //Overriding method
{
System.out.println(“This is Child class method”);
}
void showD()
{
putdata(); //This would call Sclass method
super.putdata(); //This would call Pclass method
}
public static void main(String args[])
{
Sclass obj= new Sclass();
obj.showD();
}
}
O/P:-
This is Parent class method
This is Child class method.
In the above example you saw that when we are calling only putdata() then putdata() of subclass is being invoked. But when we are using super with putdata() then putdata() of pclass is also invoked.
Super keyword with Constructors:
The super keyword can also be used to access the constructor of the parent class. Most importantly, it can call both default and parameterized constructors.
class PClass
{
PClass ()
{
System.out.println(“Super class Constructor”);
}
}
class SClass extends PClass
{
SClass ()
{
super();
System.out.println(“Sub class Constructor”);
}
}
class Test
{
public static void main(String[] args)
{
SClass h = new SClass ();
}
}
O/P:-
Super class Constructor
Sub class Constructor
In the above example we have called the constructor of super class using super keyword.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining