Reshma Bidikar

What is the Super Keyword?

Super keyword is used to access members of the superclass from the subclass. It can be used to access both instance fields and methods.

Code Sample

Consider the following code:

public class Base {
  
  int a;
  int b;
  
  public void baseMethod() {
    System.out.println("In base class method”);
  }

}

public class Sub extends Base {
  
  int c;
  
  public void subMethod() {
    super.a=4;
    super.b=9;
    super.baseMethod();
    System.out.println("In sub class method");
  }
  
}

When this code is executed, it will print the following output:

In base class method
In sub class method

Points to Remember