Java Program for Super keyword

In this tutorial, we will write a program in java for the use of super keyword. Before that, you should have knowledge on the following topic in Java.


Java Program for Super keyword

Example of a super keyword to differentiate the members of the superclass from a subclass.

class SuperClass
{
  int id = 20;
  public void display()
  {
    System.out.println("Super Class display method");
  }
}

public class SubClass extends SuperClass
{
  int id = 10;
  public void display()
  {
    System.out.println("Sub Class display method");
  }

  public void myMethods()
  {
    // Instantiating subclass
    SubClass s = new SubClass();

    // display method and value of sub class
    s.display();
    System.out.println("Sub Class id:" + s.id);

    // display method and value of super class using super
    super.display();
    System.out.println("Super Class id::" + super.id);
  }

  public static void main(String args[])
  {
    SubClass sub = new SubClass();
    sub.myMethods();
  }
}

Output:

Base Class display method
Sub Class id:10
Super Class display method
Super Class id::20