0% found this document useful (0 votes)
8 views32 pages

Unit 1 ADSP

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in Python, including key principles such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It discusses the definitions, types, advantages, and applications of these concepts, along with examples and code snippets to illustrate their implementation. The document emphasizes the benefits of OOP, such as code reusability, modularity, and improved security.

Uploaded by

Sushma Narayana
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)
8 views32 pages

Unit 1 ADSP

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts in Python, including key principles such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It discusses the definitions, types, advantages, and applications of these concepts, along with examples and code snippets to illustrate their implementation. The document emphasizes the benefits of OOP, such as code reusability, modularity, and improved security.

Uploaded by

Sushma Narayana
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

Unit-1

[Link] Concepts in Python


Introduction to OOP

Object Oriented Programming (OOP) is a programming paradigm that organizes software


design around objects rather than functions and logic.

Python supports OOP to achieve:

• Modularity

• Code reusability

• Security

• Scalability

• Real-world modeling

OOP is based on key concepts such as:

• Class

• Object

• Constructor

• Variables

• Methods

• Inheritance

• Polymorphism

• Abstraction

• Encapsulation

Class

Definition

A class is a blueprint or template used to create objects.


It defines:

• Data (variables)

• Functions (methods)

Key Points

• Logical entity

• Does not occupy memory until object is created

• Groups data + functions together

Object

Definition

An object is an instance of a class.

It represents a real-world entity and occupies memory.


Key Points

• Physical entity

• Multiple objects can be created from one class

• Access class members using object

Constructor

Definition

A constructor is a special method used to initialize object data automatically when an object is
created.

In Python, constructor is written using:


Types of Constructors

Default Constructor

A constructor that does not accept any parameters (except self).

Parameterized Constructor

A constructor that accepts parameters to initialize object data.

Key Points

• Automatically invoked

• Initializes instance variables

• Improves code readability

Types of Variables in OOP


Instance Variables

Declared inside constructor using self.

Unique for each object.

Class Variables (Static Variables)

Declared inside class but outside methods.

Shared by all objects.

Difference
Instance Variable Class Variable

Object specific Common to all

Declared in constructor Declared in class

Uses self No self needed

Types of Methods in OOP

Instance Methods

Operate on instance variables.

Use self.

Class Methods

Operate on class variables.

Use @classmethod decorator and cls.


Static Methods

Do not use class or instance data.

Use @staticmethod.

Method Comparison

Method Type Uses Parameter

Instance Instance variables self

Class Class variables cls

Static Utility logic None

Inheritance

Definition: Mechanism where one class acquires properties of another.


Syntax:

Types (Single line)

• Single: One parent → one child

• Multiple: Many parents → one child

• Multilevel: Chain inheritance

• Hierarchical: One parent → many children

• Hybrid: Combination

Polymorphism

Definition: Same function behaves differently in different situations.

Syntax Example:

Types

• Function polymorphism

• Operator polymorphism

• Method overriding

• Object polymorphism

Abstraction

Definition: Hides implementation, shows only functionality.


Encapsulation

Definition: Binding data + methods and restricting access.

Syntax:

11. Advantages of OOP

• Code reusability

• Easy maintenance

• Data security

• Modularity

• Real-world mapping

• Scalability

12. Applications of OOP

• Banking systems

• Insurance applications
• Student management systems

• GUI applications

• Web development

13. COMBINED PROGRAM COVERING ALL OOP CONCEPTS

from abc import ABC, abstractmethod

# Abstract Class (Abstraction)


class Person(ABC):

# Class Variable
species = "Human"

# Constructor
def __init__(self, name, age):
# Instance Variables
[Link] = name
[Link] = age
self.__salary = 50000 # Encapsulation (Private)

# Instance Method
def display(self):
print("Name:", [Link])
print("Age:", [Link])

# Class Method
@classmethod
def show_species(cls):
print("Species:", [Link])

# Static Method
@staticmethod
def greet():
print("Welcome to OOP in Python")

# Abstract Method
@abstractmethod
def job(self):
pass

# Inheritance + Polymorphism (Overriding)


class Engineer(Person):

def job(self):
print([Link], "is a Software Engineer")

class Doctor(Person):

def job(self):
print([Link], "is a Doctor")

# Objects Creation
e1 = Engineer("Manvitha", 23)
d1 = Doctor("Rahul", 30)

# Method Calls
[Link]()
[Link]()

[Link]()
[Link]()

# Class Method Call


Person.show_species()

# Static Method Call


[Link]()
[Link] in Python
Definition

Inheritance is an OOP concept in which one class (child/derived class) acquires the properties
and behaviors (variables & methods) of another class (parent/base class).

It promotes:

• Code reusability

• Extensibility

• Hierarchical classification

Syntax

Types of Inheritance in Python

Single Inheritance

Definition

One parent class → One child class.

Diagram (draw in exam)

Program
Output

Multiple Inheritance

Definition

One child class inherits from more than one parent class.

Diagram

Program
Multilevel Inheritance

Definition

Inheritance chain (Grandparent → Parent → Child).

Diagram
Program

Output:

Hierarchical Inheritance

Definition
One parent class → Multiple child classes.

Diagram

Program

Output:

Hybrid Inheritance
Definition

Combination of two or more types of inheritance.

(Commonly multiple + hierarchical.)

Diagram

Program
Method Resolution Order (MRO) — Extra Point

In multiple/hybrid inheritance, Python follows MRO to decide method calling order.

You can write:

