简体   繁体   中英

Can an extended class not inherit a method?

My question is: can I create a class that extends another class, but choose not to inherit a particular method?

A simple example, I have an abstract Bird class with one method, fly(). I want to create classes for different species of birds, but penguins can't fly. How can I ensure that a penguin object can not call the fly() method?

No you cant

The closest you can get is to override the method and throw

@Override
public void fly() {
    throw new UnsuportedOperationException();
}

The Java API does this sometimes but I see this as bad design personally.

What you could do is have a hierarchy of Bird and two sub-classes FlyableBird and NonFlyableBird and then have Penguin hang off NonFlyableBird. The fly method would then only be in FlyableBird class. The bird class could still contain things like makeNoise() (all birds make noises dont they?) which are common to both FlyableBird and NonFlyableBird

No you can't, that would break polymorphism fundamentally. You have to implement the function and do something like throw an Exception or do nothing.

You need to have very good design to implement scenario mentioned by you. You need to have design like following.

public abstract class Bird {
    FlyBehavior flyBehavior;
    public Bird(){
    }

    public void performFly(){
    flyBehavior.fly();      
    }

    public void setFlyBehavior(FlyBehavior fb){
    flyBehavior = fb;
    }
}

public interface FlyBehavior {
    public void fly();
}


public class FlyingBirds implements FlyBehavior {

@Override
public void fly() {
    // TODO Auto-generated method stub
    System.out.println("I can fly");
}
}


public class NonFlyingBirds implements FlyBehavior {

@Override
public void fly() {
    // TODO Auto-generated method stub
    System.out.println("I am not able to fly");
}
}


public class KingFisher extends Bird {
public KingFisher(){
    flyBehavior = new FlyingBirds();
}

public static void main(String[] args) {
    Bird bird = new KingFisher();
    bird.performFly();      
}
}



public class Penguins extends Bird {
public Penguins(){
    flyBehavior = new NonFlyingBirds();
    }

public static void main(String[] args) {
    Bird bird = new Penguins();
    bird.performFly();
}
}

You can't. But, you could throw a UnsupportedOperationException in the penguin's fly() method.

You have a wrong hierarchy of classes/concepts if you need to do what you're trying to do. Re-design your classes.

覆盖fly方法以指向另一个操作或返回而不执行该操作。

您可以将方法Fly声明为抽象方法 ,但如果您不提供实现,您将被迫将Penguin类声明为抽象,我认为这不是您要寻找的,因为企鹅不应该是抽象的,没有任何意义。

You could create an interface called Fly. Then each concrete class that extends Bird that can fly should implement this interface. Penguin would not implement this so your penguin object reference would never fly.

I think, You can create one main abstract class Bird with abstract methods like fly,walk etc. Then you can make two subclass of that Bird class. FlyingBirds and NonFlyingBirds.In non-flying birds concrete class,you can make the fly method as final.So this method will not get inherited to the subclass.

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