Home >>Java Tutorial >Super keyword in Java

Super keyword in Java

Super keyword in Java

The super keyword in Java language is basically a reference variable that is used in order to refer the immediate parent class object. Whenever the instances of subclasses are created by the programmer then an instance of the parent class is created implicitly that has been referred by the super reference variable in the language.

Super variable generally refers to the immediate parent class instance ad it can be used to invoke the immediate parent class method. The function super () basically acts as an immediate parent class constructor and it I mandatory that it should be the first line in the child class constructor. In simpler words it can be said that the super keyword is used to refer to the superclass (parent) objects.

There are other uses of the super keywords as this is also used to call superclass methods, and in order to access the superclass constructor. However, the most common use of all is that the super keyword is used to eliminate the confusion that arises between the superclasses and subclasses and that confusion arises due to the fact that they have methods with the same name.

It is recommended to all the readers and the programmers that you should have a basic understanding of the inheritance and the polymorphism before proceeding to learn the super keyword.

Example of the Super Keyword in Java language

Here we have elaborated the example of the super keyword according to the context of their functions that are generally used with them.

1. Here is an example that is depicting the use of the super class in parent class instance variable:

class Animals
{
String color = "Gray";
}
 
class Dogs extends Animals
{
  
String color = "white";
  
void showColor()
  {
    
System.out.println (color);	//This will print color of Dogs class  
System.out.println (super.color);//This will print color of Animals 
} 
} 
class Test
{
  
public static void main (String args[])
  {
Dogs obj = new Dogs ();
    
obj.showColor ();

}
    
} 
Output:
white
gray

2. Here is an example of the super keyword that is depicting the use of it in invoking the parent class method:

class Animal
{
void eat ()
  {
    System.out.println ("eating...");
} 
} 
class Dog extends Animal
{
  
void eat ()
  {
    System.out.println ("eating bread...");
  } 
void bark ()
  {
    System.out.println ("barking...");
  } 
void work ()
  {
    
super.eat ();
    
bark ();

} 
} 
class TestSuper2

{
  
public static void main (String args[])
  {
    
Dog d = new Dog ();
    
d.work ();

}
    
} 
Output:
eating...
barking...

3. Here is an example that is depicting the use of super keyword in invoking the parent class constructor, learn the example carefully to gain the most of this concept:

class Animal
{
  
Animal ()
  {
    System.out.println ("animal is created");
  }
 
}
 
class Dog extends Animal
{
  
Dog ()
  {
    
super ();
    
System.out.println ("dog is created");
  
}

}


class TestSuper3
{
  
public static void main (String args[])
  {
    
Dog d = new Dog ();

}
    
} 
Output:
animal is created
dog is created

No Sidebar ads