Class is not abstract and does not override abstract method

user063

So I've been working on a homework on abstraction for my programming class and fell into a problem. The goal for me right now is to be able to use abstraction, then later be able to draw with rectangles and ovals a simple city, like a rectangular building or a oval light on a light post.

The error I am receiving when I compile is: MyTestApp.Rectangle is not abstract and does not override abstract method drawEllipse(java.awt.Graphics) in MyTestApp.Shape. This Error shows up on the line "class Rectangle extends Shape{" right below the class Shape.

My question is what am I doing wrong with my abstraction? I've been messing with the constructors and draw() methods in classes Rectangle and Ellipse for a while now and still to no luck happen to find a solution.

Code is below:

import java.awt.*;
import javax.swing.*;

public class MyTestApp extends JPanel {
    Rectangle rect;
    Ellipse oval;
    public static void main(String [] args) {
        MyTestApp myTestApp = new MyTestApp ();
        myTestApp.test();
    }

    public MyTestApp () { //creates the jframe
        JFrame frame = new JFrame("MyClass Driver");
        setBackground(new Color(200, 250, 200));
        setPreferredSize(new Dimension(500, 400));
        frame.add(this);
        frame.pack();
        frame.setVisible(true);
    }

    public void delay(int msecs) {
        try {
            Thread.sleep(msecs);
        } catch (InterruptedException e) {
        }
    }

    public void paint(Graphics g) {//paints the rectangle and ellipse
        super.paint(g);
        if (rect != null)
            rect.drawRectangle(g);
        if (oval != null)
            oval.drawEllipse(g);
    }

    public void test() {//gives the x/y position, width/height, and fill/outline color for the rectangle and oval
        delay(1000);
        rect = new Rectangle(20, 30, 23, 75, Color.GREEN, Color.BLUE);
        oval = new Ellipse(10, 10, 10 , 34, Color.RED, Color.MAGENTA);
        repaint();
    }

    public abstract class Shape{//abstract class Shape that sets the x/y, width/height, and colors for the shapes
        private int x, y, width, height;
        private Color fillColor;
        private Color outlineColor;
        public Shape(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
            setXY(x, y);
            setSize(width, height);
            setFillColor(fillColor);
            setOutlineColor(outlineColor);  
        }

        public boolean setXY(int x, int y) {
            this.x = x;
            this.y = y;
            return true;
        }

        public void setSize(int width, int height) {
            if (width > 0)
                this.width = width;
            if (height > 0)
                this.height = height;
        }

        public boolean setFillColor(Color fillColor){
            if (fillColor == null) return false;
            this.fillColor = fillColor; 
            return true;
        }

        public boolean setOutlineColor(Color outlineColor){
            if (outlineColor == null) return false;
            this.outlineColor = outlineColor; 
            return true;
        }

        public Color getFillColor() {
            return fillColor;
        } 

        public Color getOutlineColor() {
            return outlineColor;
        } 

        public abstract void drawRectangle(Graphics g);//do i need two?
        public abstract void drawEllipse(Graphics g);//do i need both?
    }
    class Rectangle extends Shape{//!!!!!!!!!! where the error shows
        public Rectangle(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
            super(x, y, width, height, fillColor, outlineColor);
        }

        public void drawRectangle(Graphics g){//draws the retangle
            g.setColor(fillColor);
            g.fillRect(x, y, width, height);
            g.setColor(outlineColor);
            g.drawRect(x, y, width, height);
        }
    }
    class Ellipse extends Shape{
        public Ellipse(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
            super(x, y, width, height, fillColor, outlineColor);
        }

        public void drawEllipse(Graphics g){//draws the ellipse
            g.setColor(fillColor);
            g.fillOval(x, y, width, height);
            g.setColor(outlineColor);
                g.drawOval(x, y, width, height);
            }
        }
}

Thanks for reading and helping!

user2982130

Both classes Rectangle and Ellipse need to override both of the abstract methods.

To work around this, you have 3 options:

  • Add the two methods
  • Make each class that extends Shape abstract
  • Have a single method that does the function of the classes that will extend Shape, and override that method in Rectangle and Ellipse, for example:

    abstract class Shape {
        // ...
        void draw(Graphics g);
    }
    

And

    class Rectangle extends Shape {
        void draw(Graphics g) {
            // ...
        }
    }

Finally

    class Ellipse extends Shape {
        void draw(Graphics g) {
            // ...
        }
    }

And you can switch in between them, like so:

    Shape shape = new Ellipse();
    shape.draw(/* ... */);

    shape = new Rectangle();
    shape.draw(/* ... */);

Again, just an example.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Class is not abstract and does not override abstract method error

From Dev

Not abstract and does not override abstract method

From Dev

Not abstract and does not override abstract method

From Dev

Does abstract class hide or override the method of the interface?

From Dev

Error: anonymous class is not abstract and does not override abstract method

From Dev

Class is not abstract and does not override abstract method onClick(View) in OnClickListener

From Dev

Is not abstract and does not override abstract method act

From Dev

Not abstract and does not override abstract method - IDL

From Dev

Is not abstract and does not override abstract method act

From Dev

Weird "is not abstract and does not override abstract method" error

From Dev

Not abstract and does not override abstract method - IDL

From Dev

Not abstract and does not override abstract method punctuate(long)

From Dev

Is not abstract and does not override abstract method onConnectionFailed(ConnectionResult)

From Dev

Override method within abstract class

From Dev

Override method within abstract class

From Dev

Class is not abstract and does not override abstract method done(Object,Throwable) in ParseCallback2

From Dev

Class is not abstract and does not override abstract method done(Object,Throwable) in ParseCallback2

From Dev

Method is not abstract and does not override method in Interface error

From Dev

How to solve not abstract and does not override abstract method onTabReselected

From Dev

Java error - Send is not abstract and does not override abstract method run() in Runnable

From Dev

ConcreteA is not abstract and does not override abstract method doSomething(InterfaceA) in InterfaceC

From Dev

"is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener"

From Dev

SendData is not abstract and does not override abstract method returnData(byte[],byte) in ReceivedDataListener

From Dev

Java error - Send is not abstract and does not override abstract method run() in Runnable

From Dev

Actions is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

From Dev

TabMain is not abstract and does not override abstract method onTabReselected(Tab,FragmentTransaction) in TabListener

From Dev

implem is not abstract and does not override abstract method FinalScore() in extending interfaces

From Dev

extend an override method from abstract class in java

From Dev

Anonymous class is not abstract and does not override (Android + Retrofit)

Related Related

  1. 1

    Class is not abstract and does not override abstract method error

  2. 2

    Not abstract and does not override abstract method

  3. 3

    Not abstract and does not override abstract method

  4. 4

    Does abstract class hide or override the method of the interface?

  5. 5

    Error: anonymous class is not abstract and does not override abstract method

  6. 6

    Class is not abstract and does not override abstract method onClick(View) in OnClickListener

  7. 7

    Is not abstract and does not override abstract method act

  8. 8

    Not abstract and does not override abstract method - IDL

  9. 9

    Is not abstract and does not override abstract method act

  10. 10

    Weird "is not abstract and does not override abstract method" error

  11. 11

    Not abstract and does not override abstract method - IDL

  12. 12

    Not abstract and does not override abstract method punctuate(long)

  13. 13

    Is not abstract and does not override abstract method onConnectionFailed(ConnectionResult)

  14. 14

    Override method within abstract class

  15. 15

    Override method within abstract class

  16. 16

    Class is not abstract and does not override abstract method done(Object,Throwable) in ParseCallback2

  17. 17

    Class is not abstract and does not override abstract method done(Object,Throwable) in ParseCallback2

  18. 18

    Method is not abstract and does not override method in Interface error

  19. 19

    How to solve not abstract and does not override abstract method onTabReselected

  20. 20

    Java error - Send is not abstract and does not override abstract method run() in Runnable

  21. 21

    ConcreteA is not abstract and does not override abstract method doSomething(InterfaceA) in InterfaceC

  22. 22

    "is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener"

  23. 23

    SendData is not abstract and does not override abstract method returnData(byte[],byte) in ReceivedDataListener

  24. 24

    Java error - Send is not abstract and does not override abstract method run() in Runnable

  25. 25

    Actions is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener

  26. 26

    TabMain is not abstract and does not override abstract method onTabReselected(Tab,FragmentTransaction) in TabListener

  27. 27

    implem is not abstract and does not override abstract method FinalScore() in extending interfaces

  28. 28

    extend an override method from abstract class in java

  29. 29

    Anonymous class is not abstract and does not override (Android + Retrofit)

HotTag

Archive