wood burning stoves*
The moose likes Beginning Java and the fly likes Object Creation in inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Creation in inheritance" Watch "Object Creation in inheritance" New topic
Author

Object Creation in inheritance

sarathchandra chandala
Greenhorn

Joined: Sep 06, 2007
Posts: 9
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??
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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..!!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

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 ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

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.
 
Consider Paul's rocket mass heater.
 
subject: Object Creation in inheritance
 
Similar Threads
Generic class declarations
Objects
Object Reference Conversions
how to typecast arrays of Objects into array of specialized objects
Why declare an object and instantiate it as instance of inherited type?