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 which method it will use? 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 "which method it will use?" Watch "which method it will use?" New topic
Author

which method it will use?

gzw0733
Greenhorn

Joined: May 28, 2001
Posts: 8
read the code bellow:select the right choice.
class vehicle{
public void drive(){
System.out.println("vehicle:drive");
}
}
class car extends vehicle{
public void dirve(){
System.out.println(" car:drive");
}
}
public class test{
public static void main(String args[]){
vehicle v;
car c;
v=new vehicle();
c=new car();
v.drive();
c.drive();
v=c;
v.drive();
}
}
given choice:
A: vehicle:dirve
car:drive
car:drive
B: vehicle:drive
car:drive
vehicle:drive
the given answer:A
In my option A is right.but saw a similar question(one of simulate mock
exam) ,in which after the "v=c v.drive" ,the compile print out
"vehicle:drive"
now I am not so sure now,which method shoud to use ,the type of c or the instance of c?
I beg your explaination sincerely..........
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi,
I am working on it.
First thing which confuses me is that you have a typo in the code.
Method in class car is dirve(). So the code as it is returns three times "vehicle:drive()" resulting from simple inheritance.
have a nice day
Axel
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi
the answer A is right
REASON : in the case of method overriding, the method with which will be invoked depends on the class of the object and not the class of the reference
hence
v=c;
v.drive();
will print: car:drive


As for the second part,
i presume the method drive() in the mocks exams would have been static.
the result of this is that the drive() in the case class is called and hence prints : vehicle:drive
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
hi,
This has to do with something which is called Dynamic Method Lookup.
For instance methods the code of the method of the actual object is executed.
So if we have a variable of type vehicle which is actually a car the method of class Car is executed.
Vehicle Chrysler = new Car();
So this would be a Vehicle variable holding a Car() object.
If you call an instance method, the code of the method in class Car would be exectuted.
BUT you have to know: There is NO Dynamic Method Lookup for static methods AND for instance variables (and static variables, too).
Just try the example. I think it is quite instructive.

class vehicle{
public String color = "vehicle-color";
public void drive(){
System.out.println("vehicle:drive");
}
public static void brake() {
System.out.println("vehicle:brake");
}
}
class car extends vehicle{
public String color = "car-color";
public void drive(){
System.out.println(" car:drive");
}
public static void brake() {
System.out.println("car:brake");
}
}
public class Test12{
public static void main(String args[]){
vehicle v;
car c;
v=new vehicle();
c=new car();
System.out.println("---- first object ------");
v.drive();
v.brake();
System.out.println(v.color);
System.out.println("---- second object ------");
c.drive();
c.brake();
System.out.println(c.color);
v=c;
System.out.println("---- variable of first object now points to second object, but is of type first object ------");
v.drive();
v.brake();
System.out.println(v.color);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which method it will use?