-1

i have a question that is haunting me for a long time. As you know there are some kind of encapsulation methods like protected, private or public. But the question is that why do we use protected or what or who do we protect these classes from? Basically why do they need to be protected? I learned that we use private when we dont want to access that object from other classes but what is the harm of that? What will happen if other classes access that object?

Please enlighten me

I really have no idea

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

4 Answers4

1

Public members are accessible from any class or method, private members are only accessible from the same class, and protected members are accessible from the same class and its derived classes.

The use of protected members is to provide access to derived classes, while still hiding the implementation details from other classes. It is a way of restricting the direct access to the member, allowing it to be modified only by the base class and its derived classes.

The main purpose of using protected access modifier is to provide data security, to prevent any unintentional modifications from other classes that could potentially break the class’s functionality or corrupt the data.

In summary, using protected access modifier helps to maintain data integrity and restricts access to class members to only derived classes, while still allowing for modification of those members within the derived classes.

Snell
  • 89
  • 8
0

You have to imagine the whole thing like an interface to the respective class. Public releases interfaces from for external. Private closes the interface and methods and member variables can only be used within the class. With Protected you can keep the class open within an inheritance structure but close it to external access.

Why is this done? To protect the logic of your software. When you develop software and make abstractions, it makes sense to protect certain methods and variables from modifications in order not to violate the overall logic. On the other hand, you can explicitly open methods for access from outside. Because it is desired at this point.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
0

Think of it as Levels of access.

As you pointed out Private is used to protect the object from external access. You must also be knowing about the Public keyword which essentially allows access to all the members to external classes without any restriction.

But here's the issue Private is way too much restrictive and Public is way too open in a way they allow access.

That would be nice if we have a Keyword that is somewhere in the middle right? this is where Protected comes into the picture.

Protected members can only be accessed by the class itself or the derived classes.

Coming to your last question: What is the harm in accessing those objects?

The main reason, as a class, you are not supposed to know what the other class does. primarily due to security and design issues. Simple example let's suppose you have an SDK of any bank which your application uses. and now imagine you are able to alter its private members suppose recipient, amount, account no. imagine how damaging it is. Thus the restricted access.

Hope this helps and gives clarity with real-life example.

Suchandra T
  • 569
  • 4
  • 8
0

When declaring class members as protected or private, we protect classes from depending on each others implementation. It helps a lot in future, when you need to change the details. When not using encapsulation, any classes that depend on the first class private members have to be changed too.

For example, it's a good practice to make getters public and its underlying fields private. If after some time you move that field to another class or change its type, all you have to do to get things back to work is to change the implementation of the getter.

In my experience, protected are not used often. The first scenario that comes to mind is template method pattern. The template method is a method in abstract superclass that defines the skeleton of an operation in terms of a number of steps. These steps are protected helper methods declared in the base class. Its derivatives overload them to provide concrete implementation.

To sum up, template method pattern needs protected (not private), because subclasses must access methods that represent steps.