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

OBJECT ORIENTED PROGRAMMING

The document provides an overview of Object Oriented Programming (OOP) in Python, detailing its core principles such as classes, objects, inheritance, encapsulation, polymorphism, and abstraction. It explains the creation and use of classes and objects, the significance of instance and class variables, and various types of inheritance and polymorphism. Additionally, it covers encapsulation and data abstraction, emphasizing their importance in organizing and managing code effectively.

Uploaded by

Okorie Chinedu P
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)
2 views

OBJECT ORIENTED PROGRAMMING

The document provides an overview of Object Oriented Programming (OOP) in Python, detailing its core principles such as classes, objects, inheritance, encapsulation, polymorphism, and abstraction. It explains the creation and use of classes and objects, the significance of instance and class variables, and various types of inheritance and polymorphism. Additionally, it covers encapsulation and data abstraction, emphasizing their importance in organizing and managing code effectively.

Uploaded by

Okorie Chinedu P
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/ 7

Course: Business Programming

Topic: Object Oriented Programming


Object Oriented Programming is a fundamental concept in
Python, empowering developers to build modular,
maintainable, and scalable applications. By understanding the
core OOP principles (classes, objects, inheritance,
encapsulation, polymorphism, and abstraction), programmers
can leverage the full potential of Python OOP capabilities to
design elegant and efficient solutions to complex problems.
OOPs is a way of organizing code that uses objects and classes
to represent real-world entities and their behavior. In OOPs,
object has attributes thing that has specific data and can
perform certain actions using methods.

OOPs Concepts in Python


 Class in Python
 Objects in Python
 Polymorphism in Python
 Encapsulation in Python
 Inheritance in Python
 Data Abstraction in Python

Python Class
A class is a collection of objects. Classes are blueprints for
creating objects. A class defines a set of attributes and
methods that the created objects (instances) can have.
Some points on Python class:
 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using
the dot (.) operator. Example: Myclass.Myattribute

Creating a Class

Here, the class keyword indicates that we are creating a class


followed by name of the class (Dog in this case).
Python
Explanation:
 class Dog: Defines a class named Dog.
 species: A class attribute shared by all instances of the
class.
 method: Initializes the name and age attributes
__init__
when a new object is created.
Note: For more information, refer to python classes.

Python Objects
An Object is an instance of a Class. It represents a specific
implementation of the class and holds its own data.
An object consists of:
 State: It is represented by the attributes and reflects the
properties of an object.
 Behavior: It is represented by the methods of an object
and reflects the response of an object to other objects.
 Identity: It gives a unique name to an object and
enables one object to interact with other objects.

Creating Object

Creating an object in Python involves instantiating a class to


create a new instance of that class. This process is also
referred to as object instantiation.
Python

Output
Buddy
Canine
Explanation:
 dog1 = Dog(“Buddy”, 3): Creates an object of the Dog
class with name as “Buddy” and age as 3.
 dog1.name: Accesses the instance attribute name of the
dog1 object.
dog1.species: Accesses the class attribute species of the dog1
object.
Python
Explanation:
 self.name: Refers to the name attribute of the object
(dog1) calling the method.
 dog1.bark(): Calls the bark method on dog1.
Note: For more information, refer to self in the Python class

__init__ Method

__init__ method is the constructor in Python, automatically


called when a new object is created. It initializes the attributes
of the class.
Python

Output
Buddy
Explanation:
 __init__: Special method used for initialization.
 self.name and self.age: Instance attributes initialized in
the constructor.

Class and Instance Variables

In Python, variables defined in a class can be either class


variables or instance variables, and understanding the
distinction between them is crucial for object-oriented
programming.
Class Variables
These are the variables that are shared across all instances of
a class. It is defined at the class level, outside any methods.
All objects of the class share the same value for a class
variable unless explicitly overridden in an object.
Instance Variables
Variables that are unique to each instance (object) of a class.
These are defined within the __init__ method or other instance
methods. Each object maintains its own copy of instance
variables, independent of other objects.
Python
Output
Canine
Buddy
Charlie
Max
Feline
Feline
Explanation:
 Class Variable (species): Shared by all instances of the
class. Changing Dog.species affects all objects, as it’s a
property of the class itself.
 Instance Variables (name, age): Defined in the __init__
method. Unique to each instance (e.g., dog1.name and
dog2.name are different).
 Accessing Variables: Class variables can be accessed
via the class name (Dog.species) or an object (dog1.species).
Instance variables are accessed via the object (dog1.name).
 Updating Variables: Changing Dog.species affects all
instances. Changing dog1.name only affects dog1 and does
not impact dog2.

Python Inheritance
Inheritance allows a class (child class) to acquire properties
and methods of another class (parent class). It supports
hierarchical classification and promotes code reuse.

Types of Inheritance:

1. Single Inheritance: A child class inherits from a single


parent class.
2. Multiple Inheritance: A child class inherits from more
than one parent class.
3. Multilevel Inheritance: A child class inherits from a
parent class, which in turn inherits from another class.
4. Hierarchical Inheritance: Multiple child classes inherit
from a single parent class.
5. Hybrid Inheritance: A combination of two or more types
of inheritance.
Python
Explanation:
 Single Inheritance: Labrador inherits Dog’s attributes
and methods.
 Multilevel Inheritance: GuideDog extends Labrador,
inheriting both Dog and Labrador functionalities.
 Multiple Inheritance: GoldenRetriever inherits from
both Dog and Friendly.

Python Polymorphism
Polymorphism allows methods to have the same name but
behave differently based on the object’s context. It can be
achieved through method overriding or overloading.

Types of Polymorphism

1. Compile-Time Polymorphism: This type of


polymorphism is determined during the compilation of the
program. It allows methods or operators with the same
name to behave differently based on their input parameters
or usage. It is commonly referred to as method or operator
overloading.
2. Run-Time Polymorphism: This type of polymorphism is
determined during the execution of the program. It occurs
when a subclass provides a specific implementation for a
method already defined in its parent class, commonly
known as method overriding.

Code Example:

Python
Explanation:
1. Run-Time Polymorphism:
 Demonstrated using method overriding in the Dog class
and its subclasses (Labrador and Beagle).
 The correct sound method is invoked at runtime based
on the actual type of the object in the list.
2. Compile-Time Polymorphism:
 Python does not natively support method overloading.
Instead, we use a single method (add) with default
arguments to handle varying numbers of parameters.
 Different behaviors (adding two or three numbers) are
achieved based on how the method is called.

Python Encapsulation
Encapsulation is the bundling of data (attributes) and
methods (functions) within a class, restricting access to some
components to control interactions.
A class is an example of encapsulation as it encapsulates all
the data that is member functions, variables, etc.

Types of Encapsulation:

1. Public Members: Accessible from anywhere.


2. Protected Members: Accessible within the class and its
subclasses.
3. Private Members: Accessible only within the class.

Code Example:

Python
Explanation:
 Public Members: Easily accessible, such as name.
 Protected Members: Used with a single _, such as
_breed. Access is discouraged but allowed in subclasses.
 Private Members: Used with __, such as __age. Access
requires getter and setter methods

Data Abstraction
Abstraction hides the internal implementation details while
exposing only the necessary functionality. It helps focus on
“what to do” rather than “how to do it.”

Types of Abstraction:

 Partial Abstraction: Abstract class contains both


abstract and concrete methods.
 Full Abstraction: Abstract class contains only abstract
methods (like interfaces).

You might also like