waltermilner

Untitled

May 23rd, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. public class Test {
  2.  
  3.   public static void main(String[] args) {
  4.  
  5.    Base b; // declare b
  6.    if (Math.random()<0.5) // maybe..
  7.      b=new Sub1(); // b is an instance of Sub1..
  8.    else
  9.      b=new Sub2(); // or Sub2
  10.    
  11.    b.method(); // which method?
  12.    
  13.   }
  14. }
  15.  
  16. class Base
  17. {
  18.   void method() {System.out.println("Base class");}
  19. }
  20.  
  21. class Sub1 extends Base
  22. {
  23.   void method() {System.out.println("Sub1 class");}
  24. }
  25.  
  26. class Sub2 extends Base
  27. {
  28.   void method() {System.out.println("Sub2 class");}
  29. }
Advertisement
Add Comment
Please, Sign In to add comment