Advantages of Inheritance

• Code reusability

• Reduces redundancy

• Improves maintainability

• Supports polymorphism

• Extensible programs

Applications

• Banking systems

• Employee management

• GUI frameworks

• Educational software

Conclusion

Inheritance enables creation of hierarchical class structures and promotes reusability and
scalability in Python applications. Python supports single, multiple, multilevel, hierarchical, and
hybrid inheritance.
[Link] in Python
Introduction

Polymorphism is an Object-Oriented Programming concept that allows the same function name,
method name, or operator to perform different behaviors depending on the object or context in
which it is used.

The term polymorphism means “many forms.”

It enables dynamic behavior, flexibility, and reusability in software design. Polymorphism


reduces code duplication and allows a common interface for different data types and classes.

Types of Polymorphism in Python

Polymorphism in Python can be implemented in the following ways:

1. Polymorphism with Functions and Objects

2. Polymorphism with Class Methods

3. Polymorphism with Inheritance (Method Overriding)

4. Method Overloading

5. Operator Polymorphism

Polymorphism with Functions and Objects

Theoretical Explanation

In Python, built-in functions behave differently depending on the type of object passed to them.
The same function name can work on multiple data types.

Similarly, different classes can define methods with the same name, and Python will execute the
appropriate method based on the object calling it. This is called object polymorphism.

This concept supports dynamic typing and runtime decision-making.


Polymorphism with Class Methods

Theoretical Explanation

Polymorphism can be achieved when multiple classes define methods with the same name but
implement them differently.

Even though the method name is identical, the behavior changes based on the class object
invoking it.

This allows different classes to follow a common interface.

Different classes can define the same method name.

Polymorphism with Inheritance (Method Overriding)

Theoretical Explanation

When a child class redefines a method already defined in its parent class, it is called method
overriding.

The method in the child class replaces the parent class method during runtime. This is also
called runtime polymorphism.

Inheritance is required for method overriding.


Method Overloading

Theoretical Explanation

Method overloading refers to defining multiple methods with the same name but different
parameters within the same class.

Python does not support true method overloading like C++ or Java. However, it can be achieved
using:

• Default arguments

• Variable-length arguments (*args)

This allows a method to accept different numbers of parameters.

Operator Polymorphism
Theoretical Explanation

Operators in Python behave differently based on operand types.

For example:

• “+” performs addition for integers

• “+” performs concatenation for strings

This is also a form of polymorphism.

Difference Between Overloading and Overriding

Feature Overloading Overriding

Class Same class Parent and Child

Parameters Different Same

Inheritance Required No Yes

Binding Compile-time concept Runtime concept

Python Support Indirect Fully supported

Combined Program Demonstrating All Polymorphism Concepts


4. Abstraction in Python
Introduction

Abstraction is an Object-Oriented Programming concept that focuses on hiding internal


implementation details and showing only essential features to the user.

It helps in reducing programming complexity by separating “what to do” from “how to do.”

The user interacts with the interface without knowing the underlying logic.

Definition

Abstraction is the process of hiding the implementation details and displaying only the
functionality of an object.

It ensures security, modularity, and maintainability in software development.

Need for Abstraction

Abstraction is required to:

• Hide complex code logic

• Provide simple user interface

• Improve code security

• Reduce development complexity

• Enhance maintainability

• Enable modular programming

Example (conceptual):
When using ATM, user only sees options like withdraw/deposit, not internal banking operations.

Achieving Abstraction in Python

Python achieves abstraction using:

1. Abstract Classes

2. Abstract Methods

3. ABC Module (Abstract Base Class)

Abstract Class
Theoretical Explanation

An abstract class is a class that cannot be instantiated (object cannot be created directly).

It acts as a blueprint for other classes.

It contains:

• Abstract methods (without implementation)

• Concrete methods (with implementation)

Child classes must implement abstract methods.

Abstract Method

Theoretical Explanation

An abstract method is a method declared in an abstract class but does not contain
implementation.

It only defines method signature.

Child classes must override and implement it.

If not implemented → child class also becomes abstract.

Rules of Abstract Classes


• Cannot create object of abstract class

• Must inherit from ABC module

• Must use @abstractmethod decorator

• Child class must implement all abstract methods

• Supports polymorphism

Abstract Class with Concrete Method

Abstract class can also contain normal methods.

Advantages of Abstraction

• Hides internal complexity

• Improves security

• Reduces code duplication

• Enhances maintainability

• Provides modular structure

• Supports loose coupling

Real-World Applications

• Banking systems

• ATM software

• Payment gateways
• Insurance portals

• GUI frameworks

• Enterprise applications

Difference: Abstraction vs Encapsulation

Feature Abstraction Encapsulation

Focus Hiding implementation Hiding data

Achieved using Abstract classes Access specifiers

Level Design level Implementation level

Example ATM interface Bank account balance protection

Combined Program Demonstrating Abstraction


[Link] in Python
Definition

Encapsulation is an Object-Oriented Programming concept that binds data (variables) and


methods (functions) together inside a class and restricts direct access to some components of
the object.

It protects data from unauthorized access and ensures controlled interaction.

Purpose of Encapsulation
Encapsulation is used to:

• Protect sensitive data

• Prevent accidental modification

• Improve data security

• Maintain code integrity

• Achieve data hiding


Conclusion

Encapsulation ensures data protection by restricting direct access to variables and allowing
controlled access through methods. It enhances security and maintains data integrity in object-
oriented programming.

You might also like