5

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?

3
  • override fly() in the penguin class and make it throw a "I'm a Brick" exception?
    – Marc B
    Sep 10, 2012 at 19:00
  • I think you need to have another subset of classes(another level of classification), your design need to be changed. Whatever you are asking can't be done straight away.
    – kosa
    Sep 10, 2012 at 19:01

9 Answers 9

12

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

4
  • Thank you for responding. I had thought about sub-dividing the classes further, it just seemed like a hassle if there was an easier way. Looks like it's time for me to learn how to throw exceptions.
    – MrDetail
    Sep 10, 2012 at 19:12
  • @MrDetail UnsupportedOperationException is a runtime exception. Look up the difference between checked and unchecked exception. It could affect your code in quite a big way
    – RNJ
    Sep 10, 2012 at 19:14
  • @RNJ To through UnsuportedOperationException we need to override fly() on NonFlyableBirds. as per @MrDetail's question, he wants ensure that a penguin object can not call the fly() method. But the design suggested calls fly and throws UnsuportedOperationException. I think need to call fly for NonFlyableBirds also and then write some logic to avoid flying behavior. Sep 11, 2012 at 8:18
  • @PrasadSDeshpande why have fly() in NonFlyableBirds? It it's not there you dont need any logic to avoid flying behaviour
    – RNJ
    Sep 14, 2012 at 7:53
3

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.

3

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

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

1
  • 3
    You should use UnsupportedOperationException Sep 10, 2012 at 19:02
1

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

0

Override the fly method to either point to another action or returns without performing that action.

0

You could declare the method Fly as an abstract method, but if you don't provide an implementation you'll be forced to declare the Penguin class as abstract too, and i think that's not what you're looking for since penguin shouldn't be abstract, it doesn't make any sense.

0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.