简体   繁体   中英

Super() not working on my extends class

I'm pretty new in java and I'm doing a simple program but I don't know why I get an error in my program when I'm try to use super... Does anybody can explain me or what is my error, because it's not accepting super.myCoord() what should I change or add?

  public class myCoord {

    private double coorX, coorY;

    public myCoord(){
        coorX = 1;
        coorY = 1;
    }

    public myCoord(double x,double y){
        coorX =  x;
        coorY = y;
    }

    void setX(double x){
        coorX = x;
    }

    void setY(double y){
        coorY = y;
    }

    double getX(){
        return coorX;
    }

    double getY(){
        return coorY;
    }

    public String toString(){
        String nuevo = "("+coorX+", "+coorY+")";
        return nuevo;
    }

    public class Coord3D extends myCoord{
        private double coorZ;

        Coord3D(){
            super.myCoord(); // ---> I got an error here !!
            coorZ = 1;
        }

        Coord3D(double x, double y, double z){
            super.myCoord(x,y);  ---> Also here !!
            coorZ = z;

        }

        void setZ(double z){
            coorZ = z;
        }

        double getZ(){
            return coorZ;
        }

    }

Calling the super 's constructor in Java is done by super() , either with arguments or without. In your case:

public class Coord3D extends myCoord{
    private double coorZ;

    Coord3D(){
        super();
        coorZ = 1;
    }

    Coord3D(double x, double y, double z){
        super(x,y);
        coorZ = z;

    }

    // rest of the class snipped
}

You should call methods, not constructors, using the dot ( . ) operator. Here you are calling super class' constructor using dot ( . ).

That's why you are getting errors like these:

The method myCoord() is undefined for the type myCoord

and

The method myCoord(double, double) is undefined for the type myCoord

Use these to call your super constructor: super(); and super(x,y); as shown below.

public class Coord3D extends myCoord {
    private double coorZ;

    Coord3D() {
        super(); // not super.myCoord(); its a constructor call not method call
        coorZ = 1;
    }

    Coord3D(double x, double y, double z) {
        super(x,y);  // not super.myCoord(x,y); its a constructor call not method call
        coorZ = z;
    }
}
public class myCoord {

    private double coorX, coorY;

    public myCoord(){
        coorX = 1;
        coorY = 1;
    }

    public myCoord(double x,double y){
        coorX =  x;
        coorY = y;
    }

    void setX(double x){
        coorX = x;
    }

    void setY(double y){
        coorY = y;
    }

    double getX(){
        return coorX;
    }

    double getY(){
        return coorY;
    }

    public String toString(){
        String nuevo = "("+coorX+", "+coorY+")";
        return nuevo;
    }

    public class Coord3D extends myCoord{
        private double coorZ;

        Coord3D(){
            super(); // ---> I got an error here !!
            coorZ = 1;
        }

        Coord3D(double x, double y, double z){
            super(x,y);  ---> Also here !!
            coorZ = z;

        }

        void setZ(double z){
            coorZ = z;
        }

        double getZ(){
            return coorZ;
        }

    }
super()


super(x,y);

they should be like that, you are calling 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