0% found this document useful (0 votes)
61 views19 pages

CS 121 - Object Oriented Programming (Lec 9)

The document discusses object oriented programming and its pillars - abstraction, encapsulation, inheritance, and polymorphism. It specifically focuses on inheritance and encapsulation. For inheritance, it describes generalization and specialization using examples. For encapsulation, it explains how data and functions are combined into one unit using access modifiers like public, private, and protected. Private members can only be accessed within the class, while protected members can be accessed within the class and subclasses.

Uploaded by

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

CS 121 - Object Oriented Programming (Lec 9)

The document discusses object oriented programming and its pillars - abstraction, encapsulation, inheritance, and polymorphism. It specifically focuses on inheritance and encapsulation. For inheritance, it describes generalization and specialization using examples. For encapsulation, it explains how data and functions are combined into one unit using access modifiers like public, private, and protected. Private members can only be accessed within the class, while protected members can be accessed within the class and subclasses.

Uploaded by

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

CS 121

Object Oriented
Programming
Lecture 09
By: Syed Shahrooz Shamim
Object Oriented
Programming
Pillars of
Abstraction
OOP Encapsulation
Inheritance
Polymorphism
Inheritance
Pillars of OOP
• Approaches
Abstraction • Generalization
• Specialization
Encapsulation
Inheritance
Polymorphism
Generalization
• In generalization, a number of entities are brought together into one
generalized entity based on their similar characteristics.
• For example, Cow, Ox, and Goat can all be generalized as Animals.

Cow Ox Goat

Animals
Specialization
• A group of entities is divided into sub-groups based on their
characteristics. Specialization is the just opposite of generalization.
• For example a person can be identified as student and teacher.

Person

Student Staff Faculty


Class Relationship
• Inheritance
• Association
• Aggregation
• Composition
Class
Relationship
Encapsulation
• Mechanism by which we combine data and
Pillars of OOP the functions that manipulate the data into
one unit
Abstraction
• Objects & Classes enforce encapsulation
Encapsulation
Inheritance
Polymorphism
Pillars of OOP Encapsulation

Abstraction
Encapsulation Encapsulation = Method(Public) + Data(Private)

Inheritance
Polymorphism
Access Modifiers
• Everything that comprises a class is associated with an access
modifier that determines the scope or visibility of the entity.
• Each modifier has a few basic guidelines geared toward designing
robust, object-oriented code.
• We have four basic modifiers:
• Public
• Private
• Protected
• Friend.
Access Modifiers
• Figure shows the relationships between the four basic modifiers:
Public, Private, Protected, and Friend.
Access Modifiers
Public
• Public classes are visible to everyone and not restricted in any way.
Public members are visible inside and outside the class in which they
were declared.

• Member variables are not typically defined with public scope because
this definition would allow unrestricted access to an object's state. In
solid OO designs, member variables are kept private.
Public Access
• All member variables and methods are public by default in Python. So when
you want to make your member public, you just do nothing:

class Cup:
#Driver Code
def __init__(self):
self.color = None
redCup = Cup()
self.content = None
redCup.color = "red"
def fill(self, beverage): redCup.content = "tea"
self.content = beverage redCup.empty()
def empty(self): redCup.fill("coffee")
self.content = None
Access Modifiers
Private
• Private classes can only be nested within another class and are
completely restricted outside of that scope; it is not possible to
declare a standalone class with this modifier. Private classes
encapsulate functionality that is specific to the class where they were
declared.

• Private member variables are accessible only within the current class
where they were declared. Even derived classes do not have access to
Private members; they can be accessed through.
Private Access
• By declaring your data member private you mean, that nobody should be able to access it
from outside the class.
• Python supports a technique called name mangling. This feature turns every member name
prefixed with at least two underscores and suffixed with at most one underscore into.

class Peak:
def _single_method(self):
pass
def __double_method(self): # for mangling
pass
class Pyth(Peak):
def __double_method(self): # for mangling
pass
Protected Access
• Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses.
How to accomplish this in Python?
• The answer is _ by convention. By prefixing the name of your member with a single underscore,

class Cup:
def __init__(self):
self.color = None #Driver Code
# protected variable
self._content = None cup = Cup()
def fill(self, beverage): cup._content = "tea“
self._content = beverage print(cup.color, cup._content)
def empty(self):
self._content = None
Access Modifiers

You might also like