wood burning stoves*
The moose likes Java in General and the fly likes calling parent class method using child class object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "calling parent class method using child class object" Watch "calling parent class method using child class object" New topic
Author

calling parent class method using child class object

Yash Don
Greenhorn

Joined: May 19, 2010
Posts: 12
Hi,

In one of the interview it was asked to me that how will you call parent class version of method using child class object?
The exact Scenario is given below...

Can you please tell me how can call Parent version of m1 method using an object of Child class...


Thanks,
Yash


Regards,
Yash

SCJP, SCWCD
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

You can't. Inside class Child you can call super.m1(), but what you want isn't possible.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

here i have changed a little bit of code.....see it


here object is of type child1,and refernce is of type parent1.
but this is the case of overloaded method...in this refernce type determines which method to invoke not a object type....

i think interviewer ask a tricky question


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

but in case of overridden methods you cannot do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling parent class method using child class object