Last active
January 1, 2016 22:19
-
-
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
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
/** | |
* "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