0% found this document useful (0 votes)
23 views6 pages

3 Access Modifier in OOP's

The document discusses access modifiers in object-oriented programming, specifically private, protected, and public. It explains when to use each modifier, emphasizing the importance of encapsulation and restricting access to class members. Additionally, it provides tips for choosing the appropriate access level to maintain functionality while minimizing exposure to unintended modifications.

Uploaded by

Mehul Thuletiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

3 Access Modifier in OOP's

The document discusses access modifiers in object-oriented programming, specifically private, protected, and public. It explains when to use each modifier, emphasizing the importance of encapsulation and restricting access to class members. Additionally, it provides tips for choosing the appropriate access level to maintain functionality while minimizing exposure to unintended modifications.

Uploaded by

Mehul Thuletiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3 access

OOP Interview

modifiers in
object-oriented
programming

Restrictness Accessibility Find more


interview tips ->
neetcode.io
private

Private class only

private members are accessible only within the class


where they are declared. This is the most restrictive access
level.


When to use
Instance Variables: Use private for class attributes to
protect them from outside modification
Internal Methods: Use private for methods that are only
meant to be used inside the class.

If external classes need frequent


access to the variable or method,
avoid using private as it could lead to
excessive getter/setter methods.

Find more
interview tips -> neetcode.io
protected

Protected
private
class only
classes, subclasses
protected members limit the access to the variable, method,
or constructor to the class or subclasses and (in some
languages) other classes in the same package or module.

When to use
Use protected when you want a variable or method to be
accessible to subclasses but not to classes outside the
parentage.

inherits

To use protected members, a class


must inherit the class containing the
protected methods.

Find more
interview tips -> neetcode.io
public

protected

Public private

class only

classes, subclasses

every class

public members are accessible from anywhere—within the

class, outside the class, in subclasses, and even from

different packages

Use public for classes, constants (static final variables),

and methods we expose to other code, like getters and

setters, and constructors.

Avoid making everything public, as it

exposes implementation details and

can lead to unexpected modifications

from anywhere in the codebase.

Find more

interview tips ->


neetcode.io
Tips On Choosing Access

Level

Favor Encapsulation (private by default)

Restrict access to class variables as much as

possible

Use protected for Inheritance


If a member should only be accessible within the

class hierarchy, use protected

Limit the use of publi

Follow the Principle of Least Privilege


Always choose the most restrictive access level that

still allows the necessary functionality. This also

reduces unintended functionalities.

Find more

interview tips ->


neetcode.io
What other
modifiers exist in
your language? let
us know
neetcode.io

You might also like