Meaningless Drivel is fun!*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in Java" Watch "Error in Java" New topic
Author

Error in Java

Sankar Vaiyapuri
Greenhorn

Joined: Oct 10, 2011
Posts: 3

John Jai
Bartender

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
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

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
Greenhorn

Joined: Oct 10, 2011
Posts: 3

thanks
 
wood burning stoves
 
subject: Error in Java