Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OverLoading && Overrriding confused with output

 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,



i am Ok with output displayed for c1.dep() (overridden) ,
Could any one please explain me how exactly we get output in as 'A' and 'C' in line 1 and line 2 .
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gowtham!!





I hope this helps you to atleast some extent..............
[ August 20, 2007: Message edited by: dhwani mathur ]
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mathur
Thanks for your reply .
see the below code .


i was bit even more confused .... but upto some extend i will agree with you .
see the output for the this code .i added one more method in class Az.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The revised version doesn't do what you say it does: -



The original version does do what you say: -



In the original, there is no overriding of any of the m1(something) methods. The three classes provide three different overloads of it.

The variable c1 is declared to contain an Az object. When choosing between overloads, it is the declared type that determines what happens. There is only one method in Az that matches c1.m1(c2); it's the one that prints "A".

In the modified version, the class Az has a method m1(Cz) that is overridden in class Cz. When choosing between overrides, it is the run-time type that matters. So, because c1's run-time type is Cz, the method in Cz is called, printing "C".
[ August 20, 2007: Message edited by: Peter Chase ]
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter ,
Thanks for replying .
Here c1 has object which refer;s to "Cz" class.
what you had mentioned in repy "The variable c1 is declared to contain an Az object" which comfuses me .

Please let me know if am wrong in understanding the concept.?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic