File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading n overriding

 
devender negi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12023
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramesh Meher
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is simple case of Overloading, scratch a little bit of your mind, you will get the answer.
 
Tusshar Fasate
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output will be
ABC
 
devender negi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
i got this question from my institute paper.n idnot know from where they got it
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic