0% found this document useful (0 votes)
19 views

OOP Part 1

Uploaded by

daisyrechavez7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

OOP Part 1

Uploaded by

daisyrechavez7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming Essential Course

Object-Oriented Programming
● Object-oriented technology models real-world objects.
● Real-world objects are things around you, such as:
table, television, chair, etc.

Class Object

Inheritance Polymorphism
OOP

Data
Encapsulation Abstraction
Advantages of OOP

• Object-oriented programming fosters reusability.

• The modular approach used in object-oriented programming


results in highly maintainable code.

• In object-oriented programming, every class has a specific task.

• Data encapsulation adds an extra layer of security to the program


developed using the object-oriented approach.
What is a Class
A class can be defined as a blueprint,
description,
or definition of a particular classification of
objects.

What is an Object
An object (instance) is an instantiation of
a class.
Defining a Class in Python class Person:
name = str()
age = int()
class classname:
statements..... def set_details(self, n, a):
self.name = n
self.age = a

def display(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
print(f"Is {self.name} an adult?:
",end="")
The __init__ function
The __init__() function/method is called automatically every time
the class is being used to create a new object.
class Person:
def __init__(self, name,
age):
self.name = name
self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)
def __init__(self,
name,age,address):
self.pangalan = name
self.edad = age
self.lugar = address

student = Person("John","30","Iloilo
City")
print(f"My name is
{student.pangalan} ")
print(f"Im {student.edad} years
old")
print(f"I live in {student.lugar} ")
Object Methods

Objects can also contain methods. Methods in objects are functions


that belong to the object.
class Person:
def __init__(self, name, age):
self.pangalan = name
self.edad = age

def myfunc(self):
print("Hello my name is " +
self.pangalan)

p1 = Person("John", 36)
p1.myfunc()
Access Modifiers

Python uses ‘_’ symbol to determine the access control for a specific
data member or a member function of a class. Access specifiers in
Python have an important role to play in securing data from
unauthorized access and in preventing it from being exploited.

A Class in Python has three types of access modifiers:

● Public Access Modifier


● Protected Access Modifier
● Private Access Modifier
Private Access Modifiers

The members of a class that 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.

__name = “John Doe”

def __greet():
print(“hello”)
Protected Access Modifiers

The members of a class that 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.

_name = “John Doe”

def _greet():
print(“hello”)
Public access modifiers
All the class members can be accessed anywhere inside or
outside the class,
All data members and member functions of a class are public
by default.
class Computer:
def __init__(self):
self.__maxprice = 900
Encapsulatio
def sell(self):
print(f"Selling Price: {self.__maxprice}")
n
def setMaxPrice(self, price): - can restrict access
self.__maxprice = price
to
comp = Computer() methods and
comp.sell() variables.
print("==================")
- it prevents data
# change the price directly
comp.__maxprice = 1000 stored
comp.sell() inside the
# using setter function attributes
print("==================")
comp.setMaxPrice(1000) from direct
comp.sell() modification
Data Encapsulation
Create a class BankAccount that encapsulates the
details of a bank account. The class should have the
following features:

1. A private attribute __balance to store the account balance.


2. A method deposit(amount) to add money to the account. This
method should not allow negative amounts to be deposited.
3. A method withdraw(amount) to withdraw money from the
account. This method should not allow withdrawal of more
than the available balance or negative amounts.
4. A method get_balance() to check the current balance.
Test Cases
1.test_initial_balance:
•Create an account with an initial balance of 100.
•Check if the balance is correctly set to 100.
2.test_deposit:
•Create an account with an initial balance of 100.
•Deposit 50 and check if the balance updates to 150.
•Try to deposit a negative amount (-20) and verify that the balance remains unchanged at 150.
3.test_withdraw:
•Create an account with an initial balance of 100.
•Withdraw 50 and check if the balance updates to 50.
•Attempt to withdraw more than the available balance (200) and verify that the balance remains
unchanged at 50.
•Attempt to withdraw a negative amount (-20) and verify that the balance remains unchanged at 50.
4.test_get_balance:
•Create an account with an initial balance of 200.
•Verify the balance is 200.
•Deposit 100 and check if the balance updates to 300.
•Withdraw 50 and check if the balance updates to 250.
# parent class
class Bird:
def __init__(self):
print("Bird is ready") Inheritance
def whoisThis(self):
print("Bird")
- creating a new class copying all the
def swim(self):
print("Swim faster") details of
# child class an existing class without modifying it
class Penguin(Bird):
def __init__(self):
# call super() function Parent Class (Base
super().__init__() Class)
print("Penguin is ready")
def whoisThis(self): methods attribute
print("Penguin") s
def run(self):
Derived Class (Child
print("Run faster")
Class)
peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
class Parrot:
def fly(self): Polymorphism (Many
print("Parrot can fly")
def swim(self): forms)
print("Parrot can't swim")
- The ability to use a common interface for accessing
class Penguin:
def fly(self):
print("Penguin can't fly") multiple forms
def swim(self): - Same method name with different definitions
print("Penguin can swim") (signatures) being used for different forms.
#common interface Interface
def test_fly(bird):
bird.fly()
def test_swim(bird):
bird.swim()
#object instantation or object creation
chox = Parrot()
peng = Penguin() Form 1 Form 2 Form 3 Form 4
test_fly(chox)
test_fly(peng)
test_swim(chox)
test_swim(peng)

You might also like