File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access overriden Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access overriden Method" Watch "Access overriden Method" New topic

Access overriden Method

Ingo Oschwitz

Joined: Apr 28, 2009
Posts: 1

hi forum,
my questions are: why is the method of the child class invoked in this example?
and is there any possibilty to invoke the method of the parent class without changing code in the child class?

thanks for help!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032

Welcome to the forum! The reason why the child class method is called is because when you do
((Parent) childReference).overriddenMethod()
polymorphism comes into effect. Everytime you access an instance method that is overridden, the version of the method which is called is the version defined in the type of the object, not the reference (and in this case the object is a child class object, so you end up calling the overriding version of the method.)

As to whether you can call the superclass version without modifying the child class code: Not when you are calling the method through a reference which is linked to a child class object. In order to call the overridden version you would need to use the super keyword in the child class code to achieve that.

I hope that helped.

All code in my posts, unless a source is explicitly mentioned, is my own.
Robin Paul

Joined: Mar 18, 2009
Posts: 13
Hi, first of all please remember that casting reference type only affects static methods and fields. Non Static instance method are not affected whether you apply any reference casting or not. Non static instance methods are called depending on the actual type of object and not reference and this thing is resolved at runtime only.
since at this statement: BobTest f = new Bob(); and that Bob extends BobTest
f is pointing to object of Bob and the method called in an instance method, so the method defined in Bob class is called.

In order to invoke the parent class method, you need to add statement super.toString(); in the sub class Bob toString() method.

Obstacles are the things we see when we take our eyes off our goals.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Just to add to what Robin said that you have to consider access levels. Casting can affect instance methods as well. For example, let's say you have a class A with a private instance method x(), and a subclass B with a public instance method x(). A.x() and B.x() are different instance methods (B.x() doesn't override A.x() because A.x() is private and thus it is not inherited by B.) However, if you have an reference of type B b, you can call b.x(). But if you cast the reference to A, then you can't call the x() method: ((A) b).x().
I agree. Here's the link:
subject: Access overriden Method
It's not a secret anymore!