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

Lesson 8 - OOP

This lesson on Object Oriented Programming (OOP) in Python covers the fundamental concepts including the paradigm of OOP, its four pillars (encapsulation, inheritance, abstraction, and polymorphism), and the creation of classes and instances. Students will learn to define classes, create objects, and implement the __init__() method for initializing attributes. The lesson emphasizes the advantages of OOP, such as improved code structure, reusability, and maintainability.

Uploaded by

fredagbam
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 views5 pages

Lesson 8 - OOP

This lesson on Object Oriented Programming (OOP) in Python covers the fundamental concepts including the paradigm of OOP, its four pillars (encapsulation, inheritance, abstraction, and polymorphism), and the creation of classes and instances. Students will learn to define classes, create objects, and implement the __init__() method for initializing attributes. The lesson emphasizes the advantages of OOP, such as improved code structure, reusability, and maintainability.

Uploaded by

fredagbam
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/ 5

GET214: COMPUTING AND SOFTWARE ENGINEERING

(Programming in Python)
LESSON 8: OBJECT ORIENTED PROGRAMMING (OOP)
Learning Outcomes:
At the end of the lesson, students should be able to:
1. Describe the paradigm of Object Oriented Programming (OOP)
2. Describe the “pillars” of OOP
3. Create a class with instance attributes, class attributes, and the __init__()
method.
4. Use a class definition to create class instances to represent objects.
5. Create and implement __init__() with multiple parameters including default
parameter values.

8.1 Introduction to Object Oriented Programming (OOP)

Procedural programming is about writing procedures or functions that perform


operations on the data, while object-oriented programming is about creating objects
that contain both data and functions. Object-oriented programming (OOP) in Python
helps you structure your code by grouping related data and behaviors into objects.
You start by defining classes, which act as blueprints, and then create objects from
them. OOP simplifies modeling real-world concepts in your programs and enables
you to build systems that are more reusable and scalable.

For example, an object could represent a person with properties like a name, age,
and address and behaviors such as walking, talking, breathing, and running. Or it
could represent an email with properties like a recipient list, subject, and body and
behaviors like adding attachments and sending.

Put another way, object-oriented programming is an approach for modeling concrete,


real-world things, like cars, as well as relations between things, like companies and
employees or students and teachers. OOP models real-world entities as software
objects that have some data associated with them and can perform certain operations.

Object-oriented programming has several advantages over procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the Python code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug

1
 OOP makes it possible to create full reusable applications with less code and
shorter development time

8.2 The Pillars Of OOP


OOP also exists in other programming languages and is often described to center
around the four pillars of OOP:

1. Encapsulation allows you to bundle data (attributes) and behaviors


(methods) within a class to create a cohesive unit. By defining methods to
control access to attributes and its modification, encapsulation helps maintain
data integrity and promotes modular, secure code.

2. Inheritance enables the creation of hierarchical relationships between classes,


allowing a subclass to inherit attributes and methods from a parent class. This
promotes code reuse and reduces duplication.
3. Abstraction focuses on hiding implementation details and exposing only the
essential functionality of an object. By enforcing a consistent interface,
abstraction simplifies interactions with objects, allowing developers to focus
on what an object does rather than how it achieves its functionality.
4. Polymorphism allows you to treat objects of different types as instances of
the same base type, as long as they implement a common interface or behavior.
Python’s duck typing make it especially suited for polymorphism, as it allows
you to access attributes and methods on objects without needing to worry
about their actual class.

2
8.3 Classes and Instances
In a Python program, a class defines a type of object with attributes (fields) and
methods (procedures). A class is a blueprint for creating objects. Individual objects
created of the class type are called instances.

8.4 Creating instances with __init__()


In python, the constructor is denoted by___init___() and is a special method that is
called every time a new instance of a class is created. self refers to the instance of a
class and is used in class methods to access the specific instance that called the
method. __init__() uses self to define and initialize the instance's attributes.

8.5 Instance attributes vs. class attributes


The attributes shown so far have been instance attributes. An instance attribute is
a variable that is unique to each instance of a class and is accessed using the format
instance_name.attribute_name. Another type of attribute, a class
attribute, belongs to the class and is shared by all class instances. Class attributes
are accessed using the format class_name.attribute_name.

Example 8.1
class polygon:
poly_name = {3:'Triangle', 4:'Quadrilateral',
5:'Pentagon', 6:'Heptagon', 7:'Hexagon',
8:'Octagon',
9:'Nonagon', 10:'Decagon'}

def __init__(self, n):


self.sides = n

def sum_of_interior_angles(self):
return (2*self.sides - 4)*90

def get_name(self):
name = polygon.poly_name.get(self.sides)
if name == None:
return "Invalid number of sides"
else:
return name

pol1 = polygon(3)
3
print(f"Name of polygon is {pol1.get_name()}")
print(f"Sum of interior angles is
{pol1.sum_of_interior_angles()}")

Sample output
Name of polygon is Triangle
Sum of interior angles is 180

4
Sample implementation

You might also like