Why hide the implementation of a class?

Asked

Viewed 223 times

7

This is the concept I’ve worked hard to understand, but I’ve never fully achieved.

I’ll take this excerpt from a Deitel book to illustrate:

It is a better software engineering define member functions outside the class definition. This practice ensures that programmers do not write a client code that depends on your class implementation. If they needed to do that, the client code probably 'would break' if the class implementation were changed.

I cannot understand how it would be possible for the client to write a code that depends on the class implementation, since the local variables of the method and the fields private of the class are inaccessible in any way.

Could someone illustrate a practical example of code in which this could happen? An example library method exposed in which the user would use something specific to the implementation of this method in his code and then a possible change to that implementation by the creator of the library that would break client code. I need an example to understand. I’ve seen millions of conceptual explanations, but none made sense to me.

I find it even, in my layman’s view, more interesting to make implementations visible since the programmer may be curious to know how such a method works, just as one might wonder why when pressing a radio button, the station changes, for example.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

7

This is about the open-close of SOLID. Whenever you put a public behavior, or even protected in a class and allow this class to be inherited you have a responsibility with that, any change has to be very well thought out so that the inherited classes are not affected by a change you make in the class.

In a way, it has to do with liskov principle since you do not only need to respect the contract established in superclass, needs to ensure that the behavior is as expected by those who inherited it.

But if you change in a way that doesn’t break something then it’s okay.

So the fewer things you put into the class the greater the chance of something breaking. One way is to put certain features into utility classes, another is to create static members in the class itself. One way that helps a little is to have nonvirtual members, so at least these members can not be inherited. But the most effective is to put out because there is more freedom to move as you want.

One of the problems occurs when you add a new method. Imagine that the descending class may have a method of it with a identical signature. Which one should it perform? Things begin to get less predictable.

Note that everything private is of no importance. Perhaps the doubt has arisen from this, what the book talks about is the public members only. In the private part do as you wish, the only responsibility is with the class itself that you must have full control. So it all didn’t make sense until now because I was misinterpreting the concept.

Making the source visible has nothing to do with making it public. Want to leave the source open, let it, this has nothing to do with the concept of visibility of members of a kind that is something internal to the code and not how you manage the project as a whole. Decrease visibility is not about protecting the source.

  • I reflected a lot about your post and the links listed on it and yet it seems kind of obscure. I understand the concept of Open Closed, but I still don’t understand why when we analyze a library file we don’t see the implementations of its methods. If the class is blocked for editing and enabled for extension, what is the problem with having its implementation exposed, since it cannot be changed and only overwritten?

  • 2

    The problem is that you are guessing things that no one is saying. Classes can be edited. Only it must be done carefully. A lot you can’t do. If you apply O/C in a fundamentalist way, as many do, in fact it doesn’t matter, but the pragmatists know that this is unfeasible, then they make the cleaner classes so they don’t have to be fundamentalists. The question was posed by the pragmatists. Fundamentalists will think as you do now. Fundamentalists never understood OOP, but they swear that only they understood. After all are fundamentalists, it is part.

  • In fact, I must be looking at this from the wrong angle. It’s only with the maturing of all these concepts over time that maybe I look at it correctly and finally understand what I believe to be something relatively simple. I will leave the question open for now and come back another day, with a more accurate conception on the subject. I appreciate the help anyway.

  • 2

    @Eduardom if you think you should, see what you can do on the [tour].

Browser other questions tagged

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