Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- Base b; // declare b
- if (Math.random()<0.5) // maybe..
- b=new Sub1(); // b is an instance of Sub1..
- else
- b=new Sub2(); // or Sub2
- b.method(); // which method?
- }
- }
- class Base
- {
- void method() {System.out.println("Base class");}
- }
- class Sub1 extends Base
- {
- void method() {System.out.println("Sub1 class");}
- }
- class Sub2 extends Base
- {
- void method() {System.out.println("Sub2 class");}
- }
Advertisement
Add Comment
Please, Sign In to add comment