| Author |
overloading n overriding
|
devender negi
Greenhorn
Joined: May 16, 2008
Posts: 5
|
|
How we decide overloading and overriding with object ref..like class A {} class B extends A {} class C extends B {} class D { void m1(A a) {System.out.print("A");} void m1(B b) {System.out.print("B");} void m1(C c) {System.out.print("C");} public static void main(String[] args) { A c1 = new C(); B c2 = new C(); C c3 = new C(); D d1 = new D(); d1.m1(c1); d1.m1(c2); d1.m1(c3); }} what will be output. Thanks
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
Run it and you will get the output. Please also use code tags when you want to display code. What is it about overriding and overloading that you do not understand. Telling us more will allow us to help you better
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
|
also, please tell us WHERE this question is from. we take copywrite issues seriously here, and quoting something without attributing it is a violation of our policies.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Ramesh Meher
Greenhorn
Joined: Jan 19, 2008
Posts: 7
|
|
Consider reading this recent post. http://www.coderanch.com/t/268792/java-programmer-SCJP/certification/Overloading-vs-Overriding That might help
|
 |
Eduardo Mendes
Ranch Hand
Joined: Apr 30, 2008
Posts: 30
|
|
Hi, in class D you have three overloaded versions of m1(). When calling an overloaded method, the type of the arguments you pass dictate which overloaded version will be picked-up. In your example, even though all the three object references point to the same object type (C), the object references themselves are of types A, B and C. Therefore, versions m1(A a), m1(B b) and m1(C c) will be called, printing 'ABC'. Hope this helps.
|
 |
Sunny Jain
Ranch Hand
Joined: Jul 23, 2007
Posts: 433
|
|
|
This is simple case of Overloading, scratch a little bit of your mind, you will get the answer.
|
Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
|
 |
Tusshar Fasate
Ranch Hand
Joined: May 21, 2008
Posts: 81
|
|
output will be ABC
|
 |
devender negi
Greenhorn
Joined: May 16, 2008
Posts: 5
|
|
Thanks. i got this question from my institute paper.n idnot know from where they got it
|
 |
 |
|
|
subject: overloading n overriding
|
|
|