• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error in Java

 
Greenhorn
Posts: 3
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
reply
    Bookmark Topic Watch Topic
  • New Topic