RandomGuy32

InstanceofTest.java

May 31st, 2015
270
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class InstanceofTest {
  2.     public static void main(String[] args) {
  3.         Something someObject = new Something();
  4.         InstanceofTest.doSomething(someObject);
  5.     }
  6.     public static void doSomething(SomeInterface s) {
  7.         s.method1();
  8.         if (s instanceof SomeOtherInterface) {
  9.             s.method2();
  10.         }
  11.     }
  12. }
  13.  
  14. class Something implements SomeInterface {
  15.     public Something() {}
  16.    
  17.     public void method1() {}
  18. }
  19. class SomethingElse implements SomeInterface, SomeOtherInterface {
  20.     public SomethingElse() {}
  21.    
  22.     public void method1() {}
  23.     public void method2() {}
  24. }
  25. interface SomeInterface {
  26.     public void method1();
  27. }
  28. interface SomeOtherInterface {
  29.     public void method2();
  30. }
RAW Paste Data