简体   繁体   中英

Overload the constructor of a subclass

I'm trying to make a constructor that takes 2 elements of type Punto and assigns it to the instance variables, in my superclass I already have a constructor but I want one more in my subclass so first in my subclass I call the superclass constructor and then I try to add one more with the following error:

constructor in class cannot be applied to given types.

Superclass:

public class Poligono  implements Figura  {

Punto[] vertici;

public Poligono(Punto[] vertici) throws IndexOutOfBoundsException {
    if(vertici == null || vertici.length<3) {
        throw new IndexOutOfBoundsException();
    } 
    this.vertici = vertici;
}

Subclass:

package figura;

import punto.Punto;


public class Rettangolo extends Poligono{

   Punto p1;
   Punto p2;

   public Rettangolo(Punto[] vertici) throws IndexOutOfBoundsException {
       super(vertici);
   }

   public Rettangolo(Punto p1, Punto p2) throws NullPointerException{

       if(p1==null || p2==null) throw new NullPointerException();

       this.p1 = p1;
       this.p2 = p2;
   }

in my second constructor i get the error:

constructor Poligono in class Poligono cannot be applied to given types;
required: Punto[]
found: no arguments
reason: actual and formal argument lists differ in length

Your public Rettangolo(Punto p1, Punto p2) must call some constructor of the super class Poligono . The compiler complains that the only constructor available - Poligono(Punto[] vertici) - doesn't fit the parameters of the second Rettangolo constructor.

You have to explicitly call the Poligono constructor with the super(..) call.

Assuming that the two points passed to that constructor are two opposite corners of a rectangle, and that the Poligono constructor expects an array of vertices, you need something like this :

public Rettangolo(Punto p1, Punto p2) {

    super (new Punto[]{p1,
                       new Punto (p1.getX(),p2.getY()), 
                       p2, 
                       new Punto (p2.getX(),p1.getY())});

    this.p1 = p1;
    this.p2 = p2;
}

I may have gotten the order of the points or the method names wrong.

Why does this() and super() have to be the first statement in a constructor?

What you can do (besides calling super() ) is

public Rettangolo(Punto p1, Punto p2) {
     super(new Punto[] {p1, p2});
}

I'd prefer this solution, because if you use Rettangolo(Punto[]) you do not have any points to work with. So either you read the points out in your first constructor, eg,

public Rettangolo(Punto[] vertici) throws IndexOutOfBoundsException {
   super(vertici);
   p1 = vertici[0];
   p2 = vertici[1];
}

or you do not work with single points and only use the array instead.

There is no superclass constructor which will be called if you initialize 'Rettangolo' object.

Whenever a object is created the parent class state is initialized before child classes.

The first line of the constructor is either call to the parent constructor or call to the another constructor of the same class, which will call the parent constructor.

Now your case :

public Rettangolo(Punto p1, Punto p2) throws NullPointerException{

   if(p1==null || p2==null) throw new NullPointerException();

   this.p1 = p1;
   this.p2 = p2;

}

Your constructor is trying to call the default constructor but since there is no such constructor defined, error occurred.

Either you define a default constructor to the parent class and initialize Punto p1, p2 but then vertici[] array will be null.

Or you can do

public Rettangolo(Punto p1, Punto p2) throws NullPointerException{
   super(new Punto[] {p1,p2});
   if(p1==null || p2==null) throw new NullPointerException();

   this.p1 = p1;
   this.p2 = p2;
}

But then it depends on your requirements. You just need to remember that parent class constructor will always be called first.

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