简体   繁体   中英

The constructor <Class> is undefined. Can't use <super(arg)> in java

I'm a complete newbie in Java. I can't understand why Java doesn't allow me to use the keyword super(arg). OBS: Design05C is subclass of Design05.

public class Design05C extends Design05 {
    private double x;
    private double y;

    public Design05C(char typeCoord, double xCartesian, double yCartesian) {
        super(typeCoord);
        this.x = xCartesian;
        this.y = yCartesian;
    }   

    //Instance methods **************************************************    
    public double getX()  
    {
        if(typeCoord == 'C') 
            return x;
        else 
            return (Math.cos(Math.toRadians(y)) * x);
    }   

    public double getY()
    {
        if(typeCoord == 'C') 
            return y;
        else 
            return (Math.sin(Math.toRadians(y)) * x);
    }    
}

This is the superclass, Design05:

public class Design05
{
    public char typeCoord;
    public Design05C designC;
    public Design05P designP;

    public Design05(char type, Design05C dCartesian, Design05P dPolar)
    {
        if(type != 'C' && type != 'P')
            throw new IllegalArgumentException();
        typeCoord = type;
        designC = dCartesian;
        designP = dPolar;
    }
}

Thank you!!!

Possibility reason is,

super class Design05 class don't have a constructor which takes a char as argument.

Edit

Since the constructor

 public Design05(char type, Design05C dCartesian, Design05P dPolar)
    {
        if(type != 'C' && type != 'P')
            throw new IllegalArgumentException();
        typeCoord = type;
        designC = dCartesian;
        designP = dPolar;
    }

taking three argument ,but you are passing only one parameter here is

super(typeCoord);

So add another constructor to your super class such that it takes char alone also.

public class Design05
{
    public char typeCoord;
    public Design05C designC;
    public Design05P designP;

    public Design05(char type, Design05C dCartesian, Design05P dPolar)
    {
        if(type != 'C' && type != 'P')
            throw new IllegalArgumentException();
        typeCoord = type;
        designC = dCartesian;
        designP = dPolar;
    }

   public Desgin05(char type){

   typeCoord = type;

  }
}

test that Design05 has created this contructor

public Desgin05(char value){

this.value = value;

}

if there isn't constructor, you need to create.

There is no constructor in Design05 class which takes a single char argument. You will have to change your call to super constructor from this

  super(typeCoord);

to this

  super(typeCoord, xCartesian, yCartesian);

Then your code would work. Try.

EDIT:

Your design itself is incorrect. You cannot add the subclasses objects in the constructor of the super class. This wont work. Remove the subclass objects from the constructor.

If you want to add the subclass objects inside the super class object, then keep references of them and initialize them after the construction of the objects. And not in the constructor.

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