File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clarification in overriding/shadowing reqd. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clarification in overriding/shadowing reqd. " Watch "clarification in overriding/shadowing reqd. " New topic
Author

clarification in overriding/shadowing reqd.

Baskaran Subramani
Ranch Hand

Joined: Jul 30, 2000
Posts: 38
Hi,
I found this in one of the mock exams.
class car{
int gearratio = 9;
String method(){return "from car class";}
}
class sportscar extends car{
int gearratio = 7;
String method(){return "from sportscar class";}
public static void main(String[] args){
car a = new sportscar();
System.out.println("gr = " + a.gearratio + " " + a.method());
}
}
the output is , gr = 9 from sportscar class
the explanation given was,the variables are shadowed and methods are overridden. this means that the variable to be selected depends on the declared class of the variable.method to be selected depends on the actual object class the variable is refering to.
in this case, it is fine. But if I change, the method name from method() to method1() in car, then it is failing to compile saying that the method() is not found in car. here is the problem. when a method is overridden, and it is called from an object which refers to the overiding object, then, will it first check in the overridden class or overidding class? if it first checks in overriding class, it should not fail in compilation..

Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
This has nothing to do with overiding/shadowing.
What you are trying to do is simply illegal. When you change the name of method from method() to method1() in class Car, you cannot call method() on a variable of class Car. Because the method method() does not exist in class Car anymore!
As the compiler works at compile time it does not care what object is actually refered to by the reference ( that's the job of Java interpreter and not compiler). So at compile time the compiler doesn't see the method method() in Car class and it generates an error mesage.
It is same as doing :
Object obj = new Vector();
obj.addElement("asdf"); // !!
HTH,
Paul

------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!


<A HREF="http://pages.about.com/jqplus" TARGET=_blank rel="nofollow">http://pages.about.com/jqplus</A> <BR>Get Certified, Guaranteed!
Ada Wang
Greenhorn

Joined: Aug 28, 2000
Posts: 23
I'm confused. If you changed method() to method1() in the car class, the method() in the sportCar class didn't override any method in its parent class. The method1() in the car class and method() in the sportCar class are two independent methods. How come is it fail to compile?
Ada
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
Because the variable 'a' is of class 'Car'. Now, when you try to call method() on a, the compiler does not care that 'a' actually refers to an object of class 'SportsCar' which has the required method. It blindly tries to search the method in Car class and fails to find it.
-Paul
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
Baskaran Subramani
Ranch Hand

Joined: Jul 30, 2000
Posts: 38
Hi Paul,
during compile time, if it doesn't care about the reference object, rather it only concentrates on the type of object, does overriding known only at run time?

[This message has been edited by Baskaran Subramani (edited August 28, 2000).]
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
Inheritance is known at Compile time but Polymorphism is taken care of only at run time.
Consider this:
Vector v = new Vector();
v.addElement("adsf"); //1
v.wait(); //2
Line //2 is valid because the compiler knows that the class of v is Vector and as Vector extends Object, it finds the wait() method.
Object v = new Vector();
v.addElement("adsf"); //1
v.wait(); //2
v.toString(); //3
Here, //1 won't compile. 'v' declared to be of class Object. Object does not inherit properties of Vector.
//3 compiles because this is polymorphism. Object also has toString() and Vector also has it. But at runtime, the fact that v actually refers to Vector is noted and it's (Vector's) toString() is called.
HTH,
Paul.

------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there,
The thumb rule which is helpful in such cases is this:
If u have something like car a = new sportscar(); followed by a.method(); then u must have a method called method() in both the classes car and sportscar. During compilation the Javac checks whether u have a method called method() inside car or not.
On the other hand if u just say car a = new sportscar(); and leave it w/o calling a.method() then there wont be any compilation error.
Hope u get it. U can try a few combinations like this urselves.
Bye,
Ganesh
manal
Ranch Hand

Joined: Aug 17, 2000
Posts: 45
hi frnd ...
the simple answer is ..
at compile time the compiler check weather the method called is present of not with the class of reference .....
if the method is overrridden then the method called is of subclass .. this is known is dynamic binding ...
now as u have changed the name of method in superclass ..u cant call method frm its reference ...
DYNAMIC BINDING IS ONLY APPLIED IN CASE OF OVERRIDDEN METHODS AT RUNTIME AND WHEN THE COMPILER GIVES A CLEAN CHIT 2 THE PROGRAM ....
thanks ....
manal ...
Baskaran Subramani
Ranch Hand

Joined: Jul 30, 2000
Posts: 38
Thank You all for clearing my doubt.. I got it..
Baskaran.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clarification in overriding/shadowing reqd.