If I have a reference to an instance of a sub-class, how can I directly invoke a method of a parent class which has been overridden?

John Mitchell

Well, first off, let me say that this is probably a very bad idea. I mean you are actually using the sub-class and presumably the person who implemented it did actually have some idea about what they were doing.

Now, that said, you cannot just cast your reference to the sub-class to the parent class's type and invoke the method against that since instance methods are always invoked against the actual type of the object.

Anyways, it's a moot question because what you are trying to do is a violation of the type safety of the language. Java isn't C++ so it doesn't allow you to do that sort of thing. :-)

This case is specifically mentioned in section 8.4.6 of the Java Language Specification.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact