[java] Class is not abstract and does not override abstract method

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!

This question is related to java inheritance abstract-class draw

The answer is


If you're trying to take advantage of polymorphic behavior, you need to ensure that the methods visible to outside classes (that need polymorphism) have the same signature. That means they need to have the same name, number and order of parameters, as well as the parameter types.

In your case, you might do better to have a generic draw() method, and rely on the subclasses (Rectangle, Ellipse) to implement the draw() method as what you had been thinking of as "drawEllipse" and "drawRectangle".


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to inheritance

How to extend / inherit components? Inheritance with base class constructor with parameters Class is not abstract and does not override abstract method Why not inherit from List<T>? Can an interface extend multiple interfaces in Java? How to call Base Class's __init__ method from the child class? How should I have explained the difference between an Interface and an Abstract class? JavaScript OOP in NodeJS: how? When do I have to use interfaces instead of abstract classes? C++ calling base class constructors

Examples related to abstract-class

invalid new-expression of abstract class type Class is not abstract and does not override abstract method When to use: Java 8+ interface default method, vs. abstract method Spring can you autowire inside an abstract class? Abstract Class:-Real Time Example How should I have explained the difference between an Interface and an Abstract class? When do I have to use interfaces instead of abstract classes? Is it possible to make abstract classes in Python? Abstract Class vs Interface in C++ How do you handle a "cannot instantiate abstract class" error in C++?

Examples related to draw

How to draw a rectangle around a region of interest in python Class is not abstract and does not override abstract method Draw an X in CSS How to draw a standard normal distribution in R Can I draw rectangle in XML? Android: how to draw a border to a LinearLayout How to draw in JPanel? (Swing/graphics Java) Android Drawing Separator/Divider Line in Layout? Drawing a line/path on Google Maps How to show x and y axes in a MATLAB graph?