A POCKET GUIDE TO OBJECT-ORIENTED PROGRAMMING (OOP)

Access Specifiers

Sometime we don’t want outside world and another class or object of that class to see or change some or all properties or use a method of our class.

Let’s assume we are working on an inventory management system. We have many different classes in that system including ‘Product’. And our ‘Product’ class have many different properties. For example name, sku and price. And we don’t want any other class or object to change ‘price’ of a ‘Product’. How can we program this?

Or if our ‘Product’ class has a method named ‘resetPrice()’, and we want to hide it from all other classes for whatever reason. How can we achieve or implement this?

The answer is ‘Access Specifiers’ or ‘Access Modifiers’.

Access specifier are keywords, or you can say markers, that help us to set accessibility level of a class (or class objects) and it’s methods (or functions) and properties.

Now there might be chance that we want to use different rules, restrictions or access levels for different classes, methods and properties. For example, we want to make our ‘name’ property fully accessible to all other classes and just want to restrict access to ‘price’ or only want to give read-only access to ‘price’.

Therefore, almost all OOP languages provide at least three different access specifiers as a solution to this problem.

  1. Public
  2. Private
  3. Protected

Some languages do provide some other access specifiers, but these are three common access specifiers available in almost all OOP languages.

Let’s discuss these in detail.

1. Public

If we want a class, method or property available to all other classes and methods in our program, we will declare or mark it as a public class, method or property. Therefore, ‘name’ property in our class will be declared or marked as ‘public’ in our program.

Similarly, classes and methods can also be marked as ‘public’. Because, we usually want these to be publicly available to all other classes and methods in our program.

2. Private

If we want to hide or restrict access to a method or property outside of our class or program, we will declare or mark it as ‘private’. Therefore, we will mark ‘price’ of our ‘Product’ class as private in our program.

Similarly, if we want to hide a method completely from other classes or objects, we will declare it as ‘private’. Therefore, our ‘resetPrice()’ method of ‘Product’ class will be declared as ‘private’ in our program.

We can not declare or mark a whole class as ‘private’ in our program. Because, it will fail the concept of OOP languages.

3. Protected

Protected is another keyword, that we use to provide a different level of access to a method and property marked as ‘protected’.

Think of it as an access level somewhere in middle of public and private. It usually used to implement an advance level concept of Object-Oriented Paradigm that we will discuss later.

For now, you just need to know that this is another keyword or access specifier that can be used with methods and properties of a class. We can not declare or mark a class as ‘projected’ just like we can not declare it as ‘private’.

With this, I think we can conclude our discussion on Access Specifiers. If you still have any confusion on this, please ask your questions as comments below.

Let’s move on to our next chapter. Please click on ‘Next’ to continue.

Image placeholder

Hi! I'm Zeeshan Elahi

I have compiled and prepared this content with love for people like me and you. And as you know it always take some time and extra cups of coffee to read, compile and prepare a course or manual. If you have like reading this content and want to say thanks, please consider supporting from more stuff like this.

Leave a comment

Your email address will not be published. Required fields are marked *

*

*