Java
Programming
Coding Standards
Object-Oriented Programming
Method Calls

Why is super.super.method(); not allowed in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Java, the concept of invoking a superclass's method is fundamental in the context of inheritance; however, directly accessing a method from a grandparent class, which could be termed as super.super.method();, is explicitly disallowed. This prohibition is rooted deeply in the principles of Java's inheritance and encapsulation which aim to promote code reusability and maintainability. This article will explore the reasoning behind this restraint and examine alternative approaches that achieve similar outcomes.

Understanding Java's Inheritance Model

In Java, when a class inherits from another class, it gains access to its public and protected fields, methods, and constructors, except for the private ones. The keyword super is used to refer specifically to the superclass immediately above the current class in the inheritance hierarchy. This mechanism ensures that each class only needs to know about its direct ancestor, simplifying the design and interaction of class hierarchies.

Why super.super.method(); is Not Allowed

The syntax super.super.method(); would, in theory, allow a subclass to bypass its immediate parent class and directly access or modify the members of its grandparent class. This raises several issues:

  1. Violation of Encapsulation: Encapsulation is a core principle in object-oriented programming that prevents data access directly, ensuring that object data is hidden and protected through methods. Allowing super.super would enable subclasses to interfere with the internal workings of their ancestral classes, leading to a less secure and more error-prone code.
  2. Maintainability Issues: Allowing such access would make the system more complex and harder to maintain. If a superclass changes its internal implementation, it could inadvertently break subclasses that were relying on those internal methods and fields being present and accessible.
  3. Reduces Code Cohesion: Proper object-oriented design encourages classes to be self-contained. If subclasses need to know the implementation details of their ancestor classes deeper in the hierarchy, it likely indicates poor design choices that harm the cohesion of the code.

Alternatives and Workarounds

Instead of using super.super.method(), which is not allowed, Java designers and developers should use other object-oriented principles and techniques to achieve desired results:

  • Method Overriding: If a method from the grandparent class needs to be used in a way that the immediate parent class's method does not suffice, the method can be overridden in the parent class to either replicate the grandparent's functionality or to directly call the grandparent’s method. This method ensures proper use of super:
java
1class Grandparent {
2    void display() {
3        System.out.println("Grandparent method.");
4    }
5}
6
7class Parent extends Grandparent {
8    @Override
9    void display() {
10        super.display();  // Calls Grandparent's display
11    }
12}
13
14class Child extends Parent {
15    @Override
16    void display() {
17        super.display();  // Calls Parent's display, which calls Grandparent's display
18    }
19}
  • Design Patterns: Sometimes, issues requiring access to super.super can be resolved by using appropriate design patterns, like Decorator, Strategy, or Bridge. These patterns can help alter or extend the behavior of classes in a more flexible and maintainable way.
  • Interface-based Composition: Prefer composition over inheritance where possible. Instead of creating deep class hierarchies, you can define behaviors with interfaces and compose them in classes as needed.

Summary Table: Super Usage and Best Practices

ScenarioDescriptionBest Practice
Direct superclass accessNeed to use or override the parent class method.Use super.method() to enhance or modify behavior safely.
Access beyond the direct superclassIntend to bypass the immediate parent class.Opt for design patterns, method overriding wisely, or re-evaluate the design for better encapsulation using composition.

Conclusion

The restriction on using super.super.method() in Java is a design decision that encourages better software development practices, promoting secure, maintainable, and cohesive code. Rather than viewing this limitation as a hindrance, developers should see it as guidance towards better design decisions, utilizing object-oriented principles effectively.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.