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

Introduction to Object Oriented Programming in Python

The document provides an introduction to Object-Oriented Programming (OOP) in Python, explaining its core concepts, advantages, and comparison with procedural programming. It covers essential OOP principles such as encapsulation, abstraction, inheritance, and polymorphism, along with practical examples and best practices for implementation. The document emphasizes the importance of mastering these principles for writing clean, reusable, and maintainable code.

Uploaded by

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

Introduction to Object Oriented Programming in Python

The document provides an introduction to Object-Oriented Programming (OOP) in Python, explaining its core concepts, advantages, and comparison with procedural programming. It covers essential OOP principles such as encapsulation, abstraction, inheritance, and polymorphism, along with practical examples and best practices for implementation. The document emphasizes the importance of mastering these principles for writing clean, reusable, and maintainable code.

Uploaded by

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

Introduction to Object Oriented

Programming in Python
Concepts, Code Examples and Best Practices
What is Object-Oriented
Programming?
• OOP is a programming paradigm based on the
concepts of “Objects”
• Objects contain data in the form of attributes
and behavior in the form of methods.
• OOP allows you to model real-world entities in
your code.
• It helps write modular, reusable, and scalable
programs.
Why Use Object-Oriented
Programming?
• Modularity – Break programs into smaller,
independent parts.
• Reusability – Create classes that can be reused
across projects.
• Maintainability – Easier to update and debug
code.
• Scalability – Supports building large
applications efficiently.
• Abstraction – Hide internal complexity from the
user.
OOP vs. Procedural
Programming
Object-Oriented
Feature Procedural Programming
Programming

Structure Based on objects & classes Based on functions & steps

Shared globally or via


Data Handling Encapsulated inside objects
functions

Code Reusability High through inheritance Limited reuse, often repetitive

Example car.start() start(car)


Real-World Analogy
Think of a Car as an Object:
• Attributes (Data): Color, Make, Model, Speed
• Methods (Behavior): Drive(), Brake(), Honk()

You can create many cars (objects) using one


Car class (blueprint). Each car can have
different data but share common behavior.
Python and OOP
• Python is fully object-oriented—everything is an
object.
• Built-in support for defining classes, creating
instances, inheritance, and polymorphism.
• Simple syntax to define and use objects.
• Ideal for beginners to understand OOP
principles clearly.
Classes in Python
• A class is a user-defined blueprint for creating
objects.
• It groups data (attributes) and functions
(methods) together.
• Syntax example: • __init__ is a special method called constructor
method. It is called automatically when an
instance of a class is created.
Objects and Instances
• An object is a specific instance of a class.
• You can create many objects from the same
class, each with its own data.
• Example:
Attributes and Methods
• Attributes: Variables associated with an
object (e.g., self.name)
• Methods: Functions defined inside a class
(e.g., greet() )
• Example:
Real-Life Example: Car Class
Creating and Using Objects

• This creates an object my_car from the car


class.
• The drive method is called using the dot
notation.
• Each object holds its own set of data (make,
model).
The __init__ Constructor method
• Automatically called when a new object is
created.
• Initializes object attributes
• Always named __init__
Instance vs Class Variables
• Instance variables are specific to an object
• Class variables are shared across all
instances.
Real-Life Example: Book Object

• book1 and book2 are two different objects


with different data.
• Both are created from the same class Book.
Working with Multiple Methods

• You can define multiple methods in a class.


• Each method performs a specific behavior for
the object.
The 4 Pillars of OOP
• Encapsulation – Binding data and methods
together
• Abstraction – Hiding unnecessary details
• Inheritance – Reusing code from parent
classes.
• Polymorphism – Same method name behaves
differently.
Definitions Recap: The 4 Pillars
• Encapsulation – Protects internal object state
by controlling access.
• Abstraction – Simplifies complex systems by
showing only the relevant parts.
• Inheritance – Allows a new class to use
properties of an existing class.
• Polymorphism – Enables different objects to
be treated through a common interface.
Encapsulation
Encapsulation
• Encapsulation restricts direct access to internal
data.
• Achieved using private attributes and
getter/setter methods.
Encapsulation in Action

• The internal balance is not directly accessible.


• Only methods can modify the data, ensuring
data safety.
Benefits of Encapsulation
• Prevents accidental modification of internal
data.
• Encourages use of methods for data access.
• Makes code more secure and maintainable.
• Allows changes in internal implementation
without affecting external code.
Example:
Abstraction
Abstraction
• Abstraction hides internal details and shows
only relevant features.
• Makes complex systems easier to use and
understand.
• User don’t need to know how these methods
work internally.
Abstraction with
Implementation

• Simple interface to operate a complex device.


• Focus is on what the object does, not how it
does it.
Benefits of Abstraction
• Reduces complexity for users.
• Improves code readability.
• Promotes cleaner, modular design.
• Users interact with a simplified interface.
Example:
Inheritance
Inheritance
• A class can inherit attributes and methods from
another class.
• Promotes code reuse and extension.
• Dog class inherits from Animal.
• It overrides the speak() method.
• Same method name, different behavior.
Example:
Types of Inheritance in Python
• Single Inheritance – One child, one parent.
• Multiple Inheritance – One child, multiple
parents.
• Multilevel Inheritance – Chain of inheritance.
• Hierarchical Inheritance – One parent,
multiple children.
Single Inheritance: Concept
• In single inheritance, a child class inherits
from one parent class.
• Promotes code reuse and logical structure.
• Syntax:
Single Inheritance Concept

• Child class inherits all methods from Parent.


• No need to redefine greet() in the child class
unless overriding is needed.
When to Use Single Inheritance
• When you want to extend or specialize a
single class.
• Example:
• Vehicle  Car
• Animal  Dog
• Keep it simple when only one parent is needed.
Multiple Inheritance: Concept
• A child class inherits from more than one
parent.
• Python supports multiple inheritance using
comma-separated parent classes.
• Syntax:
Multiple Inheritance in Action

• Class C inherits methods from both A and B.


• Useful when combining features from multiple
sources.
Caution with Multiple
Inheritance
• Can lead to method resolution conflicts.
• Python uses MRO (Method Resolution
Order) to resolve conflicts.
• Keep hierarchy clear to avoid confusion.
Multilevel Inheritance: Concept
• A child class inherits from a parent class, and
then another child class inherits from the
child
• Forms a chain of inheritance.
• Syntax:
Multilevel Inheritance in Action

• Child inherits from Parent, which inherits from


Grand Parent.
• Child can access methods from both parent
and grandparent.
Use Cases of Multilevel
Inheritance
• Reflects all real world hierarchies e.g.,
Organism  Animal  Mammal.
• Adds depth to class structure
• Be cautious of long chains—can become hard
to manage.
Hierarchical Inheritance:
Concept
• Multiple child classes inherit from the same
parent class.
• Useful when different objects share common
functionality.
Hierarchical Inheritance in
Action

• Both child classes share behavior from the


same parent.
• Avoids duplication and keeps shared logic in
one place.
Benefits of Hierarchical
Inheritance
• Promotes code reuse for shared behaviors.
• Easy to manage multiple subclasses with
common base.
• Example:
• Shape  Circle, Rectangle, Triangle
• All these classes inherit from the same parent
class Shape.
Polymorphism
Polymorphism
• Polymorphism means many forms.
• A method behaves differently depending on the
object calling it.
Polymorphism in Action

• The same function animal_sound() works with


different objects.
• Demonstrates flexibility and reusability of code
using polymorphism.
Method Overriding
• Subclass provides a specific implementation of
a method in its parent class.
• Example:

• Bike overrides start()


function from Vehicle
class.
Built-in Polymorphism in Python
• Python supports polymorphism in built-in
functions:
• Same function behaves differently based on the
data type of the argument.
Benefits of Polymorphism
• Promotes code generalization.
• Simplifies method usage across different types.
• Enables cleaner, extensible code through
dynamic behavior.
Example:
OOP Pillars Recap

Pillar Purpose Python Feature

Encapsulation Protect data and control access Private variables, getters

Abstract methods, clean


Abstraction Hide details, show essentials
APIs
Reuse and extend existing
Inheritance Subclassing
classes
Common interface for different Method
Polymorphism
behaviors overriding/functions
Final Thoughts
• OOP helps structure code like real-world
systems.
• Python makes OOP intuitive and flexible.
• Mastering the 4 pillars is key to writing clean,
reusable, and maintainable code.
• Practice by building small projects and applying
each principle.

You might also like