Consider this base class:
public class Cat {
public void meow() {
// meowing
}
}
And for some reason, cats subclasses can't inherit from this Cat
class, because they MUST inherit from another base class, and Java doesn't support multiple inheritance. Thus:
public class Cat1 extends BaseClass1 {
}
public class Cat2 extends BaseClass2 {
}
public class Cat3 extends BaseClass3 {
}
How can I dynamically augment instances of CatX
classes to inherit that meow
method from Cat
class?
A pseudo code might be:
public cat1Instance = new Cat1();
Cat.augmentWithCatBehavior(cat1Instance);
Is there a way for me to achieve this?
Update: Composition is not what I want. For composition, I need to have an instance of Cat
class in all of my Cat1
to CatN
classes. I have to create a wrapper method in each class so that I can call that meow
. It's a huge amount of boilerplate code. That's why I said augmenting dynamically.