The super is a keyword defined in the java programming
language. Keywords are basically reserved words which have specific meaning
relevant to a compiler in java programming language likewise the super keyword
indicates the following :
-- The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
-- The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.
-- The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
-- The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.
Example to use the super keyword to call the constructor of
the superclass in the base class:
|
public class Class1{ public Class1(String arg){ super(arg); } |
-- The syntax super.<method_Name>() is used to give a call to a method of the superclass in the base class.
-- This kind of use of the super keyword is only necessary when we need to call a method that is overridden in this base class in order to specify that the method should be called on the superclass.
Example to use the super keyword with a method:
|
public class Class1{ public String String_Method(){ return super.overriden_String_Method(); } |
Use of Super:
super is used to reference the parent
class when a class extends another
for example, say we have the following 2 classes:
public class animal
{
public void printname()
{
system.out.println("I am an animal");
}
}
public class dog extends animal
{
public void printname()
{
system.out.println("I am a dog");
super.printname();
}
}
We see that dog is a class that extends animal.
if we write something the following
public static void main()
{
Dog d = new Dog()
d.printname()
}
then we will see the Dog class print the following:
I am a dog
I am an animal
The super keyword lets you access the parent class' member functions and variables. The dog class overrides the printname method but you can still call the animal's printname method by using super.printname()
for example, say we have the following 2 classes:
public class animal
{
public void printname()
{
system.out.println("I am an animal");
}
}
public class dog extends animal
{
public void printname()
{
system.out.println("I am a dog");
super.printname();
}
}
We see that dog is a class that extends animal.
if we write something the following
public static void main()
{
Dog d = new Dog()
d.printname()
}
then we will see the Dog class print the following:
I am a dog
I am an animal
The super keyword lets you access the parent class' member functions and variables. The dog class overrides the printname method but you can still call the animal's printname method by using super.printname()