Skip to content

Instantly share code, notes, and snippets.

@Javran
Created November 6, 2012 04:14
Show Gist options
  • Save Javran/4022506 to your computer and use it in GitHub Desktop.
Save Javran/4022506 to your computer and use it in GitHub Desktop.
interface vs class itself
public class Test {
public static class A {}
public static class B extends A {}
public static class C extends A {}
public interface Hello {
void hello(A a);
}
public static class Tester implements Hello {
@Override
public void hello(A a) {
System.out.println( "Hello, A" );
}
public void hello(B b) {
System.out.println( "Hello, B" );
}
public void hello(C c) {
System.out.println( "Hello, C" );
}
}
public static class Tester2 implements Hello {
@Override
public void hello(A a) {
myHello(a);
}
public void myHello(A a) {
System.out.println( "Hello, A" );
}
public void myHello(B b) {
System.out.println( "Hello, B" );
}
public void myHello(C c) {
System.out.println( "Hello, C" );
}
}
public static void main(String[] args) {
System.out.println("Interface:");
Hello h = new Tester();
h.hello( new A() );
h.hello( new B() );
h.hello( new C() );
System.out.println("Class:");
Tester t = new Tester();
t.hello( new A() );
t.hello( new B() );
t.hello( new C() );
System.out.println("Class2:");
Tester2 t2 = new Tester2();
t2.hello( new A() );
t2.hello( new B() );
t2.hello( new C() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment