How to resolve "There is no default constructor in ..." error in Java?

Asked

Viewed 183 times

2

I have an abstract class called Veiculo which has two builders:

public abstract class Veiculo {

    // Atributos ...

    public Veiculo(Marcas marca, int velocidade){...}

    public Veiculo(Marcas marca, int velocidade, Cores cor){...}

    // Métodos abstratos ...
}

Besides this class, I have another call Carro, extending the abstract class Veiculo and implements all methods declared as abstract.

But the class Carro do not overwrite the constructors because, I wish to use the constructors of the inherited class in the class Carro and in other classes that I create and extend Veiculo.

Carro veiculo = new Carro(Marcas.FERRARI, 420, Cores.VERMELHO);

Below is the class code:

public class Carro extends Veiculo {

    public String abrirJanela(){...}

    public String dirigir(){...}

    public String getMarca(){...}

    public String quebrar(){...}
}

The problem that is happening is that the Intellij IDEA shows me the following error message on the line on which I extend the class:

There is no default constructor available in 'vehicle.Vehicle'

What I’m doing wrong in my code?

2 answers

4


The first and main mistake is to think that there is a builder’s inheritance. It does not exist, you have to create builders of all classes, and I strongly advise doing this. Builders are static methods, it is not possible to make inheritance, it makes no sense, and mccada type has a need.

What you can do is in the child constructor invoke the mother class constructor in a simplified way, something like this:

public Carro(Marcas marca, int velocidade, Cores cor) {
    super(marca, velocidade, cor);
}

I put in the Github for future reference.

Even if you wish not to have any constructor it is mandatory to have at least the default constructor that is the minimum necessary to build an object.

A possible mistake would be in the mother class. Every class must have at least the default constructor, which is a constructor without parameters, so the object can be instantiated through this constructor. You usually don’t need to create it, the compiler creates it for you. Except if you create other constructs with parameters, then the default constructor is not created, and as has other builders it is not usually necessary even, in general nor should exist because there are better builders.

It may be that you do not want to create this builder, it is common to make sense not to have it, and I think it is ideal in this case, even not knowing much about the problem. There’s a question that explains it better: What good is a builder?.

The problem is occurring because the daughter class does not have a constructor that calls the constructor of the class in a specialized way, more or less as I showed above, the only way that the compiler can call the constructor of the mother class for you is through the default constructor, that doesn’t exist and makes that mistake.

I am not guaranteeing, but everything indicates that should create the builders of Carro and the error disappears. I do not guarantee because all this example to have problems, even solving this will be learning OOP the wrong way.

0

After breaking my head a lot and researching a lot, I discovered that the error was caused by the class builder Veiculo was implicitly called, without passing the parameter.

If my constructor does not receive any parameter this error would never happen, but my constructor does. What I should do is create the same builders for the class Carro with the same parameters and in the body of the function, use the super. Example:

public class Carro extends Veiculo{

    public Carro(Marcas marca, int velocidade){
        super(marca, velocidade);
    }

    public Carro(Marcas marca, int velocidade, Cores cor){
        super(marca, velocidade, cor);
    }
}
  • 2

    To supplement his reply, this behaviour is described in the second to last paragraph of documentation. It’s a normal behavior :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.