简体   繁体   中英

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;
}

}

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;
}
public Sphere(double radius, String color, boolean filled){
    this.radius=radius;
    setColor(color);
    setFilled(filled);
}

As written, this implicity calls super(); , which corresponds to GeometricObject() , which doesn't exist. Change it to this:

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

super or this must be called at the start of every constructor - if you don't write it, the compiler will insert super() by default.

However, GeometricObject has no constructor that takes no arguments. If it doesn't exist, you can't call it! Which means the compiler can't call it automatically either.

You need to call super with the colour and filled-ness of the sphere in every constructor of Sphere , like this:

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

    this.radius=radius;
}

Since your super class only has one constructor:

public GeometricObject(String color, boolean filled)

you will need to call it in your sub-class's constructors:

public Sphere(){
    super(?, ?); // but you don't know what values to specify here so you might have to use defaults
} 

public Sphere(double radius){
    super(?, ?); // but you don't know what values to specify here so you might have to use defaults
    this.radius=radius;
}

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

    this.radius=radius;
    setColor(color);    // you can get rid of this
    setFilled(filled);  // and this, since the super constructor does it for you
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM