Explain Codes LogoExplain Codes Logo

In Java, how do I call a base class's method from the overriding method in a derived class?

java
inheritance
superclass
method-overriding
Alex KataevbyAlex Kataev·Jan 10, 2025
TLDR

Supercharge your overridden methods by invoking the superclass's method using super.methodName(). This can be done directly from inside the derived class's overriding method.

class SuperClass { void display() { System.out.println("Superclass display"); } } class SubClass extends SuperClass { @Override void display() { super.display(); // Break-in session: Superclass's display System.out.println("Subclass display"); // Posh display time: SubClass } }

Invoking super helps you unlock the power of the superclass's method without the need for creating a new instance or setting up additional methods.

super: The golden key to overriding methods

Overriding methods in Java need not be an uphill battle. Leveraging the super keyword lets you extend the functionality of your class without missing out on the perks of inherited functionality.

When to say 'Hello' to super

  • Superclass's default behavior: Use super to ensure the stained glass window of your superclass's functionality is available.
  • Tailoring method behavior: Start with super and remix it to dance to your derived class's tune.
  • Evading code repetition: When superclass has generously given what your derived class needs, use super instead of duplicating code.

Riding constructor express

super can also help you travel up the hierarchy tree to call superclass's constructor, via the express lane.

class ParentClass { ParentClass(int x) { System.out.println("ParentClass: So, you think you're into construction!"); } } class ChildClass extends ParentClass { ChildClass() { super(10); // Mommy calls! System.out.println("ChildClass: Guess who learnt to use constructor from the best!"); } } // Output: // ParentClass: So, you think you're into construction! // ChildClass: Guess who learnt to use constructor from the best!

Tricky tantrums of super

Although the super keyword packs a punch and allows us to accesses superclass behavior, it can throw a few curveballs:

  • Constructor calls: You can't yell super() or this() anywhere other than the constructor.
  • Premature invocation: Avoid invoking super before the time is ripe (i.e., in a constructor before the superclass's constructor has run its course).

Decoding method resolution

Java's runtime system, in its hunt for the invoked method, starts from the bottom of the class hierarchy and moves up the tree until it finds its target. This ensures behavior that is:

  • Specific: Closer to the object's actual type.
  • Deterministic: Each class in the hierarchy can only house one definition for a method, sealing the deal against ambiguities.
  • Predictable: The method resolution process and its outcome depend solely on the inheritance hierarchy.

Bounties of using super

The super keyword transforms your code in numerous ways:

  • Elevated maintainability: With super, modifications in the superclass flow seamlessly into the derived classes.
  • Enhanced readability: Seeing super tells you that the base class functionality is being tinkered with.
  • Greater flexibility: You can call base methods and even revamp behavior according to your requirements, all while playing nice with the original design's compatibility.

Advanced use cases

Peek into more real-world examples:

Inheritance on steroids

In a hierarchy with multiple levels of inheritance, super calls the most immediate ancestor:

class Grandparent { void say() { System.out.println("Hello from Grandparent"); } } class Parent extends Grandparent { @Override void say() { super.say(); // Grandparent says hello! System.out.println("Hello from Parent"); } } class Child extends Parent { @Override void say() { super.say(); // Parent says hello! System.out.println("Hello from Child"); } } // Output: // Hello from Grandparent // Hello from Parent // Hello from Child

Prepping with super and abstract methods

Incorporate super with abstract methods and you'll be mastering more predictable designs and easier maintenance:

abstract class AbstractClass { abstract void abstractMethod(); void concreteMethod() { System.out.println("Concrete and proud of it!"); } } class ConcreteClass extends AbstractClass { void abstractMethod() { super.concreteMethod(); // ConcreteClass says: I got it from my mama! System.out.println("Abstract no more!"); } }

Picking up these tricks ensures efficient and intuitive code that superbly leverages the behaviors encapsulated in the superclasses.