Calling a class method

Asked

Viewed 556 times

2

How can I display the method of this revised class in the Test class?

public interface Produto {

    void exibirNome(String nome);
}

public class Revista  implements Produto {

    @Override
    public void exibirNome(String nome) {

        System.out.println("Exibindo revista de nome: " + nome);

    }


}


public class Teste {

    private Produto produto;
    String nome;

    public Teste(Produto produto) {
        this.produto = produto;

    }

    public Produto getProduto() {
        return produto;
    }

    @Override
    public String toString() {

        //Aqui eu gostaria de passar o método exibirNome da classe Revista
    }

}

And pass the toString();

Produto p = new Revista();
        Teste teste = new Teste(p);

        System.out.println(teste); //para exibir a revista
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can).

3 answers

5

I made it work:

interface Produto {
    void exibirNome(String nome);
}
 
class Revista implements Produto {
    @Override
    public void exibirNome(String nome) {
        System.out.println("Exibindo revista de nome: " + nome);
    }
}
 
class Teste {
    private Produto produto;
    String nome;
 
    public Teste(Produto produto) {
        this.produto = produto;
    }
 
    public Produto getProduto() {
        return produto;
    }
 
    @Override
    public String toString() {
        produto.exibirNome(nome);
        return nome;
    }
}
 
public class Program {
    public static void main(String[] args)  {
        Produto p = new Revista();
        Teste teste = new Teste(p);
        System.out.println(teste);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But that code doesn’t make any sense. I even thought about rewriting in a better way, but I didn’t think how, so meaningless that it is. To make it right I would have to change so much that it would be something else entirely.

This interface makes no sense, this signature induces the bad implementation, the implementation of this method in the class makes no sense, use it within the toString() doesn’t make sense, the class Teste has no sense, has this property nome that is not used in fact, even because she is loose there, and so on and so on...

  • class HelloWorld? You couldn’t name it better?

  • Also, this code will end up displaying the name twice, once on main and another inside the toString(), and probably wouldn’t be the goal (and if so, there are much better ways to get to it). And also, it is not clear what is the purpose or motivation of instantiating a Teste or what an instance of that means.

  • "the user class has no sense" - I think you meant something else. What do you mean? (A) The class Produto; (B) The class Teste; (C) The class that uses these two things together (HelloWorld); (D) All previous alternatives.

  • @Victorstafusa gives a job to use another name in Codingground. Does this code have any purpose? Let’s agree that it makes no sense to do any of this. I tidied up.

  • Yes this code has an objective: To serve as a pure, perfect and legitimate example of the use of Disoriented Object Programming.

  • @Victorstafusa at this point I agree :)

Show 1 more comment

3

Following the principles of Herança and of Polimorfismo, your class Revista implements the interface Produto and therefore its abstract methods. Thus, in its class Teste that has a product, just you make the following call.

produto.exibirNome();

Thus the attribute produto class Teste which is a Revista, will behave following the implementation of the Revised class method. This is known as polymorphism. As in the example below:

public interface Animal {  
    public void fala();
}


public class Homem implements Animal{

    @Override
    public void fala() {
        System.out.println("Olá Mundo!");
    }
}

public class Gato implements Animal{

    @Override
    public void fala() {
        System.out.println("Miau!");
    }    
}

public class Polimorfismo {

    public static void main(String[] args) {
        Animal homem = new Homem();
        Animal gato = new Gato();

        homem.fala();
        gato.fala();
    }

}

That’s the way out

Olá Mundo!
Miau!

NOTE: To get to the effect I believe you want to put an attribute String class name Revista and modify the method

public void exibirNome(String nome)

for

public void exibirNome()

Thus becoming part of the final code

    public interface Produto {

    void exibirNome();
}

public class Revista  implements Produto {
    String nome;
    @Override
    public void exibirNome() {
            System.out.println("Exibindo revista de nome: " + nome);

    }
}

1

In this format, to call a method within any other class you need to do so:

new Revista().exibirNome("Mah oi!");

I suggest you take a look at Java access modifiers to increase your knowledge.

Browser other questions tagged

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