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
JavaRanch » Java Forums » Java » Beginning Java
Reply locked 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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Closing as duplicate of http://www.coderanch.com/t/611160/java/java/confirm-upcasting-Class-Super-Class.

Ashish Dutt, plesae post each question only once.
 
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?