File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Error in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in Java" Watch "Error in Java" New topic

Error in Java

Sankar Vaiyapuri

Joined: Oct 10, 2011
Posts: 3

John Jai

Joined: May 31, 2011
Posts: 1776
Objects of the subclass can be assigned to the reference of the super class. Indeed that is a way of polymorphism in Java.

Super class references are eligible only to call the overridden methods that are present in the subclass. So when you try to call the output2() method defined in the subclass the compiler shows you the error. Because output2() does not overrides any super class method. You are just implementing it from the interface Interface

And please read HowToAskQuestionsOnJavaRanch
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Hi Sankar. Welcome to the Ranch!

Sankar Vaiyapuri wrote:The Compiler must show error when the Object of SubClass is assigned to the Object of SuperClass.
Because in real time programs the memory needed for the Object of SubClass is always higher than the memory needed for
the Object of SuperClass.

You've got a misunderstanding there of how variables work in Java. Variables aren't a slot that needs to be big enough to hold the entire object. They're just a reference to an object that lives on the heap. And references are all the same size regardless of what they're pointing at. So there's no "physical" problem in assigning an object of a subclass to a superclass reference.
Sankar Vaiyapuri

Joined: Oct 10, 2011
Posts: 3

I agree. Here's the link:
subject: Error in Java
It's not a secret anymore!