Java MCQ: Why is the ‘super’ keyword used in Java?
Answer:
Explanation:
The super
keyword in Java is used to refer to the immediate parent class object. It is often used to access fields, methods, and constructors of the parent class. This allows a subclass to call a method or constructor from its superclass, ensuring that the behavior defined in the superclass is preserved or extended in the subclass.
For example, super()
is used to invoke the parent class’s constructor, and super.methodName()
is used to call a method from the parent class. This is particularly useful when a method in the subclass overrides a method in the superclass, and the subclass still needs to access the overridden method.
Understanding the use of super
is crucial for effectively managing inheritance and ensuring that the intended behavior of classes is maintained when extending or modifying them.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html