CS 121 - Object Oriented Programming (Lec 9)
CS 121 - Object Oriented Programming (Lec 9)
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
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