Constructor {0} in class {1} can not be applied to given types

2

My query is because I create a subclass (childBook) that extends from the Book class, when I rewrite the constructor in the subclass I get an error, which I do not understand:

Constructor Libro in class libro cannot be applied to given types; requered: boolean, String, String, int, int; found: No arguments; Reason: Actual and formal arguements lists differ in length

The code is as follows:

package ejerciciolibroinfantil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author jorge
 */
public class EjercicioLibroInfantil {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here

        System.out.println("Ingresee los datos correspondientes");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

       System.out.print("Titulo: ");
       String til = br.readLine();
       System.out.print("ISBN: ");
       String ISBN = br.readLine();
       System.out.print("Cantidad de paginas: ");
       int numPaginas = Integer.parseInt(br.readLine());
       System.out.print("Pagina Actual: ");
       int pagAcual = Integer.parseInt(br.readLine());
       System.out.print("Edad requerida: ");
       int edadRec = Integer.parseInt(br.readLine());

       LibroInfantil lib = new LibroInfantil(false, til, ISBN, numPaginas, pagAcual,edadRec);

      lib.abierto();
      lib.mostrarLibro();
      lib.esRecomendable(edadRec);

    }

}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ejerciciolibroinfantil;

/**
 *
 * @author jorge
 */
public class LibroInfantil extends Libro{

    private int edadRecomendado = 18;


    public LibroInfantil(boolean estado, String titulo, String ISBN,int numPaginas, int pagActual, int edadRec){

        this.estado = estado;
        this.titulo = titulo;
        this.ISBN = ISBN;
        this.numPaginas = numPaginas;
        this.pagActual = pagActual;
        this.edadRecomendado = edadRec;
    }

    public boolean esRecomendable(int edadNinyo){

        if(edadNinyo < 18)
            return true;
        return false;
    }        
}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ejerciciolibroinfantil;

/**
 *
 * @author jorge
 */
public class Libro {

    protected boolean estado = false;
    protected String titulo ="null";
    protected String ISBN = "null";
    protected int numPaginas = 0;
    protected int pagActual = 0;

    public Libro(boolean estado, String titulo, String ISBN,int numPaginas, int pagActual){

        this.estado = estado;
        this.titulo = titulo;
        this.ISBN = ISBN;
        this.numPaginas = numPaginas;
        this.pagActual = pagActual;
    }

    public void abierto(){

        this.estado = true;
        System.out.println("Libro abierto");
    }

    public void cerrado(){

        this.estado = false;
        System.out.println("Libro cerrado");
    }

    public void mostrarLibro(){

        System.out.println("Titulo del libro: "+titulo+" ISBN: "+ISBN+" Numero de pag: "+numPaginas+" Pagina Actual: "+pagActual);
    }
}
    
asked by Jorge Gonzalez 14.01.2016 в 21:04
source

2 answers

3

Maybe modifying the following class:

package ejerciciolibroinfantil;

/**
 *
 * @author jorge
 */
public class LibroInfantil extends Libro{

private int edadRecomendado = 18;

  public LibroInfantil(boolean estado, String titulo, 
                      String ISBN,int numPaginas, 
                      int pagActual, int edadRec) {

        super(estado, titulo, ISBN, numPaginas, pagActual);

        this.edadRecomendado = edadRec; //este que usted usa no se como lo usa pero hay va.
    }

    public boolean esRecomendable(int edadNinyo){

        if(edadNinyo < 18)
            return true;
        return false;
    }

try super (params):

UPDATE :( It turns out that if I had those methods to improve the format I could see that if I extended the book, then I deleted it, even so try to add super).

New :

Based on your questions in the comments take some time to analyze your code, because I saw that he used the inheritance, protect, this, well I did not understand very well why the failure / error (one can have code failures " I the first ") but I do not mean the failure but the background of the error and I think this can help.

  

The call to the constructor of the base class can be implicit

     
  • If omitted, the default constructor will be called implicitly:
  •   
  • Equivalent to put as first sentence super ();
  •   
  • If there is no default constructor in the base class it will give an error in compile time

         

    or explicit:

