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 Mr sona nagee give me explain about the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mr sona nagee give me explain about the code" Watch "Mr sona nagee give me explain about the code" New topic
Author

Mr sona nagee give me explain about the code

gzw0733
Greenhorn

Joined: May 28, 2001
Posts: 8
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

Joined: Feb 14, 2001
Posts: 234
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


sona<br />SCJP
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
gzw0733 please re-register with a valid first name and last name according to our naming regualtions. http://www.javaranch.com/name.jsp


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
 
 
subject: Mr sona nagee give me explain about the code