public interface Animal { void bark(); } public class Cat implements Animal{ @Override public void bark() { System.out.println("miao..."); } } public class Dog implements Animal{ @Override public void bark() { System.out.println("wang..."); } } public class Context { private Animal animal; public Context(Animal animal){ this.animal = animal; } public void bark(){ animal.bark(); } public static void main(String[] args) { Context context; context = new Context(new Dog()); context.bark(); context = new Context(new Cat()); context.bark(); } }