Instantiating a class "x" from another class "y" inherited by the class "x"

Asked

Viewed 74 times

2

If I have a class Diretor who inherits from the class Funcionario, so I say a direct is also an employee.

Public class Diretor : Funcionario{}

Diretor diretor = new Diretor();

But I’m allowed to instantiate

Funcionario funcionario = new Diretor();

What is the real sense if I have access to only the working class?

  • If you know how to drive a car, and you pick up a pickup truck (which is inherited by car), you have the same interface, you can drive the pickup truck normally, the idea is this.

1 answer

4


This shown this way does not make much sense, but it is a simple and obvious way to show how the mechanism works.

What most people don’t understand is just this, and probably one of the biggest reasons people learn a lot of things wrong in programming, especially object-oriented, since examples and documentation just want to show the mechanism and not give a cake recipe how it uses, but most people understand that this is exactly how it should do.

Let me give you an example: everyone teaches inheritance by showing that the dog inherits from animal, because it is an easy way to understand inheritance, Then everyone models wrong because they don’t understand what the same object is with a more abstract classification or what another completely different object is that has some point relation. So people believe that a Diretor is a Funcionario, where there are directors who are not employees and have employees who have more than one position, or even change positions and becomes a mess when it has to change these things. Funcionario is a form of hiring a Pessoa and Diretor is a position that a person has, there is no inheritance between these things, there is papers in some form of composition. See more in Heritage and Polymorphism.

But let’s follow this fictional example.

Somewhere in the application you do something specific to do with an employee, no matter what class. So you have a method that takes an object that is a Funcionario and inside does everything he has to do, remembering that he only knows what is the Funcionario and can only access elements of this type. Even because this class is inherited by other classes then the concrete object that this method is receiving may also be another more specific type. No problem because if there was inheritance it is guaranteed that this object has all that Funcionario has and the method only accesses those parts of the object, so it can receive an object that is concretely a Diretor that was created somewhere.

Something like that:

void Print(Funcionario funcionario) { ... }

It makes more sense in most scenarios. And then you’ll call it something like:

Print(new Diretor());

Realized that the object is of a type but the variable, in the parameter case, that will receive the object is declared of the base type Funcionario?

That’s the whole basis of what it is inheritance and polymorphism.

When you do:

Funcionario funcionario = new Diretor();

I put in the Github for future reference.

Are you saying that the object is the type Diretor and the variable is the type Funcionario (one builder so it seems wrong, but it’s okay if it’s just to demonstrate the mechanism, it’s only a problem if you think you should always create this way).

Is there a reason to create an object with the specific type and already say that the type of the variable is the base type instead of the object type? It has, if it is already certain that later it will be used with the base type, in the case Funcionario, but then you can use it with the more specific type.

Understand that in cases like this Funcionario is probably an abstract class and cannot be instantiated (if it is not, probably the hierarchy is even more wrong to allow having an employee who has no position even though all positions depend on being employees), so if you want the type Funcionario for what to use there would still have to instantiate through an object Diretor.

If you don’t have to do this, don’t do it. And just understand that you can only learn to apply mechanism in the right way with clear and real contexts, the artificial disengage this part. If I had a context in the question I could answer more properly, but it seems that there is no context at all except for being an artificial example.

Browser other questions tagged

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