• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which method it will use?

 
gzw0733
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic