super in Java
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
In Java, super is a reserved keyword that plays a critical role in object-oriented programming (OOP), particularly in the realm of inheritance. It serves as a reference variable that points to the immediate parent class of an object. By using super, developers can access methods and constructors from the parent class when extending a base class, thereby enhancing code functionality and reusability.
Understanding super in Java
Java does not support multiple inheritance directly to avoid ambiguity, but the use of super allows developers to manage inheritance effectively by enabling the following:
- Access Parent Class Variables: If child and parent classes have instance variables with the same name,
superhelps reference the parent class variable explicitly. - Call Parent Class Methods: When a child class overrides methods of the parent class,
supercan be used to invoke the overridden method from the parent class. - Invoke Parent Class Constructor:
supercan be used to call constructors from the parent class, ensuring that the parent class is initialized properly before the child class is initialized. This should be the first call within a constructor of the child class.
Examples
Example 1: Accessing Parent Class Variable
In this example, the parent class Animal and the child class Dog both have a variable name. Using super.name, the child class can access the name variable from its parent class.
Example 2: Calling Parent Class Method
Here, super.sound() allows the child class to invoke the overridden sound method in the Animal class.
Example 3: Using super to Call Parent Class Constructor
This example demonstrates the use of super() to call the parent class Animal constructor from the child class Dog constructor.
Summary
The following table outlines the key points when using super in Java:
| Aspect | Description |
| Access Parent Class Variable | super.variableName is used to access a variable from the parent class |
| Call Parent Class Method | super.methodName() can invoke a method from the parent class |
| Invoke Parent Class Constructor | super() must be the first statement in a child class constructor to call the parent class constructor |
| Method Overriding | Allows overriding of methods while still providing access to the original method via super |
| Resolves Naming Conflict | Avoids confusion by specifying whether the reference is to the parent class or the current class |
Additional Details
Limitations of super
superonly refers to an immediate parent class, not an ancestor further up the inheritance chain.- It cannot be used in static methods as it requires an instance of the class to function.
super vs this
thisrefers to the current class instance and can be used to differentiate between instance variables and parameters.superrefers to the parent class instance and is pivotal in executing parent class methods and constructors.
By understanding the implementation and nuances of super in Java, developers can effectively manage class hierarchies and reduce redundancy, streamlining their object-oriented programming practices.
Related reading
- super raises TypeError must be type, not classobj for new-style class
- Swift class introspection generics
- Swift enum inheritance
- Testing if object is of generic type in C
- Support Vector Machine for Java?
- Swagger with Spring Boot 2.0 leads to 404 error page
- The dependencies of some of the beans in the application context form a cycle
- Thread Safe singleton class

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.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.