Doubt about class inheritance in Java

Asked

Viewed 107 times

3

I have the following code one ebook who inherits the class Livro (superclass).

However, I can’t create a new ebook and set in name, when I put the main method (main) makes a mistake.

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’t call the main in class Ebook and create a new object ebook, and set the name, and gives error.

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

3 answers

2


Apparently your class modeling is correct, the problem that makes the error is the way you are trying set the content of nome, since you are using a non-valid 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 remarks regarding your code, but that do not generate errors, only behaviors:

  • in this stretch you are not reconfiguring the value of waterMark. You must change the parameter of weterMark for 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 remains unchanged;
  • class Livro: included default constructor (no arguments), so you can do new Ebook() without passing instance of 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 (no arguments) which calls the default constructor from 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 thus create instances of Ebook informing already in 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");
    }

}
  • Error it, I create the object in the Ebook class? And the main one too? because the set and get of the name(private) is in the Book class

  • @Leonardinho already updated the answer, missing some builders also

  • @Leonardinho updated the answer, see if you have any questions about modeling =)

  • Okay, settled...

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, should be public for you to create a new author:

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

0

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

And to set you must do:

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

Browser other questions tagged

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