Hi Mary,
yeah the o/p for the first will be ABC and the second code will be D.printS1 and D.printS2.
first make this clear.
In the first code, u r applying concept of Overloading whereas
in the
second one u r applying Overriding Concept. In the above code method m1() at line 2 takes an object ot type A
at line 3 takes an object ot type B
at line 4 takes an object ot type C
So, when u invoke the method m1() at line 10 by passing c1 as argument whose type is A , m1(A a) will be invoked.No question of invoking m1(C c) coz the type of c1 is A , not C.
Same explanation can be applied to lines 11 and 12.
so, the o/p will be ABC , not CCC.
So, don confuse overloading concept with overriding concept.
"Overriding of methods can take place only in different classes( ie subclasses)"
coming to the second code,
Here Overriding concept should be applied coz class D extends class C. As a result of this , class D inherits the methods(non private) defined in class C and overrides them at lines 6 and 7.
at line 9 u r instantiating class D with a reference variable of superclass C.
at lines 10 and 11 when u invoke the printS1 and printS2 on c, the overridden methods in class D are invoked , rather than the methods in class C coz here
Dynamic polymorphism takes place.
i.e. when invoking methods, the runtime obj is considered but not the ref type.
in the above code since the runtime obj is of type D and the methods are overridden in class D the overridden methods are invoked.
so, the o/p will be D.printS1 and D.printS2.
suppose if the methos were not overridden then the o/p would have been
c.printS1 and c. PrintS2.
Hope this helps u.
vineela