Difficulty: Easy
Correct Answer: super is used to call the immediate superclass constructor or to access overridden superclass methods and fields from a subclass
Explanation:
Introduction / Context:
Inheritance allows one Java class to extend another, reusing and specializing behavior. The super keyword is an important tool for working with inherited members. It enables a subclass to call a superclass constructor and to refer explicitly to methods or fields defined in the superclass when they need to be distinguished from overridden versions. This question asks you to identify the proper uses of super in typical object oriented Java code.
Given Data / Assumptions:
- We have at least two related classes where one extends the other.
- The superclass may define constructors, methods and fields that the subclass can reuse or override.
- We may need to chain constructors or call the superclass implementation of an overridden method.
- We focus on everyday Java usage, not unusual bytecode or reflection tricks.
Concept / Approach:
The super keyword has two main uses. First, super(...) is used as the first statement inside a constructor to invoke a specific constructor of the immediate superclass. If not written explicitly, Java inserts a call to the no argument superclass constructor. Second, super.methodName() or super.fieldName allows a subclass to access the superclass version of a method or field, which is useful when overriding and extending behavior rather than completely replacing it. Super has nothing to do with starting new JVM instances, importing classes or changing methods at runtime.
Step-by-Step Solution:
Step 1: Recall that constructors in a subclass often call a superclass constructor using super(...).
Step 2: Remember that super must appear as the first statement in a constructor when used for constructor chaining.
Step 3: Recognize that inside an overridden method, you can call super.methodName() to reuse the parent implementation and then add extra logic.
Step 4: Note that super.fieldName refers to a field defined in the superclass, which can be helpful when a field is shadowed.
Step 5: Choose the option that mentions these constructor and member access roles of the super keyword.
Verification / Alternative check:
Consider a subclass class Manager extends Employee. In the Manager constructor, you might write super(name, salary) to call an Employee constructor that sets common fields. Later, in an overridden method calculateBonus(), you might call super.calculateBonus() to get the base bonus and then add additional amount. Both of these are direct uses of super. There is no link to JVM startup or dynamic modification of methods, so the description in option A aligns with real usage.
Why Other Options Are Wrong:
Option B claims that super terminates the current thread and starts a new JVM, which is not true and not a function of any Java keyword.
Option C says super is used for importing classes from JAR files, which is handled by import statements and classpath configuration, not by super.
Option D suggests that super converts subclass methods into abstract methods at runtime, which has no basis in the Java language or runtime behavior.
Common Pitfalls:
A common mistake is forgetting to call the appropriate superclass constructor, leading to compilation errors when the superclass does not have a no argument constructor. Another pitfall is overusing super for field access instead of designing clearer, well encapsulated APIs. Developers should use super explicitly when they genuinely want superclass behavior, but avoid unnecessary complexity by over relying on inheritance hierarchies. Understanding super makes it easier to read subclass code and to reason about which implementation of a method will run at runtime.
Final Answer:
The correct explanation is super is used to call the immediate superclass constructor or to access overridden superclass methods and fields from a subclass.
Discussion & Comments