Skip to content

Instantly share code, notes, and snippets.

@vijay-v
Last active January 1, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijay-v/8209247 to your computer and use it in GitHub Desktop.
Save vijay-v/8209247 to your computer and use it in GitHub Desktop.
Example for "Reference to an object of any class that implements two or more given interfaces", http://stackoverflow.com/questions/20860880/reference-to-an-object-of-any-class-that-implements-two-or-more-given-interfaces
/**
* "Reference to an object of any class that implements two or more given interfaces"
*
* http://stackoverflow.com/questions/20860880/reference-to-an-object-of-any-class-that-implements-two-or-more-given-interfaces
*
* @author VJ
*
*/
interface Foo { void foo(); }
interface Bar { void bar(); }
class Humpty implements Foo, Bar {
public void foo() { System.out.println("Humpty.foo()"); }
public void bar() { System.out.println("Humpty.bar()"); }
}
class Dumpty implements Foo, Bar {
public void foo() { System.out.println("Dumpty.foo()"); }
public void bar() { System.out.println("Dumpty.bar()"); }
}
@SuppressWarnings("unchecked")
public class Program {
public static<FooBar extends Foo & Bar> void main(String[] args) {
FooBar foobar = (FooBar) new Humpty<FooBar>();
foobar.foo();
foobar.bar();
foobar = (FooBar) new Dumpty();
foobar.foo();
foobar.bar();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment