Re: How "Super" is implemented by Java?
- From: anup <anup.kalbalia@xxxxxxxxx>
- Date: Mon, 13 Oct 2008 11:54:06 -0700 (PDT)
On Oct 13, 10:00 pm, Joshua Cranmer <Pidgeo...@xxxxxxxxxxxxxxx> wrote:
anup wrote:
Hi,
Can anybody point to some resource which explains how a call to the
"super" is resolved/implemented in java (or for that matter any OO
language?
The short way to explain this is in the difference between virtual
method calls and nonvirtual method calls. A virtual method call finds
the method that it actually invokes starting from the actual class
instance and working up. Nonvirtual method calls will only look at calls
defined in the class it is called upon (or higher).
In Java, all methods except private methods and constructors are
generally called virtually (this is not the case in, say, C++, where
virtual methods have to be explicitly noted as such). Calls using super
use a nonvirtual method call.
I am a bit confused as to when an object of any subclass is
instantiated does an object of the super class also get created? The
answer is probably NO as only one object gets created in the memory
and the fact is documented almost in every inheritance tutorial.
I'm going to step back and assume single inheritance here because
multiple inheritance breaks some basic assumptions and identities in
non-trivial ways.
Suppose we have an instance of type Object. Object has its data fields,
etc., laid out in memory in some fashion:
+---------------+
| Object fields |
+---------------+
Now we have an instance of type String, which extends Object. It too has
its own data fields, which need memory to lay out.
+---------------+
| String fields |
+---------------+
But, since it extends Object, it needs the fields from Object somewhere
in the list somewhere. Things work out if we put these fields at the
beginning.
+-----------------+
| STRING OBJECT: |
|+---------------+|
|| Object fields ||
|+---------------+|
|| String fields ||
|+---------------+|
+-----------------+
A pointer to the object will generally point to offset 0, or to the
Object fields. If we don't know of the existance of the String fields,
we have a reference to only an Object object at the same place. A String
object is therefore nothing more than an Object object with a little
more data.
In answer to your question, instantiating a String instance will also
instantiate an Object instance--the same instance, in fact. Only one
object is being created, true, but the same object is being
instantiated, first as an Object, then as a String.
But then the point is - how does "super" work, which is supposed to
act as a reference to an object of the parent class? Is there any
demarcation in the memory for the members (data/methods) of the parent
class?
An object of the subclass is merely an object of the parent class with
some extra stuff tacked in on the end. The location of this data is
almost always (assuming single inheritance) at the beginning of the
object to avoid having to fiddle with offsets.
Note that I've been talking about fields; methods are a different story.
I know how g++ does virtual methods, but I haven't looked into how JVM
does the same thing.
Any resource on the control flow during memory allocation while
instantiating an inherited object (indicating both the function as
well as data) shall be great.
The JLS includes a section on how object initialization must occur in
terms of what must be done when:
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.htm...>.
The JVM discusses this topic from a much lower level, but is more
explicit on (say) which method to be invoked. I can't point to any one
spot in this reference, so here's the ToC:
<http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc...>.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Thanks Silvio and Joshua for your answers.
Silvio, what you say does not answer my question of how a call to
"super" is implemented by the JVM. The notion of virtual methods is
well understood and there is no confusion there. Still your effort is
very much appreciated.
How "super" knows that it has to fetch the "hidden/overridden" members
from the parent class was point in question. Does the JVM hardcode
this fetching when it sees the "super" keyword? Just thinking out
loud.
Anyways, Joshua I think your answer is leading me to the solution. I
believe I should go through the links and dig down the VM specs to get
the answer. Thanks a ton for the reply.
.
- Follow-Ups:
- Re: How "Super" is implemented by Java?
- From: Mike Schilling
- Re: How "Super" is implemented by Java?
- From: Mark Space
- Re: How "Super" is implemented by Java?
- References:
- How "Super" is implemented by Java?
- From: anup
- Re: How "Super" is implemented by Java?
- From: Joshua Cranmer
- How "Super" is implemented by Java?
- Prev by Date: Re: Using POI to create XLS from a C app
- Next by Date: JFrame shrinks when given its own bounds.
- Previous by thread: Re: How "Super" is implemented by Java?
- Next by thread: Re: How "Super" is implemented by Java?
- Index(es):
Relevant Pages
|