• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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.



0
gudii9
Asked:
gudii9
  • 3
  • 3
1 Solution
 
theKashyapCommented:
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).
"
0
 
gudii9Author Commented:
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

0
 
theKashyapCommented:
>>>   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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gudii9Author Commented:
>>>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
0
 
theKashyapCommented:
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
No it's exactly the opposite. You can invoke a method declared in the base/parent type but NOT in derived/subclass type.
0
 
gudii9Author Commented:
>>>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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now