Help with object reference of superclass pointing to subclass object ! ! !
can any1 plz tell me wht exactly happens on heap , when object reference of superclass is pointing to subclass object ? From such object reference of superclass u wont be able to access methods which are specific to subclass as reference is of superclass . I m bit confused about wht exactly happens on heap which prevents object reference to prevent accessing methods of object it is pointing to ......
I can't answer your question about the internal workings of java that allow the declaration of a superclass object and the assignment of it to a subclass, but I believe that you do this specifically when you want to take advantage of polymorphism and would likely avoid it when you want to use methods or data that are specific to the subclass (corrections welcome).
I am trying now to google the mechanics of polymorphism / dynamic or late binding in java....
After looking at a few sources, and in particular wikipedia, I think that java uses virtual method tables (aka dispatch tables or vtables) for its classes in order to allow subtyping and polymorphism. You can read up on this here:
http://en.wikipedia.org/wiki/Virtual_table
again, corrections most welcome
thanx buddy fr ur reply ....
i know tht superclass reference pointing to subclass object will help in overriding at runtime where type of object is considered rather than object reference.
but i wanna know tht wht exactly happens internally on heap ?
coz ideally in other cases using object reference we can invoke methods of object it is pointing to .....
> any1, plz, wht, u, wht
Please use real words and proper spelling and grammar.
> exactly happens on heap which prevents object reference to prevent accessing
> methods of object it is pointing to ......
I'm not sure I follow what you're trying to say here.
And what is your obsession with the heap?
> thanx fr ur tht wht coz
Please stop doing that. You are on an international forum where many of the members are not native English speakers and junk like that is very confusing. (Many consider this to be rude, myself included.)
> thanx buddy fr ur reply ....
> i know tht superclass reference pointing to subclass
> object will help in overriding at runtime where type
> of object is considered rather than object
> reference.
> but i wanna know tht wht exactly happens internally
> on heap ?
Say I have something declared like so:
SuperClass myObject = new SubClass();
My believe is that myObject will use the SuperClass vtable, and this vtable will not contain any references to methods that are not found in the superclass but only found in the subclasses. If you cast, though, you'll probably get access to the cast class's vtable.
caveat: "my believe" however may be misguided. Believe it at your own risk.
hey buddy sorry if i hurt your feelings .... i am very much new to this forum that's why din't know about this ....
by the way, my question is that in normal cases lets say,
class A{
public void meth1()
}
public static void main(String [] args){
A a = new A();
a.meth1();
}
then using reference 'a' we can invoke meth1() as this object reference is pointing to an object on heap which contains meth1().
but lets say we have,
class A{
public void meth1(){};
}
class B extends A{
public void meth2(){};
}
public static void main(String [] args){
A a = new B();
a.meth1(); // is legal
a.meth2(); // is illegal as method meth2() is not visible to object ref 'a'
}
so i want to know that , To which object on heap , object reference is pointing ? and if it pointing to object of subclass B , then why it is not able to invoke method meth2() which belongs to subclass B.
So if you can tell me , please explain what exatly happens on heap when such thing happens ........
> hey buddy sorry if i hurt your feelings .... i am
> very much new to this forum that's why din't know
> about this ....
> by the way, my question is that in normal cases lets
> say,
>
> class A{
> public void meth1()
> }
> public static void main(String [] args){
> A a = new A();
> a.meth1();
> }
> then using reference 'a' we can invoke meth1() as
> this object reference is pointing to an object on
> heap which contains meth1().
>
> but lets say we have,
> class A{
> public void meth1(){};
> }
> class B extends A{
> public void meth2(){};
> }
> public static void main(String [] args){
> A a = new B();
> a.meth1(); // is legal
> a.meth2(); // is illegal as method meth2() is
> not visible to object ref 'a'
> }
>
> so i want to know that , To which object on heap ,
> object reference is pointing ? and if it pointing
> to object of subclass B , then why it is not able to
> invoke method meth2() which belongs to subclass B.
>
> So if you can tell me , please explain what exatly
> happens on heap when such thing happens ........
Okay, I see what you're asking now.
A a = new B(); // Allocates a new "B" on the heap
So then why is this illegal...
a.meth2();
? Because you declare "a" as an "A." A has no definition for "meth2.""A" has no idea what to do with a "meth2." By declaring "a" as an "A" you are limiting it to the functionality of an "A" even though you have instantiated it as a "B." Does that make sense the way I have explained it?
> So if you can tell me , please explain what exatly
> happens on heap when such thing happens ........
What about my post did you not understand? a will only be able to use A's virtual method table (as stored on "the heap"). That restricts it to only being able to use methods that A understands. Me entiendes?
> > So if you can tell me , please explain what exatly
> > happens on heap when such thing happens ........
>
> What about my post did you not understand? a will
> only be able to use A's virtual method table (as
> stored on "the heap"). That restricts it to only
> being able to use methods that A understands. Me
> entiendes?
For the love of "pete", just give him the **** dukes already.
nahhhh, don't want them.
I just want to know how much of "the heap" does he need to see to believe?
hey pete,
thanks for the reply .... i know the concept of Vtable in C++ , but i am not sure whether JAVA also uses the same concept .....
please correct me if I am wrong .....
It does
Addendum:
I stand somewhat corrected. Please look at this specification information at java.sun.com in "The Java Virtual Machine Specification", Chapter 3, Structure of the Java Virtual Machine:
http://java.sun.com/docs/books/jvms/first_edition/html/Overview.doc.html
In particular, look at section 3.7, Representation of Objects. It states:
The Java Virtual Machine does not require any
particular internal structure for objects. In Sun's
current implementation of the Java Virtual Machine, a
reference to a class instance is a pointer to a handle
that is itself a pair of pointers: one to a table
containing the methods of the object and a pointer to
the Class object that represents the type of the
object, and the other to the memory allocated from
the Java heap for the object data.
Other Java Virtual Machine implementations may
use techniques such as inline caching rather than
method table dispatch, and they may or may not use
handles.
My interpretation of this is that Sun's implementation of the JVM uses virtual method tables, but that this is not an absolute necessity for other implementations.
Message was edited by:
petes1234
