If you want a way to specify, say, "a type that extends Animal and implements Fly", just define a class that does exactly that:
public abstract class FlyingAnimal extends Animal implements Fly{ }
Now you have Eagle
and Falcon
extend from FlyingAnimal
rather directly from Animal
:
public class Falcon extends FlyingAnimal {
public void fly(){ System.out.println("I'm a fast flier");
}
public class Eagle extends FlyingAnimal {
public void fly(){ System.out.println("I'm built for soaring");
}
public class Cat extends Animal {
// I'm a cat; I can't fly
}
Now you can do something like this:
public void flyIt(FlyingAnimal fa){
fa.fly();
}
public void test(){
Falcon falcon = new Falcon();
Animal eagle = new Eagle();
Animal cat = new Cat();
flyIt(falcon); // OK: `Falcon` is a `Falcon`, which is also
// a `FlyingAnimal`
flyIt(cat); // COMPILE ERROR: `cat` is an `Animal`,
// which is not a subclass of `FlyingAnimal`
flyIt(eagle); // COMPILE ERROR: `eagle` is an `Animal`, which is
// not a `FlyingAnimal`
flyIt((Eagle)eagle);
// OK: because we know that `eagle` actually references
// an `Eagle`, we know the type-cast `(Eagle)eagle`
// will succeed at run-time; `Eagle` is a `FlyingAnimal`
// and thus is acceptable as an argument to `flyIt`
flytIt((FlyingAnimal)eagle);
// OK: because we know that `eagle` actually references
// an `Eagle`, which in turn is a `FlyingAnimal`, we
// know the type-cast `(FlyingAnimal)eagle` will
// succeed at run-time
flyIt((FlyingAnimal)cat);
// RUN-TIME ERROR: `cat` references a `Cat`, which is
// an `Animal` but not a `FlyingAnimal`, and so will
// not successfully convert to a `FlyingAnimal` at
// run-time.