Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overiding

 
kantu Deshpande
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Parent {
private void print(){
System.out.println(" From Parent");
}
public static void main(String args[]){
Parent p1 = new Parent();
p1.print();
p1 = new Chield();
p1.print();
}
}
class Chield extends Parent{
public void print(){
System.out.println(" From chield");
}
}
Output will be:
A. Compile error can't override the private method print()
B. From Parent
From chield
C. From Parent
From Parent
D. From chield
From chield
Given ans : C
If you instantiate two objects with same name as we did in this (p1) what happnes to the first reference object?..Please let me know..
Thanks,
Kantu..

 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kanthu,
It becomes eligible for garbage collection. We lose reference to the object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic