Is the implementation of an interface part of the encapsulation or heritage pillar?

Asked

Viewed 93 times

2

The implementation of an interface is part of the pillar of encapsulation or heritage of the POO?

As far as I know encapsulation disrespects the visibility of the attributes and methods contained in a class, but inheritance is the abstraction of a class so that we can extend it to other classes. I don’t know where the interface fits these two...

1 answer

2


Encapsulation refers to putting everything together in a capsule in the same location, so strictly the interface mechanism itself has nothing to do with that.

But you can interpret in some way that has relation in the sense that the behaviors that will be created to meet the interface contracts will be encapsulated in the object, as opposed to having an object or functions elsewhere to meet these demands.

So it’s not so simple to answer this, it fits interpretation, but strictly the interface mechanism is only to establish contracts, nothing to do with object orientation in a direct way.

To inheritance in itself also does not occur completely in the interface, but the interface is a way to create a type inheritance, ie you say that an object will have a capability and the interface will work as a taxonomic way of indicating a generalization, which is also what inheritance does. But it is good to be clear that the interface, pure, since there are languages that gave extra functionality to interfaces, does not inherit the behaviors of contracts, and even languages that have more powerful interfaces do not accept state inheritance. So the classical inheritance is not used by the interface, but again, we can say that there is some relation because it is possible to use the mechanism to establish a multiple inheritance in a certain point of view, just considering the type.

Encapsulation is not about field visibility (not attributes, that term is taught wrong around), the concept of leaving private visibility is called information Hiding. And in a way it’s a way of creating an abstraction.

Every statement about encapsulation is weird, so it’s even hard to say how wrong it is.

Inheritance is not an abstraction, these concepts are different. It is possible to have an abstract class to do inheritance, but the abstract term there is a little different, just say that it is an approximate general form of how the object should be, it is not the complete definition of the object.

In PHP the interface is a weird bug, or was, because it is a contract engine and PHP is a dynamic and weak typing language, IE, it does not care about contracts. Until this is changing, the language has added contract mechanisms at least in the syntax of the language. It’s a strange language to be born with a philosophy and ride a stick and do the opposite afterwards.

Can I tell you? OOP is more complex than people realize and few people do it right. People really appreciate something they don’t even understand. If they did OOP the right way the code would be too complex, some people who try really tend to make code more complex than it should be. OOP is useful, but until you understand everything, gain experience, because that’s what counts, it goes too long, it takes a lot of effort. Most people just want to say they do OOP because that’s being fashionable.

Read more about in Programming for interface and not for implementation, why?.

Browser other questions tagged

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