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

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.
 
I've never won anything before. Not even a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic