• 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

subclass sub = new superclass() ??

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This maybe a very basic concept, but I am afraid I fail to understand this. Please help me clear the following query:
Why cannot we assign an object of Superclass to a subclass? Shouldn't this work as subclass inherits all attributes of a superclass?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Super { int x; }
class Sub { int y; }

// This isn't allowed, but let's pretend
Sub sub = new Super();

// This is why it's not allowed: we're trying to access the field y
// of an instance of the class "super". This would compile, but fail
// when the program ran.
sub.y = 3;

// But on the other hand, this is legal:
Super sup = new Sub();

// And this is fine because a Sub has an x, since it's
// a subclass of Super.
sup.x = 3;
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you meant to say that
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jaspreet atwal:
Why cannot we assign an object of Superclass to a subclass? Shouldn't this work as subclass inherits all attributes of a superclass?


There is an "is a" relationship between subclasses and superclasses. An instance of a subclass is an instance of the superclass. That is why you can assign an instance of a subclass to a variable of the superclass type - not the other way around!

[ November 06, 2007: Message edited by: Jesper Young ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jan van Mansum:
I suppose you meant to say that



Yes, thank you.
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your replies! I understand a lot better now.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic