This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 19649
    
  18

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calling parent class method using child class object
 
Similar Threads
Overloading methods.
Inheritance of static methods
Overriding
Static binding Vs Dynamic Binding
Overriding Vs Shadowing