Search code examples
javaoopjcreator

Java - Cannot find symbol constructor in sub class


I'm getting error "cannot find symbol constructor GeometricObject()" Since radius and height is not in the super class, I can't use the super() in the Sphere() constructor.

public class Sphere extends GeometricObject{

private double radius;

public Sphere(){
} 
    public Sphere(double radius){

        this.radius=radius;
    }

    public Sphere(double radius, String color, boolean filled){

        this.radius=radius;
        setColor(color);
        setFilled(filled);
    }

This is the super class public class GeometricObject{

private String color = "white";
private boolean filled;


public GeometricObject(String color, boolean filled){

    this.color=color;
    this.filled=filled;

}

public String getColor(){

    return color;
}

public void setColor(String color){

    this.color=color;
}

public boolean isFilled(){

    return filled;
}

public void setFilled(boolean filled){

    this.filled=filled;
}

public String toString(){

    return "Color: "+color +" and filled: "+filled;
}

}


Solution

  • When you create the derived object first call the super constructor. If you don't the default constructor is called. In your code there is no default constructor with no parameter, that is why the object construction has failed. You need to provide a constructor with no parameter, or call the existing one as below:

    public Sphere(double radius, String color, boolean filled){
      super(color, filled);
      this.radius=radius;
    }