Reputation: 477
Suppose I have a class that extends another class and implements one or more interfaces. How can I specify a type that requires such condition?
For example:
class Eagle extends Animal implements Fly {
}
class Falcon extends Animal implements Fly {
}
public static void main (){
??? anAnimalWhoCanFly;
}
Update: I removed the list. Just suppose I want to have an object that is an object of a class that extends Animal and implements Fly.
Thanks
Upvotes: 2
Views: 1179
Reputation: 4592
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.
Upvotes: 2
Reputation: 477
It is sad that we cannot do it in java, if @EvgenyTanhilevich is right.
I found something that somehow does what I want. It is not exactly what I wanted, because it does not require that object MUST be an instance of class that implements Fly, but still let me call fly function:
public class Main {
public static void main(String[] args) {
Eagle eagle = new Eagle();
((Fly)eagle).fly();
}
}
Upvotes: 0