jmasterx
jmasterx

Reputation: 54123

How could I add this attribute?

I have a class that is called ShapeBase. It is abstract and draw method must be implemented. It has instance variables for things like width and height. From this, Circle, Line, and Rectangle are subclasses and implement the draw method. The Line class does not have an isFilled property (get / set) but Rectangle, and Circle do. I could obviously just add the property to both separatly, but later on I may want to dynamically gather all shapes that can be 'filled'. I thought of making a filled interface but, the problem is then I have to implement the getter and setter for both. In C++ I'd make use of multiple inheritance to solve this, but what can I do in Java to solve this kind of problem?

Thanks

Upvotes: 0

Views: 72

Answers (3)

aromero
aromero

Reputation: 25761

public abstract class FillableShape extends Shape {
    // isFilled
}

public class Circle extends FillableShape {
   ....
}

public class Rectangle extends FillableShape {
   ....
}

Upvotes: 1

jornb87
jornb87

Reputation: 1461

You could have a FillableShapeBase which is abstract and extends ShapeBase but has the added isFilled property with getters and setters. Rectangle and Circle could inherit from FillableShapeBase.

Upvotes: 1

Daniel Brockman
Daniel Brockman

Reputation: 19270

Why not simply make FilledShape an abstract class that inherits from ShapeBase and implements isFilled?

Upvotes: 0

Related Questions