Skip to content

Instantly share code, notes, and snippets.

@vijay-v vijay-v/Program.java
Last active Jan 1, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.