super Keyword in Java

The super keyword in Java is used to refer to the parent class of a subclass.

It can be used to:

  • Call a superclass constructor
  • Access a superclass field
  • Call a superclass method

Calling a superclass constructor

When a subclass is created, its constructor must call the constructor of its parent class. This is done using the super() keyword, which calls the constructor of the parent class.

For example:

class Animal {
  public Animal() {
    System.out.println("Animal constructor");
  }
}

class Dog extends Animal {
  public Dog() {
    super(); // This calls the Animal constructor
    System.out.println("Dog constructor");
  }
}

Accessing a superclass field

If a subclass has a field with the same name as a field in its parent class, the super keyword can be used to access the field in the parent class.

For example:

class Animal {
  protected String color = "white";
}

class Dog extends Animal {
  public String color = "black";

  public void printColor() {
    System.out.println(this.color); // Prints "black" from child class
    System.out.println(super.color); // Prints "white" from parent class
  }
}

Calling a superclass method

If a subclass overrides a method in its parent class, the super keyword can be used to call the method in the parent class.

For example:

class Animal {
  public void makeSound() {
    System.out.println("Animal sound");
  }
}

class Dog extends Animal {
  @Override
  public void makeSound() {
    System.out.println("Woof!");
  }

  public void callSuperMakeSound() {
    super.makeSound(); // This calls the Animal.makeSound() method
  }
}

The super keyword can be a useful tool for accessing and calling methods and fields in the parent class. It is important to use it correctly to avoid errors.

Things to keep in mind about the super keyword

  • The super keyword must be the first statement in a constructor.
  • The super keyword cannot be used in a static context.
  • The super keyword can be used to access the superclass constructor, even if the subclass has its own constructors.

FAQs

When is the super keyword most commonly used?

The super keyword is commonly used in the following situations:

To call a superclass constructor from the subclass constructor.

To access overridden methods or fields of the superclass in the subclass.

To resolve ambiguity when a subclass inherits multiple methods or fields with the same name from different superclasses.

Can the super keyword be used outside of constructors in Java?

Yes, the super keyword can be used outside of constructors as well, such as in methods or instance initialization blocks, to access superclass methods and fields. It’s not limited to constructors.

What is the difference between super and this keywords in Java?

The super keyword is used to refer to members of the superclass, while the this keyword is used to refer to members of the current instance (either in the current class or superclass). super is often used to resolve name conflicts between superclass and subclass members, while this is used for clarity within a single class.

Can I use the super keyword in a static method?

No, you cannot use the super keyword in a static method because static methods are associated with the class itself, not with instances of the class. The super keyword is used to reference members of instances, and static methods do not have access to instance-specific context.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments