Created
December 31, 2018 17:04
-
-
Save benjishults/af4d65d6f9c5ef0681631f3c272bf251 to your computer and use it in GitHub Desktop.
Simple overloading function puzzle in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base { | |
public void foo(Base x) { | |
System.out.println("Base.Base"); | |
} | |
public void foo(Derived x) { | |
System.out.println("Base.Derived"); | |
} | |
public void bar(Object x) { | |
System.out.println("bar-Base:Object"); | |
} | |
} | |
class Derived extends Base { | |
public void bar(Derived x) { | |
System.out.println("bar-Derived:Derived"); | |
} | |
public void foo(Base x) { | |
System.out.println("Derived.Base"); | |
} | |
public void foo(Derived x) { | |
System.out.println("Derived.Derived"); | |
} | |
} | |
public class StaticParamsDemo { | |
public static void whichFoo(Base arg1, Base arg2) { | |
arg1.foo(arg2); | |
} | |
public static void main(String[] args) { | |
Base b = new Base(); | |
Derived d = new Derived(); | |
Base d2 = new Derived(); | |
System.out.print("b.bar(b): "); | |
b.bar(b); | |
System.out.print("b.bar(d): "); | |
b.bar(d); | |
System.out.print("d.bar(b): "); | |
d.bar(b); | |
System.out.print("d.bar(d): "); | |
d.bar(d); | |
System.out.print("d.bar(d2): "); | |
d.bar(d2); | |
System.out.print("d2.bar(b): "); | |
d2.bar(b); | |
System.out.print("d2.bar(d): "); | |
d2.bar(d); | |
System.out.print("d2.bar(d2): "); | |
d2.bar(d2); | |
System.out.print("whichFoo(b, b): "); | |
whichFoo(b, b); | |
System.out.print("whichFoo(b, d): "); | |
whichFoo(b, d); | |
System.out.print("whichFoo(d, b): "); | |
whichFoo(d, b); | |
System.out.print("whichFoo(d, d): "); | |
whichFoo(d, d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment