aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloaded methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloaded methods" Watch "overloaded methods" New topic
Author

overloaded methods

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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 is the result of attempting to compile and run the program?

a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error
e. Run-time error
f. None of the above

in this eg, we have three overloaded methods in class D.
My q: in case of overloaded methods, in same class or in subclasses, the method call invocation is based on the variable reference type and not the run time reference type of the object instance.

please correct me.


SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi jayashree,

Please put ur question clearly.
Let me explain what I have understood from ur question.

The a supper class object can be used to reference a sub class object, but the reverse way is not possible. But that does not mean that if u refer a super class object to a subclass object then it would call the functios of superclass. Thats why there is nothing complicated here. The subclass methods are called cooly.

Arnab
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi,
You are right.
FYI: If there are overloaded methods with base class , subclass parameters. Always the method which contains more specific subclass will be called.


In the above example there is no method for the type E, so call d1.m1(e) will execute void m1(C c) {System.out.print("C");} rather than calling the other methods. If the method void m1(C c) {System.out.print("C");} is not present it will execute void m1(B b) {System.out.print("B");}
and so on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloaded methods