Access Modifiers in OOP

Object-oriented Programming (OOP)

Senthil Nayagan
1 min readDec 25, 2018

Overview

Access modifiers aka access specifiers that determines the scope or accessibility of classes, methods, and other members.

Encapsulation principle of object-oriented programming is managed through the use of access modifiers.

Access specifiers are specified using the following keywords in general:

  • Public
  • Private
  • Protected (derived classes and/or within same package)
  • Package

Any attempts at referencing an in-accessible member causes a compile-time error.

Use private unless we have a good reason not to.

Note that there is a way to override such protection in some languages usually via reflection.

Access modifiers are used not only to class members, but also to other code constructs with the same intention.

Benefits of Access Modifiers

Controlled Access —Prevents unauthorized direct access i.e. control who can access what.

Data Integrity — Ensures data integrity by preventing users (by means of data hiding) from setting the internal data into an invalid or inconsistent state.

--

--

Senthil Nayagan
Senthil Nayagan

Written by Senthil Nayagan

I am a Data Engineer by profession, a Rustacean by interest, and an avid Content Creator.

No responses yet