Thursday, July 08, 2004

Re: Unified Type System

My immediate thoughts turned to collections... Having a requirement that every class in the system implement a common interface (Object's) makes it possible for collections to be generic enough to be used with anyone.

I was a bit confused about what you are trying to say? Do you mean simple Casting? How is C++ better at handling collections/generics w/o unified type system?

Personally I do not think that Collections were a reason for Object. Object was an initial design consideration, but Collections came in at a later stage. Casting though is a very important factor.

To notice the advantages of Object look at the class itself. Object guarantees that Thread support is provided out of the box with final classes like wait( ) notify( ). Other methods like clone( ) equals( ) getClass( ) and hashCode( ) provide features to be extended by child classes. getClass( ) is important for Reflections. hashCode( ) is important for generating hashcode indices for Collections like Map. Also finalize( ) serves as a Destructor.

Actually I have this picture of how a JVM may work. I have already given you guys lots of dumb ideas and stuff before so one more. I do not know from where I got this idea or if I have mentioned it before. Anyways...

Suppose I have 3 Classes with C extending B and B extending A.
C c = new C( );

And now only one pointer is created on the stack pointing to the new C object on the heap. Something like ..
ptr-----> ((((Object)A)B)C)
stack---> heap

so if I say (B)c;
ptr is used to read data sizeof(B) and that is used to retrieve (rather reconstruct) the instance from the heap.
if I say (A)c;
the same ptr is used to read data sizeof(A) and that is used to get the instance of type A.
And in this way Casting is done.

So I would like to emphasize that one pointer is used which initially points to type Object and this can be 'extended'. In this way the JVM and Classloader have a starting point for any Class which is seems to be a benefit.

I think I may have drifted quite a bit, but hey.. it is a pvt blog!! Now can any of you verify this? Do you know how it happens in C++?

No comments: