Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mr sona nagee give me explain about the code

 
gzw0733
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sona nagee !though I agree with .
But give a explaination about the code bellowed which from Axel Janssen (you can find the code of his in gzw0733's "which method will be use?")
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);
}
}

 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all i am mrs
class vehicle{
public void drive(){
System.out.println("vehicle:drive");
}
}

class car extends vehicle{
public void drive(){
System.out.println(" car:drive");
}
}
public class test10{
public static void main(String args[]){
vehicle v;
car c;
// methods are picked according to the object type and variables are picked according to the reference type
// reference is vehicle type and object is also vehicle type
v=new vehicle();
// refernce is car type and object is also car type
c=new car();
// drive() of vehicle is pickeup up as the object type is vehicle
v.drive(); // vehicle:drive
// drive() of car is picked up as the object type is car
c.drive(); // car: drive
// v is a reference of vehicle type which is now made to refer to an object type of car. remember the object type here is car
v=c;
// methods of object type are picked up so drive() of car is picked up
v.drive(); // car:drive
}
}
hope it is clear now
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gzw0733 please re-register with a valid first name and last name according to our naming regualtions. http://www.javaranch.com/name.jsp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic