aspose file tools*
The moose likes Beginning Java and the fly likes Just want to confirm if upcasting a Sub Class to a Super Class not allowed in Inheritance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just want to confirm if upcasting a Sub Class to a Super Class not allowed in Inheritance?" Watch "Just want to confirm if upcasting a Sub Class to a Super Class not allowed in Inheritance?" New topic
Author

Just want to confirm if upcasting a Sub Class to a Super Class not allowed in Inheritance?

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172




When i run the above code AnimalTest.java I get a class cast exception error. I just need to confirm with you kind folks that is/are my following inferences correct ?
Inference 1: In the try block horse subclass reference variable h is trying to up-cast itself to Animal Super Class so that it could print "Generic Animal eating" when invoked with the eat method as h.eat();
Inference 2: Since Java does not support up casting therefore it throws the ClassCastException error.
Inference 3: So all this boils down to Java allows inheritance to sub classes but gets mad if you try to access its "super" powers.

Sincerely awaiting your reply.


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Ashish Dutt wrote: . . . .
When i run the above code AnimalTest.java I get a class cast exception error. I just need to confirm with you kind folks that is/are my following inferences correct ?
None of the following is correct.

Inference 1: In the try block horse subclass reference variable h is trying to up-cast itself to Animal Super Class so that it could print "Generic Animal eating" when invoked with the eat method as h.eat();
Inference 2: Since Java does not support up casting therefore it throws the ClassCastException error.
Inference 3: So all this boils down to Java allows inheritance to sub classes but gets mad if you try to access its "super" powers.

Sincerely awaiting your reply.
Don’t go on about super powers. Remember you are on the beginners’s forum and somebody will believe you

You are not casting up anywhere. You can always cast something up to its superclass, but there is hardly ever any benefit to be gained from doing that. What you are doing is telling the compiler, “this is a Horse”, and the compiler is (in this instance) very trusting and will believe you. Or maybe you are promising always to provide a Horse at that point. When you come to execute the cast, the JVM complains it has been lied to (or the promise was broken; the object in question is not a Horse), and if the JVM has a complaint, it always responds with an Exception. In this case a ClassCastException.

What you are doing is casting down towards the subclass. Something you should avoid as much as possible.because it is very error‑prone. You had designed your inheritance so as to obviate any need for such a cast, and still carried it out regardless.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

In addition to what Campbell said, you seem to have a couple other misconceptions about casting that need to be cleared up.

1. Casting does not turn an object of one class into an object of another class. Casting does not affect objects at all. The only thing casting does (when casting a reference, as opposed to a primitive), is to tell the compiler and the runtime, "I know you think this is a reference of type X, but treat it as a reference of type Y." At runtime, as Campbell points out, if the object pointed to by that reference is not in fact an instance of Y or a subtype of Y, then the JVM throws a ClassCastException. And at compile time, if the compiler knows that an X reference can never point point to a Y, then it's a compile-time error.

2. Upcasting (to an ancestor class) does not let you bypass polymorphism and call the parent's version of the method. There's no way to do that. If the actual object that exists at runtime is of type ChildClass, and ChildClass has its own implementation of the method being called, there's no way for the caller to make it call ParentClass's version instead. The child's version may choose to invoke the parent's version itself, but you can't force it to from outside.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

A big thank you to both Jeff and Campbell.
I m glad that i joined this forum because you guys go an extra mile to provide detailed explanations supplemented with rich examples.
Cheers. :-)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome. Glad we could help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Jeff Verdegan wrote:You're welcome. Glad we could help.
Agree
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just want to confirm if upcasting a Sub Class to a Super Class not allowed in Inheritance?