import java.util.*;
import java.lang.*;
import java.io.*;


class A {
    public void foo() {
        System.out.println("foo");
    }
}

class B extends A {
    public void foo() {
        System.out.println("foobar");
    }
}

class C {
    protected A a;

    public C(A a) {
        this.a = a;
    }
    
    public void callFoo() {
        this.a.foo();
    }
}

class Main {
    public static void main(String[] args) {
        C c1 = new C(new B());
        c1.callFoo();
        
        C c2 = new C(new A());
        c2.callFoo();
    }
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: