Doubt about class inheritance in Java

3

I have the following code a ebook that inherits the class Livro (superclass).

However, I can not create a new ebook and setar in the name, when I put the main method ( main ) it gives error.

Class Autor :

public class Autor {
    private String nome;
    private String cpf;
    private String email;


}

Class Livro :

public class Livro {
    private String nome;
    private String descricao;
    private double valor;
    private String isbn;
    private Autor autor;
    private boolean impresso;


    public Livro(Autor autor) {
        this.autor = autor;
        this.isbn = "00-000-0000-00";
        this.impresso = true;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String nome() {
        return nome;

    }

    public boolean aplicaDescontoDe(double porcentagem) {
        if(porcentagem > 0.3) { //for maior que 30%
            return false; //retorna falso;
        } else if (!this.impresso && porcentagem > 0.15){ //se livro digital e for maior que 15% de desconto
            return false; //retorna falso

        }
        this.valor -=this.valor *porcentagem; 
        return true;
    }



}

Class Ebook :

public class Ebook extends Livro { 


    private String waterMark;

    public Ebook(Autor autor) {
        super(autor); //superclasse
    }

    public void setWaterMark(String weterMark) {
        this.waterMark = waterMark;

    }

    public String getWaterMark() {
        return waterMark;
    }




}

I can not call main in class Ebook and create a new object ebook , and set the name, and it gives error.

Ebook ebook = new Ebook() {
ebook.setNome("Bla bla bla");
}
    
asked by anonymous 05.07.2015 / 18:41

3 answers

2

Apparently your class modeling is correct, the problem that causes the error is the way you are trying to set the content of nome , since it is using an invalid syntax.

For this problem, instead of this:

Ebook ebook = new Ebook() {
    ebook.setNome("Bla bla bla");
}

Use this:

Ebook ebook = new Ebook();
ebook.setNome("Bla bla bla");

Other comments on your code, but do not generate errors, just behaviors:

  • In this section you are not reconfiguring the value of waterMark . You should change the parameter from weterMark to waterMark OR change the assignment form, change this:
public void setWaterMark(String weterMark) {
    this.waterMark = waterMark;
}

For this:

public void setWaterMark(String waterMark) {
    this.waterMark = waterMark;
}

A final example would be this:

  • class Autor continues unchanged;
  • class Livro : included default constructor (no arguments), then you can new Ebook() without passing instance Autor ;
public class Livro {

    private String nome;
    private String descricao;
    private double valor;
    private String isbn;
    private Autor autor;
    private boolean impresso;

    public Livro() {}

    public Livro(final Autor autor) {
        this.autor = autor;
        isbn = "00-000-0000-00";
        impresso = true;
    }

    // getter e setters

    // demais métodos que precise

}
  • class Ebook : added default constructor (without arguments) that calls the default constructor of Livro ;
public class Ebook extends Livro {

    private String waterMark;

    public Ebook() {
        super();
    }

    public Ebook(final Autor autor) {
        super(autor); // superclasse
    }

    // getter e setters

    // demais métodos que precise

}

Finally, an example of class Main would be like this, creating instances of Ebook already informing the construction Autor and not:

public class Main {

    public static void main(final String[] args) {
        final Ebook ebook = new Ebook();
        ebook.setNome("Bla bla bla");

        final Autor autor = new Autor();
        autor.setNome("Nome do Autor");
        autor.setEmail("[email protected]");
        final Ebook ebook2 = new Ebook(autor);
        ebook2.setNome("Bla2 bla2 bla2");
    }

}
    
05.07.2015 / 18:51
1

Instead of:

Ebook ebook = new Ebook() {
    ebook.setNome("Bla bla bla");
}

Use, without { and } :

    Ebook ebook = new Ebook(autor);
    ebook.setNome("Bla bla bla");

You forgot to create a new author and pass it to the Ebook builder Autor :

    Autor autor = new Autor();
    autor.nome = "Felipe";
    autor.cpf = "333.333.333-22";
    autor.email = "[email protected]";

    Ebook ebook = new Ebook(autor);
    ebook.setNome("Bla bla bla");

Note also that your author has private properties and in this case, it should be public for you to create a new author:

public class Autor
{
    public String nome;
    public String cpf;
    public String email;
}
    
05.07.2015 / 18:58
0

You have to set attributes as protected and not private as you are doing, otherwise herança does not act on atributo .

And to set you should do:

Ebook ebook = new Ebook();
ebook.setNome("bla bla bla");
    
05.07.2015 / 19:06