wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading doubt?" Watch "Overloading doubt?" New topic
Author

Overloading doubt?

Amit Parnerkar
Greenhorn

Joined: Apr 24, 2004
Posts: 10
Can someone help me here?
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {
public static void main(String[] args) {
A c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
The output will be "A". I understand that method m1() is being overloaded here and not inherited, hence c1.m1(c2) will invoke class A's m1() method. Just wondering if I am thinking in right direction or missing something here?
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
I understand that method m1() is being overloaded here and not inherited

Amit, you're right about m1 being overloaded but it is being inherited too. Class C actually has the two methods it inherits from B and C and the method of its own.
The reason the method in A is the one invoked is because you have a reference of type A. It doesn't matter what the actual type of the object at runtime is. What you need to remember is this: when choosing among OVERLOADED methods check the type of the reference, when choosing among OVERRIDEN methods use the actual type of the object.
Cheers
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Lionel what you said is correct. But I am still confusing about how you diffentiate overloading and overriding methods.


if you think you can you r right<br />if you think you can not you r double right
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Amit, take a look at marcus Green's tutorial at jchq.net


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading doubt?
 
Similar Threads
Method question !!
overriding + overloading
Could any one of you explain me this code
inheritance question
Mock exam question