• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

clarification in overriding/shadowing reqd.

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You all for clearing my doubt.. I got it..
Baskaran.
 
reply
    Bookmark Topic Watch Topic
  • New Topic