  •   
  • super (params); or super (); or the parameters carried by the constructor that you want to invoke from the base class will depend on the constructor to which   we invoke has or not arguments

  •   

When you create "simple" class you open experienced or you may have seen that you do not need to create the constructor;

for example:

public class TestBase {


}

and do:

TestBase testBase = new TestBase();

And create the object without any problem, because the compiled and created by you. When you create a class that class does not static it is expected that you can create instances of it.

Then you could do this too:

public class TestDerivada extends TestBase {

       public TestDerivada (String str){

       }         
}

and do:

TestDerivada test = new TestDerivada("String");

Why it works, because the constructor of the base class is implicitly invoked from the derived class, since constructor, to the constructor created by default that was mentioned before, when omitted, it is as if it were put super ();

But now if you change and add a constructor:

public class TestBase {

   public TestBase (String str){

   }
}

and it does:

TestDerivada test = new TestDerivada("String");

You will receive an error in time of compilacion (some ides warn you of this, if not your calm that the compiler already warns you), because you have to call the constructor of the base class.

"And you can say, do not call it, but it was said that it was also called by default if nothing was put in the derived class, because it was created by the compiler blah blah".

And if pero when the class in this case the base is added a constructor created by you the constructor that created the compiled is omitted, and why, because you are defining how you want comporte/logica to the time to create instances, if not, someone could instantiate it in a way for which the class was not designed.

This is a simple example, but if that class were passed to another as a parameter, and you had to take data from that clase/instancia to work, and it turns out that that data is only inicializado/creados or whatever, since the constructor that you created, it would be a problem because in the constructor por defecto these "call" variables would not be initialized or created or what you would implement in construtor que diseño (I hope it is understood).

There should be something like this:

public class TestBase {

   public TestBase (String str){

   }
}

.

public class TestDerivada extends TestBase {

       public TestDerivada (String str){
           super(str);
           //super(); <- no valdria porque no esta, ni tampoco dejalo sin ninguno por lo mismo
       }         
}

If you wanted to call it implicitly or explicitly, you would have to create it, then add one before and this other let's say that it is canceled.

It would be something like that;

public class TestBase {

   public TestBase (){

   }

   public TestBase (String str){

   }
}

Knowing that you can use it in the way you think most appropriate in your code, (in addition to what I said in the comments).

    
answered by 14.01.2016 / 21:13
source
3

The problem is that Book has only one constructor, and the parameters must be passed to that constructor. To correct it:

public class Libro{
  protected boolean estado = false;
  protected String titulo ="null";
  protected String ISBN = "null";
  protected int numPaginas = 0;
  protected int pagActual = 0;

  public Libro(boolean estado, String titulo, String isbn, 
    int numPaginas, int paginaActual){
    this.estado = estado;
    this.titulo = titulo;
    this.isbn = isbn;
    this.numPaginas = numPaginas;
    this.paginaActual = paginaActual;
  }

  public void abierto(){
    System.out.println("Libro abierto");
    this.estado = true;
  }

  public void cerrado(){
    System.out.println("Libro cerrado");
    this.estado = false;
  }

  public void mostrarLibro(){
    System.out.println("Titulo del libro: "+titulo+" ISBN: "+ISBN+" Numero de pag: "+numPaginas+" Pagina Actual: "+pagActual);
  }
}

Then in LibroInfantil:

public class LibroInfantil extends Libro{
  private int edadRecomendada = 18;

  public class LibroInfantil(boolean estado, String titulo, String ISBN,
    int numPaginas, int pagActual, int edadRec){

    //aquí es donde la magia ocurre ya que es aquí
    //en donde llamamos al constructor de la clase
    //madre:
    super(estado,titulo,ISBN,numPaginas,pagActual);

    //inicializamos el valor que no está en la clase
    //madre:
    this.edadRecomendada = edadRec;
  }

  public boolean esRecomendable(int edadLector){
    return edadLector < this.edadRecomendada;
  }
}

And that's it. Another is that you can define a default constructor in the Book class, something like public Libro(){} and it would be more than enough.

    
answered by 14.01.2016 в 21:40