Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

casting related example


class Car extends Object {

      void carMethod() {
      }
}

 HeavyVehicle extends Object {
}

class Ford extends Car {

      void fordMethod() {
            System.out.println("I am fordMethod defined in Class Ford");
      }
}

class Honda extends Car {

      void fordMethod() {
            System.out.println("I am fordMethod defined in Class Ford");
      }
}

public class ObjectCastingEx {

      public static void main(String[] args) {
            Car obj = new Ford();
            //    Following will result in compilation error
            //    obj.fordMethod();      //As the method fordMethod is undefined for the Car Type
            //  Following will result in compilation error
            // ((HeavyVehicle)obj).fordMethod();
                              //fordMethod is undefined in the HeavyVehicle Type
            //  Following will result in compilation error
            ((Ford) obj).fordMethod();
            //Following will compile and run
            //      Honda hondaObj = (Ford)obj;      Cannot convert as they are sibblings
      }
}      }
I was going through above example from link

http://www.javabeginner.com/learn-java/java-object-typecasting



I have not understood the output clearly.
I could not execute this program at all. how to run it and understand the output.

 Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.



Avatar of theKashyap
theKashyap

See this.
Code in your question is a very good example of
"
Another way of looking at this is using English:
casting from B to A works as long as A "is a" B. (where "is a" means inheritance or derivation, in this case A extends/implements B).
"
Avatar of gudii9

ASKER

how can i compile this code. I cannot run it to see my self.


>>>   Car obj = new Ford();

above line should compile fine right since car is up hierachy of ford.

also
 LHS variable type obj of Car class, RHS real type ford which is subclass of Car right?

            //    obj.fordMethod();      //As the method fordMethod is undefined for the Car Type
Above line also not clear to me. pleas advise

>>>   Car obj = new Ford();
above line should compile fine right since car is up hierachy of ford.
LHS variable type obj of Car class, RHS real type ford which is subclass of Car right?
Yes, it should compile and it does compile for me at least.
If it doesn't compile for you please post the errors you're getting.



//    obj.fordMethod();      //As the method fordMethod is undefined for the Car Type
Above line also not clear to me. pleas advise
Here the basic rule is, you can invoke a method on an object only if the method is declared in variable-type of that object. This is irrespective of what the "real-type" of the object is.

how can i compile this code. I cannot run it to see my self.
well the code you posted (assuming you'll uncomment all code) is wrong, so you CAN NOT (not expected to) compile and run it.
Avatar of gudii9

ASKER

>>>Here the basic rule is, you can invoke a method on an object only if the method is declared in variable-type of that object.


so we can invoke a method on an object only if the method is declared in variable-type of that object or its subclass also right but not its parent/base class right. Please advise
ASKER CERTIFIED SOLUTION
Avatar of theKashyap
theKashyap

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>>Here the basic rule is, you can invoke a method on an object only if the method is declared in variable-type of that object. This is irrespective of what the "real-type" of the object is.
>>No it's exactly the opposite. You can invoke a method declared in the base/parent type but NOT in derived/subclass type.

Then what is the use of saying like

Car c=new Ford();
c.powerSteering();
if this car just calls unimplemented car basic method it is of not much use to us right. In that case we could have instead written directly like
Car c=new Car();
cc.powerSteering();

Please advise