简体   繁体   中英

How to use polymorphism with inheritance and interface at the same time in Java?

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

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.

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();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM