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.
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:
- 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.superwould enable subclasses to interfere with the internal workings of their ancestral classes, leading to a less secure and more error-prone code. - 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.
- 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:
- Design Patterns: Sometimes, issues requiring access to
super.supercan 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
| Scenario | Description | Best Practice |
| Direct superclass access | Need to use or override the parent class method. | Use super.method() to enhance or modify behavior safely. |
| Access beyond the direct superclass | Intend 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
- Why is synchronized block better than synchronized method?
- Why is the Java 11 base Docker image so large? openjdk11-jre-slim
- Why is the Java 11 base Docker image so large? openjdk11-jre-slim
- Why is there no GIL in the Java Virtual Machine? Why does Python need one so bad?
- Why is there no GIL in the Java Virtual Machine? Why does Python need one so bad?
- Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
- Why is there no need for Maven in .NET?
- Why is there no SortedList in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.