0% found this document useful (0 votes)
1 views6 pages

Python Notes 4 Th Unit

The document provides an overview of Object-Oriented Programming (OOP) in Python, highlighting key principles such as encapsulation, abstraction, inheritance, polymorphism, and type identification. It explains how to define classes and create objects, along with examples demonstrating instance methods, class variables, and inheritance. The document emphasizes the significance of OOP in enhancing program design and structure.

Uploaded by

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

Python Notes 4 Th Unit

The document provides an overview of Object-Oriented Programming (OOP) in Python, highlighting key principles such as encapsulation, abstraction, inheritance, polymorphism, and type identification. It explains how to define classes and create objects, along with examples demonstrating instance methods, class variables, and inheritance. The document emphasizes the significance of OOP in enhancing program design and structure.

Uploaded by

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

Unit- IV

Object-Oriented Programming (OOP) in Python

Object-Oriented Programming (OOP) is a programming paradigm based on the


concept of "objects," which bundle both data and methods that operate on the data.
Python is a powerful and versatile language that fully supports OOP.
Understanding the principles of OOP can significantly enhance the way you design
and structure your programs.

Key Principles of Object-Oriented Programming

1. Encapsulation:
Encapsulation refers to bundling the data (variables) and the methods
(functions) that operate on the data into a single unit known as a class. It
also involves restricting access to some of the object's components (e.g.,
private variables or methods), so that the internal workings of an object are
hidden from the outside world.
2. Abstraction:
Abstraction is the concept of hiding the complex implementation details and
showing only the necessary features of an object. In Python, this is typically
done using classes and methods that expose a clean interface while hiding
the internal workings.
3. Inheritance:
Inheritance allows a new class (called a subclass) to inherit the properties
and behaviors (methods) from an existing class (called a superclass). This
promotes code reuse and the creation of hierarchical relationships between
classes.
4. Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a
common superclass. More specifically, it enables methods to have the same
name but behave differently depending on the object calling them. This is
achieved through method overriding (in subclass) or method overloading
(with different arguments in some languages, though Python relies more on
overriding).
5. Type Identification:
Type identification is the process of identifying the type of an object, which
helps in performing specific operations based on the object's type. In Python,
this can be done using the type() function or is instance() to check
an object’s type or class.
Classes in Python

A class in Python is a blueprint for creating objects (instances). A class defines the
properties and behaviors (methods) that the objects created from it will have.

Syntax for Defining a Class


python
Copy code
class ClassName:
# Constructor to initialize the object
def __init__(self, parameter1, parameter2):
self.attribute1 = parameter1
self.attribute2 = parameter2
# A method within the class
def method1(self):
return f"Attribute 1: {self.attribute1},
Attribute 2: {self.attribute2}"

 __init__(self, ...): The constructor method initializes the


attributes of the object when it is created.
 self: Refers to the instance of the class. It allows the instance to access its
own attributes and methods.

Creating Classes and Objects

Once the class is defined, you can create objects (instances) of that class. Each
object has its own set of attributes and can call the methods defined in the class.

Example:
python
Copy code
# Define a class
class Dog:
# Constructor
def __init__(self, name, age):
self.name = name
self.age = age
# Method
def bark(self):
return f"{self.name} says Woof!"

# Create an object (instance) of the class


my_dog = Dog("Buddy", 3)

# Access attributes and methods


print(my_dog.name) # Output: Buddy
print(my_dog.bark()) # Output: Buddy says Woof!

Instance Methods

An instance method is a method that operates on the instance of the class (the
object) and can access its attributes. These methods take self as their first
parameter, which refers to the instance of the class.

Example of Instance Method:


python
Copy code
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def car_info(self):
return f"{self.year} {self.make} {self.model}"
# Create an object of Car class
my_car = Car("Toyota", "Corolla", 2020)
print(my_car.car_info()) # Output: 2020 Toyota Corolla
Class Variables

A class variable is a variable that is shared by all instances of the class. Unlike instance
variables, which are unique to each object, class variables are the same for all objects of that
class.

Example of Class Variable:


python
Copy code
class Dog:
species = "Canine" # This is a class variable

def __init__(self, name, age):


self.name = name
self.age = age

def info(self):
return f"{self.name} is {self.age} years old and belongs to the
{Dog.species} species."

# Create objects
dog1 = Dog("Max", 5)
dog2 = Dog("Bella", 3)

print(dog1.info()) # Output: Max is 5 years old and belongs to the Canine


species.
print(dog2.info()) # Output: Bella is 3 years old and belongs to the Canine
species.
print(Dog.species) # Output: Canine

Inheritance

Inheritance allows one class to inherit the properties and methods of another class,
making code reuse easier. A subclass can override methods from the superclass to
provide its own specific implementation.

Example of Inheritance:
python
Copy code
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal): # Dog is a subclass of Animal
def speak(self):
return f"{self.name} barks."

# Create objects
dog = Dog("Rex")
animal = Animal("Generic Animal")

print(dog.speak()) # Output: Rex barks.


print(animal.speak()) # Output: Generic Animal makes a
sound.

In this example, the Dog class inherits from Animal and overrides the speak
method.

Polymorphism

Polymorphism allows different classes to be treated as instances of the same class


through inheritance. The main feature of polymorphism is that different classes can
define methods with the same name, but with different implementations.

Example of Polymorphism:
python
Copy code
class Cat(Animal):
def speak(self):
return f"{self.name} meows."
# Create objects
cat = Cat("Whiskers")
dog = Dog("Rex")

animals = [dog, cat]

for animal in animals:


print(animal.speak())
# Output:
# Rex barks.
# Whiskers meows.
Here, both Dog and Cat classes have a speak method, but each one behaves
differently depending on the class of the object.

Type Identification

To check the type of an object or identify its class, Python provides built-in
functions such as type() and isinstance().

 type() returns the type (class) of an object.


 isinstance() checks if an object is an instance of a specific class or
subclass.

Example:
python
Copy code
# Using type()
obj = Dog("Buddy", 5)
print(type(obj)) # Output: <class '__main__.Dog'>

# Using isinstance()
print(isinstance(obj, Dog)) # Output: True
print(isinstance(obj, Animal)) # Output: True (since
Dog inherits from Animal)

You might also like