Reputation: 13
I have these classes: an abstract class called "Shape", a class Point extends Shape, and a class Circle extends Point
I can't change this relationship. What I need to do is create a constructor in Circle which receives as parameters an object Point and a double. I already created an object Point, called point I already tried this:
public Circle (Point objPunto, double valorRadio){
Pointp = new Point(objPunto.getX(), objPunto.getY());
setRadio(valorRadio);}
and
Circulo circulo2 = new Circulo(point, 3);
but it doesn't seem to work.It shows me this:
Implicit super constructor Punto() is undefined. Must explicitly invoke another constructor
.
I also tried some weird things using Super, but that didn't work either. I'm starting to think that it can be done, and if so... anyone knows why?
Thanks!
Code for Point
public class Point extends Shape {
private int x;
private int y;
public Point( int valorX, int valorY ){
x = valorX;
y = valorY;
}
public void setX( int valorX ){
x = valorX;
}
public int getX(){
return x;
}
public void setY( int valorY ){
y = valorY;
}
public int getY(){
return y;
}
public String getName(){
return "Punto";
}
public String describe(){
return "[" + obtenerX() + ", " + obtenerY() + "]";
}
}
Upvotes: 1
Views: 4174
Reputation: 10830
With the info you just gave, you should do something like this
public Circle (Point objPunto, double valorRadio) {
super.(objPunto.getX(), objPunto.getY());
}
Make you sure you understand when to call the super class' constructor though. Remember that when you are extending, it still creates the super classes, meaning if there is any information they need to create itself then you need to call super. And its important to note that super MUST be the first thing you call if you do.
Upvotes: 0
Reputation: 34367
I think you can just call super()
for first two arguments as below:
public Circle (Point objPunto, double valorRadio){
super(objPunto.getX(), objPunto.getY());
setRadio(valorRadio);
}
super()
calls the constructor of the parent class
and it should be the first statement inside the child constructor, if used.
Upvotes: 1
Reputation: 6778
Your circle is trying to call super();
as it's first step, but Point
doesn't have a default no-arg constructor. You have to call the super method that takes parameters as the first line of your Circle's constructor.
Try super(objPunto.getX(), objPunto.getY());
as the first call in the Circle constructor
Upvotes: 2
Reputation: 10794
If your class Circle
extends Point
, then it can access it's members.
There's no need to create a new Point()
:
public Circle (Point objPunto, double valorRadio){
setX(objPunto.getX());
setY(objPunto.getY());
setRadio(valorRadio);
}
Upvotes: 0