Java super Keyword
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java super Keyword

Harine

Java super Keyword

In Java, the super keyword is used within a subclass to refer to its immediate parent class (superclass). It is primarily used when a subclass needs to access or reuse members from its parent class that are hidden, overridden, or shared.

The super keyword plays an important role in inheritance, helping maintain clear relationships between parent and child classes.

What Is the super Keyword in Java?

super provides a reference to the parent class and is commonly used in three ways:

  • Access parent class methods
  • Access parent class fields (attributes)
  • Invoke the parent class constructor

1. Calling a Parent Class Method with super

When a subclass overrides a method, it can still call the original parent version using super.methodName().

Example: Calling overridden parent method

class Appliance {

   public void powerOn() {

     System.out.println("Appliance is powering on");

   }

}

class WashingMachine extends Appliance {

   @Override

   public void powerOn() {

     super.powerOn(); // call parent method

     System.out.println("Washing machine is ready to use");

   }

}

public class SuperMethodDemo {

   public static void main(String[] args) {

     WashingMachine wm = new WashingMachine();

     wm.powerO

}

}

Output

Appliance is powering on

Washing machine is ready to use

When to use:

Use super when you want to extend — not completely replace — parent behavior.

2. Accessing Parent Class Fields with super

If a subclass defines a field with the same name as its parent, the parent field becomes hidden. Use super.fieldName to access it.

Example: Accessing hidden parent attribute

class Device {

   String category = "Electronic Device";

}

class Laptop extends Device {

   String category = "Portable Computer";

   public void showCategories() {

     System.out.println("Child: " + category);

     System.out.println("Parent: " + super.category);

   }

}

public class SuperFieldDemo {

  public static void main(String[] args) {

    Laptop laptop = new Laptop();

    laptop.showCategories();

  }

}

Output

Child: Portable Computer

Parent: Electronic Device

3. Calling the Parent Constructor with super()

super() invokes the constructor of the parent class. This is commonly used to reuse initialization logic defined in the superclass.

Rule: super() must be the first statement in a subclass constructor.

Example: Constructor chaining with super

class Account {
   protected String owner;
   public Account(String owner) {
     this.owner = owner;
     System.out.println("Account created for " + owner);
   }
}
class SavingsAccount extends Account {
   private double interestRate;
   public SavingsAccount(String owner, double interestRate) {
     super(owner); // call parent constructor
     this.interestRate = interestRate;
     System.out.println("Savings account with interest " + interestRate + "%");
   }
}
public class SuperConstructorDemo {
  public static void main(String[] args) {
     SavingsAccount acc = new SavingsAccount("Ravi", 4.5);
  }
}

Output

Account created for Ravi
Savings account with interest 4.5%

Why Use the super Keyword?

The super keyword helps maintain proper inheritance structure and reuse parent logic.

Benefits

  • Access overridden parent methods
  • Resolve naming conflicts between parent and child members
  • Reuse superclass constructors
  • Extend behavior safely

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !