AppDividend
Latest Code Tutorials

Super Keyword In Java Tutorial | Java Super Keyword Example

0

Super Keyword In Java Tutorial | Java Super Keyword Example is today’s topic. The super keyword in Java is the reference variable that is used to refer the parent class objects. Java “superkeyword came into the picture with a concept of Inheritance. Super is a reference variable that is used to indicate an immediate parent class object. Whenever you create an instance of the subclass, an instance of the parent class is created implicitly, which is referred by a super reference variable. Before learning the super keyword, you must have the knowledge of inheritance in Java so that you can understand easily.

Super Keyword In Java

Super can be used to refer to the immediate parent class instance variable. Super can be used to invoke the direct parent class method. Super() can be used to invoke an immediate parent class constructor

The syntax of the super keyword is following.

super(argument-list);

#Important points about Super Keyword

  1. Call to super() must be a first statement in the derived class constructor.
  2. If the constructor does not explicitly invoke the superclass constructor, the Java compiler automatically inserts the call to the no-argument constructor of a superclass. If a superclass does not have the no-argument constructor, you will get the compile-time error. The object does have such a constructor, so if an Object is the only superclass, there is no problem.
  3. If the subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that the whole chain of constructors called, all the way back to the constructor of an Object. It is called constructor chaining.

#Use of super with constructors

The super keyword can also be used to access the parent class constructor. One more important thing is that the ‘’super’ can call both parametric as well as nonparametric constructors depending upon the situation.

We can call the super() explicitly in the constructor of the child class, but it would not make any sense because it would be redundant. It’s like explicitly doing something which would be implicitly done otherwise.

However when we have the constructor in the parent class that takes the arguments, then we can use the parameterized super, like super(100) to invoke the parameterized constructor of the parent class from the constructor of a child class.

See the following program.

class Bike /* super class */
{
  Bike() /* constructor is created */
  {
    System.out.println("Bike class is created");
  }
}

class Cycle extends Bike /* child class */
{
  Cycle() {
    super(); /* calling the parent class */
    System.out.println("Cycle class is created");
  }
}

class goal {
  public static void main(String[] er) {
    Cycle c = new Cycle(); /* creating the object of Cycle class */
  }
}

See the following output.

 

Super Keyword In Java

In the main class, we never create an object of the parent class. In the above example, we have called a superclass constructor using the keyword ‘super’ via subclass constructor.

#Use of super with methods

For invoking the parent class method, the child class should have the same method name as the parent class. So whenever the parent and child class have same named methods then to resolve the ambiguity, we use the super keyword. This is the perfect example of super keyword, which is calling forth(invoke) direct parent class method.

When a child class declares the same method, which is already present in the parent class, then this is called a method overriding. We will learn the method overriding in the next tutorials of this series.

For now, you need to remember this: When a child class overrides the method of a parent class, then a call to the method from the child class object always call a child class version of the method.

However by using the super keyword like this: super.method_name, you can call the method of the parent class (the method which is overridden).

In the case of method overriding, these terminologies are used: Overridden method: The method of parent class Overriding method: The method of a child class.

See the following program for more understanding.

class Bike /* parent class */
{
  void helmet() /* method is created */
  {
    System.out.println("Bike class is created");
  }
}

class Cycle extends Bike /* child class */
{
  void helmet() {
    System.out.println("Cycle class is created");
  }

  void show() {
    super.helmet(); /* It will call the parent class method */
    helmet(); /* It will call present class method */
  }
}

class goal2 {
  public static void main(String[] er) {
    Cycle c = new Cycle();
    c.show();
  }
}

See the following output.

 

Use of super with methods

Note: There must be a method of the same name in both parent and child class.

#Use of super with variables

It is used when the parent class and child class have the same variable name. When you have a variable in a child class, which is already present in the parent class than to access the variable of the parent class, you need to use the super keyword.

class Bike     /*Parent class*/
{ 
   int speed=40;
}
class Cycle extends Bike
{
   int speed=20;
   void show( )
   {
     System.out.println("Average speed: "+super.speed);   /*calling parent speed variable*/
     System.out.println("Average speed: "+speed);         /*calling current variable*/
   }
}
class goal3
{
   public static void main(String [ ] er)
   {
       Cycle c = new Cycle( );
       c.show( );
   }
}

See the following output.

 

Use of super with variables

#Accessing the num variable of the parent class

By calling a variable like this, we can access the variable of parent class if both the classes (parent and child) have the same variable.

super.variable_name

#Second use case for super

The second form of super acts somewhat like this keyword, except that it always refers to the superclass of the subclass in which it is used.

This usage has the following general form.

super.member

The member could be anything that is a method or a variable.

See the following program of super is used to call forth(invoke) immediate parent class variable.

class Bike {
  int i;
}

class Cycle extends Bike {
  int i; // this i hides the i in Bike

  Cycle(int a, int b) {
    super.i = a; // i in Bike
    i = b; // i in Cycle
  }

  void show() {
    System.out.println("i in superclass: " + super.i);
    System.out.println("i in subclass: " + i);
  }
}

class UseSuper {
  public static void main(String args[]) {
    Cycle c = new Cycle(1, 2);
    c.show();
  }
}

See the following output.

 

Second use case for super

#Are superclass’s Constructor Inherited

No. They can’t be inherited, and they are invoked explicitly or implicitly, explicitly using the super keyword.

Finally, Super Keyword In Java Tutorial | Java Super Keyword Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.