Super () does not work on my extends class

I am new to java and I am making a simple program, but I don’t know why I get an error message in my program when I try to use super ... Can anyone explain to me or what is my mistake because it is not accepts 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; } } 
+6
source share
4 answers

You should call methods, not constructors, using the dot ( . ) Operator. Here you call the constructor of the super class using the dot ( . ).

This is why you get errors like this:

 The method myCoord() is undefined for the type myCoord 

and

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

Use them to call the 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; } } 
+5
source

A call to the super constructor in Java is done using super() , with or without arguments. 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 } 
+10
source
 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; } } 
+2
source
 super() super(x,y); 

they should be like that, you call the constructor

+1
source

All Articles