• 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

Creating an object and polymorphism

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I've read in a book: "In Java a subclass collects automatically all visible properties of the superclass, but the objects in the hierarchy EXIST INDIVIDUALLY. This means: If a subclass is istantiated, the constructor automatically calls the constructor of the superclass to instantiate the upper object."
I knew about the constructor-chaining but never thought about the consequences. This means: When creating one single object (not of the superclass Object) there are really some more objects created? If so: Could anyone post me a link or give me a hint where to find more of this subject? I haven't found anything telling me what exactly happens during runtime if a child-object is created.

Another question: whether a method should be dynamically invoked or static, this is decided by the compiler, am I seeing this right?

Thanks in advance,

Sasha.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

This is a good question. According to the section Initializing the base class from Thinking in Java...

When you create an object of the derived class, it contains within it a subobject of the base class. This subobject is the same as if you had created an object of the base class by itself. It's just that from the outside, the subobject of the base class is wrapped within the derived-class object.


We would generally say there is one object created, because the "subobjects" are inseparable parts of the derived object. After all, whether we treat a reference to this object as the base type or the derived type, that reference still points to the same object.
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case I am beginning to understand what really happens if an object inherits methods from a superclass.

Thanks for the answer and the link. Haven't heard about "Thinking in Java 3" yet. Seems to be an interesting ressource.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sasha Ruehmkorf:
...Haven't heard about "Thinking in Java 3" yet. Seems to be an interesting ressource.


The link is to the 3rd edition of Thinking in Java, which Bruce Eckel provided free online. However, starting with the 4th edition, Thinking in Java is not available as a free version.

So just be aware that the the online version is an older edition, and does not cover Java 5.0.
 
Slime does not pay. Always keep your tiny ad dry.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic