Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting this error and it is confusing me. I do not know how to fix it. I know there are some other errors in there but i'm not too worried about those for now.

Here is the code:

import java.awt.*;

public abstract class Shape {
// Instance variables
private int x;
private int y;
private Color color;

// Constructor
protected Shape(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}

// Abstract methods
 public abstract void draw(Graphics g);
public abstract int getHeight();
public abstract int getWidth();
public abstract void scale();
public abstract double area();

// Other instance methods
public Color getColor() {
return color;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void move(int dx, int dy) {
x += dx;
y += dy;
}

public void setColor(Color color) {
this.color = color;
}

public String toString() {
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";

} }

Here are the class files:

// Represents a circle that can be displayed in a graphics
// context

import java.awt.*;

public class Circle extends Shape {
// Instance variables
private int diameter;
private double scaleFactor = 2;
private double area;

// Constructor
public Circle(int x, int y, Color color, int diameter) {
super(x, y, color);
this.diameter = diameter;
}

 // Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillOval(getX(), getY(), diameter, diameter);
}

public int getHeight() {
return diameter;
}

public int getWidth() {
return diameter;
}

public void scale(double scaleFactor) {
(double) diameter *= scaleFactor;
}

public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}

public String toString() {
return "Diameter: " + diameter + "\nWidth: " + getWidth() + "Height: " + getHeight();
}
}

import java.awt.*;

public class Rectangle extends Shape {
// Instance variables
private int width;
private int height;
private double scaleFactor = 2;
private double diameter = width * height;
private double area;

// Constructor
public Rectangle(int x, int y, Color color,
               int width, int height) {
super(x, y, color);
this.width = width;
this.height = height;
}

// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillRect(getX(), getY(), width, height);
}

public int getHeight() {
return height;
}

public int getWidth() {
return width;
}

public void scale(double scaleFactor){
width *= scaleFactor;
height *= scaleFactor;
}

public double area() {
    area = (3.14 * (diameter * diameter)) / 4;
    return area;
}

public String toString() {
    return "Diameter: " + diameter + "\nWidth: " + getWidth() + "Height: " +
getHeight();
}
}

And here is the test file:

public class ShapeTest
{
public static void main(String[] args)
{
    Color myC = new Color(10, 30, 20);
    Circle myCircle = new Circle(0, 0, myC, 5);
    Rectangle myRectangle = new Rectangle(0, 0, myC, 2, 3);

    System.out.println(myCircle.toString());
    System.out.println(myRectangle.toString()); 


}
}

Here are the errors:

javac ShapeTest.java
ShapeTest.java:5: cannot find symbol
symbol  : class Color
location: class ShapeTest
    Color myC = new Color(10, 30, 20);
    ^
ShapeTest.java:5: cannot find symbol
symbol  : class Color
location: class ShapeTest
    Color myC = new Color(10, 30, 20);
                    ^
./Shape.java:46: ';' expected
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " +   getBlue() + ", " getGreen() + ")";
                                                                                                   ^
./Shape.java:46: not a statement
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + g etBlue() + ", " getGreen() + ")";
                                                                                                             ^
./Shape.java:46: cannot find symbol
symbol  : method getRed()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
                                                               ^
./Shape.java:46: cannot find symbol
symbol  : method getBlue()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
                                                                                 ^
./Shape.java:46: cannot find symbol
symbol  : method getGreen()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
                                                                                                  ^
./Circle.java:16: Circle is not abstract and does not override abstract method scale() in     Shape
public class Circle extends Shape {
   ^
./Circle.java:43: unexpected type
required: variable
found   : value
(double) diameter *= scaleFactor;
^
 ./Rectangle.java:3: Rectangle is not abstract and does not override abstract method scale() in Shape
 public class Rectangle extends Shape {
   ^
10 errors
like image 322
TheNameHobbs Avatar asked Dec 08 '25 14:12

TheNameHobbs


2 Answers

I agree with user1490835's diagnosis, but I thought it might be good to explain what's going on with this error, just in case you're unsure.

An abstract method is a promise to the compiler that all child classes (ones extending the class with the abstract method) will be able to do that method. Part of fulfilling that promise is that the child classes have to implement the abstract method using the exact same header. So in the Java runtime, you can call the abstract method on an instance of any of the child classes and know you'll have used the correct parameters and so on.

Why? Because if the subclass used a slightly different header for that method (causing the runtime error we're trying to avoid) the code would not have compiled (it would've given the error you're getting).


For example, imagine an abstract class A that has an abstract method f() and two concrete subclasses B and C. Somewhere in the program we have:

    A avar;
    if (somevar < othervar) {
        avar = new B();
    } else {
        avar = new C();
    }
    avar.f();

We don't know wether avar will be a B or a C at runtime, because it depends on the result of the if statement. But we do know that avar will be a subclass of A. Since all subclasses of A must have implemented f with the correct header (otherwise the programmer would have gotten the compile error you're getting) we know this code won't break while it's running!


In your code, the difference between the abstract method header and its implementation is:

public abstract void scale(); // no parameter
public void scale(double scaleFactor) { // one parameter

To fix it, add the double parameter to your abstract method.

like image 84
chm Avatar answered Dec 10 '25 03:12

chm


The very first issue that i could find is the that you have declared a Scale abstract method with no parameters but when you are extending the abstract class, you are implementing the Scale method with parameters. So that's why this error is thrown in all your sub classes:

Circle is not abstract and does not override abstract method scale() in Shape

Also with the following error:

./Shape.java:46: ';' expected

return "Position: (" + getX() + ", " + getY() + ") Color: (" + 
getRed() + getBlue() + ", " getGreen() + ")";

You forgot to add the + sign before getGreen function and Java expects that as the end of line, so that's why the error ';'

the getRed symbol error is because you don't have a function called getRed? and so on!

like image 22
Baahubali Avatar answered Dec 10 '25 03:12

Baahubali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!