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()???
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