Exercises · Q4
Q.What is method overriding? Explain the role of the super keyword, giving a short example.
ChseodishaTextbookSubjectiveImportance★★★★★est
29% · 4/14 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Method overriding. When a child class defines a method with the same name and same parameters as a method in its parent, the child's version replaces the parent's for objects of the child class. This is called overriding, and it lets each child behave differently while sharing a common method name — the basis of polymorphism.
The super keyword. Sometimes the child wants to add to the parent's behaviour rather than throw it away. The keyword super refers to the parent class, so super.methodName() runs the parent's original version from inside the child.
Example:
class Person {
void introduce() {
System.out.println("I am a person");
}
}
class Student extends Person {
void introduce() { // overrides the parent method
super.introduce(); // first run the parent's version
System.out.println("...and a student");
}
}
public class Demo {
public static void main(String[] args) {
new Student().introduce();
// Output:
// I am a person
// ...and a student
}
}
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.