Bird
0
0

Given these classes, how can you modify the Child class to call both its own display() method and the Parent class's display() method inside its showBoth() method?

hard📝 Application Q8 of 15
Java - Inheritance

Given these classes, how can you modify the Child class to call both its own display() method and the Parent class's display() method inside its showBoth() method?

class Parent {
    void display() {
        System.out.println("Parent display");
    }
}
class Child extends Parent {
    void display() {
        System.out.println("Child display");
    }
    void showBoth() {
        // What code goes here?
    }
}
Adisplay(); super.display();
Bthis.display(); super.display();
CAll of the above
Dsuper.display(); display();
Step-by-Step Solution
Solution:
  1. Step 1: Understand calling methods in child class

    Calling display() calls child's method; super.display() calls parent's method.
  2. Step 2: Verify the code snippets

    Any combination of calling display() (or this.display()) and super.display(), regardless of order, correctly calls both methods.
  3. Final Answer:

    All of the above -> Option C
  4. Quick Check:

    Calling both methods = use display() and super.display() [OK]
Quick Trick: Use super.method() to call parent, method() for child [OK]
Common Mistakes:
  • Trying to call parent method without 'super'
  • Confusing order of calls
  • Thinking only one way works

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes