Search code examples
javaoopdesign-patternsooad

have an issue with designing in the class in proper manner


What’s the issue with the following, and how would I implement it better using OO principles?

My app contains a bunch of shape classes which all inherit from Shape - Circle, Rectangle, Triangle, etc. Some of these need to be displayed on the screen, in which case they need to leverage common screen logic, so there's a ScreenShape superclass which contains the common logic, and ScreenCircle, ScreenTriangle child classes.


Solution

  • I would suggest to make an interface Shape which provides a basic blue print about the shape of the geometric shape and all your classes implement the shape interface and create a separate class ScreenShape(or abstract class) which all of your classes will extend, and provide the method for displaying on screen in the ScreenShape class. for example your rectangle class will be somewhat like this

    class rectangle extends ScreenShape implements Shape
    {
    
    // provide implementation of Shape interace methods.
    
    
    // over-ride ScreenShape methods
    
    public void draw()
    {
    // actual logic of drawing the objects on screen
    
    }
    
    }