0

I want to inherit specific instances from a superclass, not all of them.

For example:

public class Snake extends Reptile {
    private boolean hasLegs  = false;

    public Snake(double[] legLength, double tailLength, String color, boolean hasScales, boolean hasLegs) {
        super(legLength, tailLength, color, hasScales);
        this.hasLegs = hasLegs;
    }

I want to inherit all instance variables from the class Reptile except double[] legLength(as snakes doesn't have legs).

How can I do that without changing code in the Reptile class?

Thanks.

1 Answer 1

1

I think you are asking how to not have to pass all the parameters you don't need to the parent class. You can't do that, you need to pass them all, but that doesn't mean you have to expose them all in the child class:

public Snake(double tailLength, String color, boolean hasScales) {
    super(null, tailLength, color, hasScales);
    this.hasLegs = false;
}

You can't just get some variables from the parent - you get them all. You can set them to values that make sense for your subclass. That is the whole point!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.