• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Mr sona nagee give me explain about the code

 
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);
}
}

 
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
 
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
 
Don't listen to Steve. Just read this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic