0% found this document useful (0 votes)
4 views45 pages

Week 8 Object-Oriented Programming (1)

The document provides an overview of Object-Oriented Programming (OOP), detailing its core concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains the structure of classes and objects, including attributes and methods, and discusses access modifiers in Python. Additionally, it covers different types of inheritance and the concept of polymorphism, emphasizing the importance of these principles in software development.

Uploaded by

Gelo Maranan
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)
4 views45 pages

Week 8 Object-Oriented Programming (1)

The document provides an overview of Object-Oriented Programming (OOP), detailing its core concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains the structure of classes and objects, including attributes and methods, and discusses access modifiers in Python. Additionally, it covers different types of inheritance and the concept of polymorphism, emphasizing the importance of these principles in software development.

Uploaded by

Gelo Maranan
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/ 45

Week 8 – Object-Oriented

Programming
Object-Oriented Programming (OOP)
• Object-oriented technology models real-world objects.
• Examples of real-world objects
Object-Oriented Programming (OOP)
• Object Oriented Programming or OOP is a methodology to design a program using
classes and objects. It simplifies the software development and maintenance by
providing some concepts:
• Object
• Class
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
Object
• a software component whose structure is similar to objects
in the real world.
• an entity that has a state, behavior and identity with a well-
defined role in problem space, both tangible and intangible
• composed of a set of data (properties/attributes)
• variables describing the essential characteristics of the object,
• and it also consists of a set of methods (behavior) Object: Dog
• describes how an object behaves State: attributes of the object
(name, color, breed)
• can only be invoked by sending messages to an object.
Behavior: bark, jump, roll-over
• Behavior is shared among objects.
• an actual instance of a class.
Class
• Objects with similar characteristics
can fall under one class.
• Example:
• Dog represents all breeds of dogs:
bulldog, poodle, chihuahua, etc.
• Each dog breed can be “derived” or
“built from” a general class called
Dog.
Class
• In object-oriented terms, the Dog class is the template or blueprint from
which bulldog, poodle and chihuahua are derived.
• A class is a template or a blueprint, while an object is an instance of a
class.
• A class is made up of attributes and behavior.
• State or Attributes of the class are represented by variables called fields.
• Class behavior is revealed through methods.
Class Members
• Class members are declarations made inside the body of the class.
• The following are class members:
• FIELDS
• Also referred to as attributes.
• These are data used by the class.
• They are variables declared inside the class body.
• METHODS
• Also referred to as the behavior(s).
• These are program statements grouped together to perform a specific function
Creating a Class

Blank Class Class with class attributes


Creating Class
Creating Objects
• Since classes are templates, they provide the benefit of reusability.
• A class can be used over and over to create many objects.
• An object created out of a class will contain the same variables that the class
has. However, the values of the variables are specific to the object created.
• Creating an object out of a class is called class instantiation.

obj = Dog()
dog1 = Dog()
cat1 = Cat()
p1 = Person()
Class

Object
Example
The __init__() Function
• The __init__() function is called automatically every time the class is
being used to create a new object.
• Also known as Constructor in other programming

The self parameter is a


reference to the current
instance of the class,
and is used to access
variables that belong to
the class.
Problem Set 1
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
Encapsulation
• Encapsulation is one of the key features of object-oriented
programming. Encapsulation refers to the bundling of attributes and
methods inside a single class.
• It prevents outer classes from accessing and changing attributes and
methods of a class. This also helps to achieve data hiding.
Public Access Modifiers
• The members of a class that are declared public are easily accessible
from any part of the program. All data members and member
functions of a class are public by default.
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.
Private Access Modifiers
Protected Access Modifiers
• The members of a class that are declared protected are only
accessible within the class and 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.
Protected
Access Modifiers
Problem Set 2
Inheritance
• Inheritance is when an object or class is based on another object
using the same implementation or specifying a new implementation
to maintain the same behavior
• Such an inherited class is called a subclass (Child) of its parent class
or super class (Parent)
• This idea was first adopted in the Simula 67 programming language.
Types of Inheritance in Python
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
Single Inheritance
• Single inheritance enables a
derived class to inherit
properties from a single
parent class, thus enabling
code reusability and the
addition of new features to
existing code.
Multiple Inheritance
• When a class can be derived from
more than one base class this
type of inheritance is called
multiple inheritance. In multiple
inheritance, all the features of the
base classes are inherited into the
derived class.
Multilevel Inheritance
• In multilevel inheritance,
features of the base class and
the derived class are further
inherited into the new derived
class. This is similar to a
relationship representing a child
and grandfather.
Hierarchical Inheritance
• When more than one derived
classes are created from a single
base this type of inheritance is
called hierarchical inheritance. In
this program, we have a parent
(base) class and three child
(derived) classes.
Single
Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Problem Set 3
Polymorphism
• Polymorphism is another
important concept of object-
oriented programming. It simply
means more than one form.
• That is, the same entity (method
or operator or object) can perform
different operations in different
scenarios.
Polymorphism in Class Methods
Method Overriding
• Like in other programming languages,
the child classes in Python also inherit
methods and attributes from the parent
class. We can redefine certain methods
and attributes specifically to fit the child
class, which is known as Method
Overriding.
• Polymorphism allows us to access these
overridden methods and attributes that
have the same name as the parent class.
Problem Set 4
THANK YOU
45

You might also like