OBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING
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
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
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
Output
Buddy
Explanation:
__init__: Special method used for initialization.
self.name and self.age: Instance attributes initialized in
the constructor.
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:
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
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:
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: