This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  overloading n overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " overloading n overriding " Watch " overloading n overriding " New topic
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: 6661
    
    5

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 - More SCJP Mocks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11240
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overloading n overriding