File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Initialisation Workbencch - Difficult Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Initialisation Workbencch - Difficult Question" Watch "Initialisation Workbencch - Difficult Question" New topic
Author

Initialisation Workbencch - Difficult Question

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192

C:\Java\EigeneJavaProgramme>java Q028a
A(): i= 1
B(): i= 2
Q028a(): i= 2
A(): i= 3
Q28a:get()
7
A nlyA()

In Q028a class Q028a overrides the get() method of class B.
At first a Q028a instance is created which results in the call of the superclass
Constructors A() and B(). Then i is incremented from 0 to 2.
Then an A instance is created and „A(): i= 3“ is printed.
Afterwards the Q028a instance is transformed to an A instance. It still retains
The object type of Q028 but gets A as Referencetype.
Therefore it calls Q28a.get() method because it is overridden and increments
Variable i to 7. The Object type (Q028a) is responsible for overridden methods
So it calls the get() method of class Q028a.
Then it invokes onlyA(). This method is not overridden in Q028a so the Reference-
Type counts for the method invocation and „onlyA()“ from class A is invoked.

Now consider this case Q028:
Here Q028 has no own get() method.
MY QUESTION IS:
 WHY does it invoke the get() method of superclass B and not the get() method
Of A because obj is of Reference type A when it invokes obj.get()???

C:\Java\EigeneJavaProgramme>java Q028
A(): i= 1
B(): i= 2
Q028(): i= 2
A(): i= 3
B:get()
6
AnlyA()
(edited by Cindy to disable smilies)
[ November 21, 2002: Message edited by: Cindy Glass ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi thomas,
here i try to explain what i understand in this inheritance example....the important thing is "we can't just look at text of the code to guess what should happen...". if we try to put this code in terms of how 'computer' implements it then we would more clear about what will happen and the reason for it...
for these kind of problem i still see the C++ Virtual Tables for the virtual functions the best way to understand which gets created when we have inheritance...Java seems not different...
alright, enough of philosophy....now let me get to the point i want to explain...
when class B is defined following might be the code,

here we assume we only had class B (as we inclued compatibility information into the class)...
now, same happens for Q (i will just name it Q for simplicity) as well.
when Q is defined we can assume following code is written to class Q forgetting about class B and A...

here in above code we have considered class Q to have get() method. in this case- in Q's definition ONLY ONE get() method is known which is Q's get() method and it has the compatibility information for A and B classes so that a valid casting can call this method...
so....this clears the first example you gave where Q's get() is called...
now, if we remove get() from Q then we will have following line in Q's definition for get() method,

so...in your second example class B's get() method gets called even if the declared object is of class A and we have a valid cast...
inheritance i believe doesn't exist at all u can say once we have this compatibility information embedded in the class and all the parent's corresponding code gets sort of copied to child definition...this argument can surely be counter attacked regarding private methods in Class A (and even in Class B) that are called by Class A's onlyA() method and gets called when we call onlyA() on Q's object BUT we can accomodate that scenario as well using this "compatibility" information embedded in class definitions...
e.g. we can have some private method in A which is getting called by A's onlyA() method and in class Q's definition we can have...

here we can't call q.somePrivateMethodOfA() where q is the Q's object but we can call q.onlyA() which is in turn calling A's somePrivateMethodOfA()...
did make any sense???
if not i 'm sorry....i'm poor at explaining things :-( but i tried my best...
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initialisation Workbencch - Difficult Question