Design using composition and interfaces in Java <p>I designed the following for a problem:</p> <pre><code>class Animal { // ... } class Guppy extends Animal { ... } class Pigeon extends Animal { ... } class TailedAnimal extends Animal { // ... } class Dog extends TailedAnimal { ... } class Cat extends TailedAnimal { ... } class HornedAnimal extends Animal { // ... } class Ram extends HornedAnimal { ... } public static void main(String[] args) { Animal a = getSomeAnimal(); a.doSomething(); if (a instanceof TailedAnimal) { // do something } if (a instanceof HornedAnimal) { // do something else } } </code></pre> <p>Animal, HornedAnimal and TailedAnimal are used mainly as data models.</p> <p>Since Java does not support multiple inheritance, I have trouble creating Rhinoceros which is a Horned and Tailed animal. After asking around, someone recommended using composition and interfaces. I came up with the following:</p> <pre><code>class Animal { // ... } class Guppy extends Animal { ... } class Pigeon extends Animal { ... } class Ram extends Animal implements IHorned { ... } class Cat extends Animal implements ITailed { ... } class Dog extends Animal implements ITailed { BasicTail t = new BasicTail(); public Object getTail() { return t.getTail(); } public void setTail(Object in) { t.setTail(in); } } interface ITailed { public Object getTail(); public void setTail(Object in); //... } class BasicTail implements ITailed { Object myTail; public Object getTail() { return myTail; } public void setTail(Object t) { myTail = t; } } interface IHorned { // getters and setters } public static void main(String[] args) { Animal a = getSomeAnimal(); a.doSomething(); // how do I check if a is horned or tailed? } </code></pre> <p>My questions are:</p> <ol> <li>Is this the correct way of using composition/interfaces?</li> <li>My interface has getters and setters. Is that supposed to be correct? Is there any way to avoid this? Assuming that there is currently no way to abstract the behaviour of Tails and Horns, and they're are being used mainly as data holders.</li> <li>How do I determine if my Animal is Horned or Tailed?</li> </ol>