Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in Java

 
Sankar Vaiyapuri
Greenhorn
Posts: 3
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic