What will be printed when this is executed? Explain what happens in each section
ID: 3689599 • Letter: W
Question
What will be printed when this is executed? Explain what happens in each section marked with "//Explain this line".
class Parent {
void overload (Parent p) {
System.out.println("Parent");
}
} // end Parent
public class Child extends Parent {
void overload (Child c) {
System.out.println("Child overload");
}
void overload (Parent p) {
System.out.println("Child override");
}
void testOverload (){
Parent p = new Child();
Child c = new Child();
overload(p); //Explain this line
overload(c); //Explain this line
p.overload(p); //Explain this line
c.overload(c); //Explain this line
p.overload(c); //Explain this line
c.overload(p); //Explain this line
} // end testOverload
public static void main(String[] args) {
Child c = new Child();
c.testOverload();
} // end main
} // end Child
Explanation / Answer
overload(p); here overload is called with object of parent so from Child class overload(Parent p) will be called
overload(c); here overload is called with object of child so from Child class overload(Child c) will be called
p.overload(p); here overload is called by using object of Parent with object of parent but since it is overloaded function then the function from Child will be called overload(Parent p)
c.overload(c); here the object of class Child is used to call overload function with its own object as parament so overload(Child c) it is called
p.overload(c); here overload is called by using object of Parent with object of Child as parameter but since it is overloaded function then the function from Child will be called overload(Parent p)
c.overload(p); here overload is called by using object of Child with object of parent as parameter but since it is overloaded function then the function from Child will be called overload(Parent p)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.