When can you use super keyword?
Question Explanation
The super keyword is a crucial concept in object-oriented programming, particularly in languages like Java, C#, and Python. It allows a subclass to access methods and properties of its parent class. Interviewers often ask this question to gauge a candidate's understanding of inheritance, polymorphism, and class hierarchies. By using the super keyword, developers can avoid code duplication and promote reusability in their applications. Understanding when and how to use super is essential for building efficient, maintainable software. Common misconceptions include believing that super can only be used for constructors, rather than also for accessing methods and properties. Familiarity with this keyword not only demonstrates technical prowess but also reveals an understanding of fundamental programming principles that are applicable across many languages and frameworks. This knowledge is vital in real-world scenarios where code optimization and design patterns play a significant role in software architecture.
Sample Answers
Example 1: Accessing Parent Class Methods
In a scenario where you need to extend the functionality of a method from the parent class, the super keyword comes into play. For instance, consider a base class Animal with a method makeSound(). If you have a subclass Dog that overrides this method but still wants to include the base functionality, you can do it like this:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
void makeSound() {
super.makeSound(); // Calls the parent class method
System.out.println("Bark");
}
}
In this example, the Dog class first calls makeSound() from Animal, and then adds its own implementation. This ensures that the behavior from the parent class is preserved while extending it. Such design helps in maintaining a clean and organized codebase, especially as the project grows.
Example 2: Calling Parent Class Constructor
Using the super keyword is also essential when dealing with constructors in inheritance. If a subclass has its own constructor, it can invoke the parent class constructor to ensure that the base class is properly initialized. For example:
class Vehicle {
Vehicle(String type) {
System.out.println("Vehicle type: " + type);
}
}
class Car extends Vehicle {
Car() {
super("Car"); // Calls the parent class constructor
}
}
Here, the Car constructor uses super to invoke the Vehicle constructor, passing in a string. This is crucial for initializing attributes defined in the parent class, promoting proper encapsulation and ensuring that the base class is set up correctly before the subclass adds its specific features.
Example 3: Avoiding Ambiguity in Method Resolution
In complex class hierarchies, the super keyword can help resolve method ambiguity. Consider a situation where a subclass overrides a method from two different parent classes. Using super allows you to specify which parent class's method to call. For example:
class A {
void show() {
System.out.println("Class A");
}
}
class B {
void show() {
System.out.println("Class B");
}
}
class C extends A, B {
void show() {
super.show(); // Calls the method from class A
}
}
In this example, C can specify which show() method to invoke. This capability is particularly useful in languages that support multiple inheritance, ensuring clarity in method resolution and preventing potential conflicts.
Keywords
Ready to practice more questions?
Explore our collection of technical interview questions from top companies.
View All Questions