Java - Overloading and Overriding
01 class SubClass extends SuperClass {}
02 class AppSuperClass {
03 /**
04 * @param superClass
05 */
06 public void print(SuperClass superClass) {
07 System.out.println("AppSuperClass:superclass is parameter");
08
09 }
10 /**
11 * @param subClass
12 */
13 public void print(SubClass subClass) {
14 System.out.println("AppSuperClass:subclass is parameter");
15
16 }
17 }
18
19 class AppSubClass extends AppSuperClass {
20 /**
21 * @param superClass
22 */
23 public void print(SuperClass superClass) {
24 System.out.println("AppSubClass:superclass is parameter");
25
26 }
27 /**
28 * @param subClass
29 */
30 public void print(SubClass subClass) {
31 System.out.println("AppSubClass:subclass is parameter");
32
33 }
34 }
35 public class OverloadedTest {
36 public static void main(String[] args) {
37 AppSuperClass appSuperClass = new AppSuperClass();
38 AppSuperClass appSubClass = new AppSubClass();
39 SuperClass superClass = new SuperClass();
40 SuperClass subClassInstance = new SubClass();
41 /*
42 * Making request to print AppSuperClass
43 * 1. Passing SuperClass instance
44 * 2. Passing SubClass instance (*make note of the type) <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1304052800g" alt=":)" class="wp-smiley">
45 */
46
47 appSuperClass.print(superClass);
48 appSuperClass.print(subClassInstance);
49 /*
50 * Above is repeated with AppSubClass instance
51 */
52 appSubClass.print(superClass);
53 appSubClass.print(subClassInstance);
54 }
55
56 }
When I run this I get
AppSuperClass:superclass is parameter
AppSuperClass:superclass is parameter
AppSubClass:superclass is parameter
AppSubClass:superclass is parameter
How can I get
AppSuperClass:superclass is parameter
AppSuperClass:subclass is parameter
AppSubClass:superclass is parameter
开发者_JAVA百科 AppSubClass:subclass is parameter
as o/p without changing the type of any of the objects?
Your question isn't very clear, but it sounds like you're basically after execution-time overloading, which simply doesn't exist in Java. Overloads are entirely resolved at compile-time. One option is this:
public void print(SuperClass superClass) {
if (superClass instanceof SubClass) {]
print((SubClass) superClass);
return;
}
System.out.println("AppSuperClass:superclass is parameter");
}
Note that you'd need to do this in the override as well, or have a template method which does this, and a separate printImpl(SuperClass)
method which can be overridden in the subclass.
To take this to the logical extreme, you might have:
class AppSuperClass {
public final void print(SuperClass superClass) {
if (superClass is SubClass) {
printImpl((SubClass) superClass);
} else {
printImpl(superClass);
}
}
protected void printImpl(SuperClass superClass) {
...
}
protected void printImpl(SubClass subClass) {
...
}
}
AppSubClass
would then only override printImpl
(one or both overloads).
EDIT: As noted in comments, an alternative is to use the Visitor pattern. It's not a pattern I'm terribly fond of, but if you can modify SuperClass
and SubClass
to know about AppSuperClass
(or an interface it implements) it could work for you.
You pass the parameters as SuperClass
. Either by using the genuine type from start, or by casting when calling the method.
SubClass subClassInstance = ....
Or use instance type checking inside the method.
The problem is that the compiler just knows the parameters are at least of type SuperClass
and doesn't know the actual type of the objects you assigned to the variables. Thus it always uses the method taking the SuperClass
parameter.
You could check for the class of the parameter in the method, but overloading as you want to use it, doesn't work here I'm afraid.
精彩评论