• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Output

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
Y the output in the below program is ABC and not CCC

Also Y the out put in the below program is -- D.printS1 D.printS2

I think both programs attacks the same concept
Pls explain in detail
[ February 22, 2004: Message edited by: Mary Cole ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mary,
The reason why ABC is printed for the first program is that a child class is assigned to the base class in the code snippet A c1 = new C(); .
The pointer currently points to the base class and not the child class C.
A base class cannot be typcasted to the child class.
Hence the appropriate method in class D is called which takes in the right argument for the base class which in this case is A.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C c = new D(); c.printS1(); c.printS2();
The short answer is because the object 'c' is of class 'C'
and class 'C' overrides the methods printS1 and printS2.
In the class D, remove the message printS2 and observe
the difference (concept of inheritance).
For your first question, the JRE attempts to resolve the
message based on the class of the object that it was called
and matching the most appropriate input arguments.
Thus the results that you observed.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pointer currently points
pointer...huh?
Terminology Amit, terminology.
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx guys for ur explaination...It was nice explaination
But am confused in this code....


What I think is that ans should be CCC , but its ABC.
More expalination pls
[ February 23, 2004: Message edited by: Mary Cole ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
when you say a = b = c = new C(); you're actually creating
a reference variable 'a' to A but of type object C
a reference variable 'b' to B but of type object C
a reference variable 'c' to C and of type object C
each of the objects A B C have string s1, B shadows the one inherited from A and C shadows it (i dont know if i can say if it inherited frm B or frm A)
now m1() is an overloaded method in C.
what i suggest you kinda make a mnemonic is
if Overriden method - use the Object
if Overloaded method - use the Reference
now, m1() being an overloaded method, when passed a, b, c respectively, it's clear to the compiler at compile time that the method parameter is of object type A when a is passed and B when b is passed. so the corresponding methods are invoked and m1(a); m1(b); m1(c) sequentially calls to print out ABC
hope this helps.
check out Kathy's book, chapter 5 pages 18 thru 20. it has a very clear explanation of what's goin on.
hope this helps.
if this has not cleared things up, plz do post back and we can discuss it further.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing to add:

Overriding never takes place with static methods!

BTW, excellent examples.

B.
[ February 24, 2004: Message edited by: Bojan Knezovic ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic