If you want to get a property, you have to instantiate a class that has this property. So if you want CPF
, you need to instantiate DadosCLientesPF
. If you instantiate DadosClientes
you can not get the CPF simply because it does not exist in this class.
If you are using a method that receives DadosClientes
and needs to access something that is not available in it, you are doing something wrong as well. In this case the correct would be to have separate methods to deal with DadosClientesPF
, with DadosCLientesPJ
and maybe even keep what it deals with only DadosClientes
if it is useful, which I doubt.
I especially do not like the solution given in the other answer. I'm not a purist but that's not object-oriented programming. Methods should not have to know how to deal with derived objects of the type expected. When this is necessary, specialized methods must be created, otherwise if it is then created ClientesDadosEstrangeiro
would have to tinker with the implementation of this method. Depending on the scenario it may not be problematic but in others it may be. I do not know if you need to use inheritance in this case, but if you will use it, do it for the right reasons, and use it the right way.
If you have separated into two classes, treat them as different things. If the intention is to treat it as if it were the same thing, do not separate it.
But if your problem is polymorphism (not that it is the case), that is, you are instantiating the derived class but when you pass it to some method that expects the base class, then your problem is that property must be virtual . This guarantees the polymorphism and will call the property correctly, since it will interpret the object with its type originally instantiated and not by the type concretely declared in the parameter of the method. Of course this only applies if the property exists in the base class.
You may need to re-evaluate your entire modeling .