File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " overloading n overriding " Watch " overloading n overriding " New topic

overloading n overriding

devender negi

Joined: May 16, 2008
Posts: 5
How we decide overloading and overriding with object

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.

Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

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

Joined: Oct 02, 2003
Posts: 11923

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

Joined: Jan 19, 2008
Posts: 7
Consider reading this recent post.

That might help
Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30

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.
Deepak Chopra
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
devender negi

Joined: May 16, 2008
Posts: 5
i got this question from my institute paper.n idnot know from where they got it
I agree. Here's the link:
subject: overloading n overriding
It's not a secret anymore!