This week's book giveaway is in the Functional programming forum.
We're giving away four copies of A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles and have Ben Weidig on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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.
 
Marshal
Posts: 77957
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. Glad we could help.
 
Campbell Ritchie
Marshal
Posts: 77957
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:You're welcome. Glad we could help.

Agree
 
What? What, what, what? What what tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic