How to use a concrete class method created from an interface?

Asked

Viewed 124 times

1

In class Lawyer implemented all Interface properties and methods Ipessoafisica, and some other class-specific properties and methods Lawyer, how to Advocate() and Oab.

How to access this method? Since in the creation of the object Lawyer i created as Testemethod below:

public interface IPessoaFisica
{
    int PessoaId { get; set; }
    string Nome { get; set; }
    IEndereco Endereco { get; set; }
    IContato Contato { get; set; }
    string Cpf { get; set; }
    string Identidade { get; set; }
    char Sexo { get; set; }
}

//Classe Concreta
public class Advogado: IPessoaFisica
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
    public IEndereco Endereco { get; set; }
    public IContato Contato { get; set; }
    public char Sexo { get; set; }
    public string Identidade { get; set; }
    public string Cpf { get; set; }
    public string Oab { get; set; }

    public void Advogar()
    {
        //Fazer qualquer coisa
    }
}

public void TestMethod1()
{
    IPessoaFisica adv = new Advogado();
    adv.Cpf = "111.111.111-11"; //OK
    adv.Nome = "José da Silva"; //OK
    adv.Advogar(); //ERRO - IPessoaFisica não contém definição de Advogar.
               // Método esta na classe concreta Advogado,e não está declarado na interface IPessoaFisica.
}

1 answer

5

As much as your object is built as Advogado he is declared as IPessoaFisica which makes accessible only the interface methods IPessoaFiscia. To gain access to the method Advogar you will need to make one of the following changes:

Option 1- Declare method advocate on interface IPessoaFisica

Option 2- Change the type of the Adv variable to Advogado:

public void TestMethod1()
{
    Advogado adv = new Advogado();
    adv.Cpf = "111.111.111-11";
    adv.Nome = "José da Silva";
    adv.Advogar();
}

Interfaces are used to allow building common abstractions of objects and thus use polymorphism. This is only useful when polymorphic methods are declared in the interface because then you can operate on variables declared with the type of interfaces. When you need to use specific methods that cannot be abstracted and consequently cannot be declared in the interfaces it is necessary to operate on variables with the type of concrete implementation to allow access to its concrete methods.

  • Thank you Giuliana. I know that if I do any of the options you mentioned I will be able to access the Lawyer() method. However, the idea was to be able to "plug" into Ipessoafisica any type of person. That is, a lawyer, a judge, or any other kind of physical person. But access the specific methods of the particular class. Tb do not want to declare directly the concrete class aiming to decrease the coupling.

  • 1

    Then you could declare in your interface the method exercise officiating, then you could implement it as advocate, "judge", etc... It would work for you like this?

  • Sounds like a good idea. :)

  • @Léob Have some problems declaring the variable as IPessoaFisica and then convert to the concrete type?

  • Has if @Léob wants the Testmethod1 method to be polymorphic.

  • Always consider an interface as follows: What is common in multiple objects so that I can abstract in an interface, in this mode your interface can never have something that is unique to the final object! Very good comment @Giulianabezerra !!!

Show 1 more comment

Browser other questions tagged

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