0% found this document useful (0 votes)
3 views16 pages

Week No 8

The document discusses access modifiers in object-oriented programming, specifically in Python, highlighting public, protected, and private access levels. It explains how these modifiers control access to class variables and functions, using underscores to denote access levels. Public members are accessible from anywhere, protected members are accessible within the class and its subclasses, and private members are only accessible within the class itself.
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)
3 views16 pages

Week No 8

The document discusses access modifiers in object-oriented programming, specifically in Python, highlighting public, protected, and private access levels. It explains how these modifiers control access to class variables and functions, using underscores to denote access levels. Public members are accessible from anywhere, protected members are accessible within the class and its subclasses, and private members are only accessible within the class itself.
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/ 16

CS200 Object Oriented

Programming
1

Ms. Sunbal Rani


Week No 8
2 “Public and private access
specifiers”
3 Access Modifiers in Python Public, Private
and Protected
• In object-oriented programming languages like C++, Java, and
Python, access modifiers are used to control who can use or see the
variables and functions inside a class. This helps keep the data safe
and organized.
• Most languages have three types of access:
• Public: anyone can access it.
• Protected: only the class and its child classes can access it.
• Private: only the class itself can access it.
4 Access Modifiers in Python Public, Private
and Protected
In Python, we use underscores to show access levels:
• A single underscore (_name) means it’s protected (should not be used outside the
class, but can still be accessed if needed).
• A double underscore (__name) means it’s private (not meant to be accessed
outside the class at all).
• These access rules help protect the data inside a class from being changed or used
in the wrong way.
5 Access Modifier Public

• In a class, when something is marked as public, it means any part of


the program can use it or change it, there are no restrictions.
• By default (if you don’t specify anything), all variables and functions
in a class are public, they can be accessed from anywhere in the
program.
6 Access Modifier Public
7 Access Modifier Protected

The members of a class which are declared protected are only accessible
to a class derived from it. Data members of a class are declared
protected by adding a single underscore ‘_’ symbol before the data
member of that class.
8 Access Modifier Protected
9 Access Modifier Protected

In the above program, _name, _roll and _branch are protected data
members and _displayRollAndBranch() method is a protected
method of the super class Student. The displayDetails() method is a
public member function of the class Geek which is derived from
the Student class, the displayDetails() method in Geek class
accesses the protected data members of the Student class.
10 Access Modifier Private

The members of a class which are declared private are accessible within
the class only, private access modifier is the most secure access modifier.
Data members of a class are declared private by adding a double
underscore ‘__’ symbol before the data member of that class.
11 Access Modifier Private
12 Access Modifier Private

In the above program, _name, _roll and _branch are private


members, __displayDetails() method is a private member function (these
can only be accessed within the class)
and accessPrivateFunction() method is a public member function of the
class Geek which can be accessed from anywhere within the program.
The accessPrivateFunction() method accesses the private members of
the class Geek.
13 Access Modifier Public, Protected &Private
14 Access Modifier Public, Protected &Private
15 Access Modifier Public, Protected &Private
Thank You
16

You might also like