Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Object Creation in inheritance

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a subclass named Orange which extends Fruit class.. Now when I make an object of Orange,it will call the constructor of superclass fruit and then up till Object Class..

Does this mean that actually Objects are created for Super classes when i make an object for my subclass??
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to solve the confusion up to some extend :

When Orange extends Fruit, It inherits some properties of Fruit class, when you compile Orange we need to determine which properties you inherits, generally these properties include Instance Variable,methods....and we know that Instance properties will be defined only after call to super constructor..
so subclass instance properties will be defined only after super() {fruit}, superclass instance properties will be defined only after super() {Object}..if we don't invoke super...we wont be able to access superclass properties..so entire concept of inheritance vanishes...!!!

Please correct me if I am wrong..!!!
 
author and iconoclast
Posts: 24203
44
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
Sunny's not wrong, but I don't think s/he's answered the question.

The way to think about it: an Orange object contains a Fruit object. Every instance of a subclass has a part which is the superclass part.

Picture the base class as a cricket ball. Now if I want to make a subclass, I add to that cricket ball by wrapping it up in tinfoil. To make a sub-subclass, I can add another layer of tinfoil. Etc, etc. There's one superclass object in the middle; subclasses are formed by adding more stuff on the outside.
 
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks!
I'd like to introduce my "opinion".

You have the class Orange, right? It IS a Fruit. A Fruit IS an Object. So, Orange IS an Object.

It means that you've created an Object, a Fruit and an Orange (tipically speaking), but Object and Fruit don't go to the Heap. Orange goes, and Oragen carries some properties of Object and Fruit. It's like... When I go to work I don't bring my father and my grandfather with me, do I (ok, ok)... I just bring some PARTS of them, like the middle (or last) name, and some properties (color of the hair, eyes, size) and things like that.

I don't know why, but I think that I'm wrong

EDIT: When I say properties, I mean methods (generally public, like hashCode(), equals() from Object, and eat() or other from Fruit) and instance variables (like color, size, tasteful from Fruit).

BTW, when I was typing to above thing (the EDIT), a doubt came... Does my class inheritates(*) some instance variable from Object?

See you around folks!

BTW, I'm sorry my English
[ January 18, 2008: Message edited by: Andre Brito ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see an orange in real life. You see an orange, a fruit, and an object, but there is really only one object -- the orange.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic