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

Data Structures

The document explains Abstract Data Types (ADTs) as logical models defining data and operations without implementation details, emphasizing data abstraction and modularity. It also covers classes in Object-Oriented Programming (OOP), detailing their structure, features, and the relationship between classes and objects. Additionally, it discusses inheritance types in OOP, highlighting their significance in code reuse and organization.

Uploaded by

kalaiselvik
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)
2 views32 pages

Data Structures

The document explains Abstract Data Types (ADTs) as logical models defining data and operations without implementation details, emphasizing data abstraction and modularity. It also covers classes in Object-Oriented Programming (OOP), detailing their structure, features, and the relationship between classes and objects. Additionally, it discusses inheritance types in OOP, highlighting their significance in code reuse and organization.

Uploaded by

kalaiselvik
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

Abstract Data Type (ADT)

🔹 Definition

An ADT (Abstract Data Type) is a logical model that defines:

 Data

 Operations on data
without specifying how they are implemented.

🔹 Key Points

 Focuses on what operations are performed

 Hides implementation details

 Provides data abstraction

 Improves modularity and reusability

🔹 Example

Stack ADT

 Operations: push(), pop(), peek()

 Implementation: array or linked list (not shown in ADT)

🔹 Real-life Example

 ATM machine → You use functions (withdraw, deposit)


but don’t know internal working

📘 Classes

🔹 Definition

A class is a blueprint for creating objects.


It contains:

 Variables (data)

 Methods (functions)

🔹 Key Points

 Used to implement ADTs

 Supports OOP concepts

 Objects are instances of a class

🔹 Example (Python)

class Student:
def __init__(self, name):
[Link] = name
def display(self):
print([Link])

🔹 Real-life Example

 Class = Blueprint of a house

 Object = Actual house built from blueprint

📘 Difference Between ADT and Class

ADT Class

Abstract (concept) Concrete (implementation)

Defines what operations Defines how operations

No code Contains code

Example: Stack

Quick Summary

 ADT → idea, behavior

 Class → implementation using code

 ADT hides details, class provides them

 ntroduction to Object-Oriented Programming (OOP)

 🔹 Definition

 Object-Oriented Programming (OOP) is a programming approach based on objects and


classes, used to model real-world entities in software.

 🔹 Key Concepts

 1. Class

 Blueprint for creating objects

 Contains data (variables) and methods (functions)

 2. Object

 Instance of a class

 Represents real-world entity

 👉 Example:
Class = Student
Object = John, Ravi

 🔹 Main Features of OOP

 Encapsulation

 Wrapping data and methods together

 Protects data from outside access

 Inheritance

 One class inherits properties of another

 Promotes code reuse

 Polymorphism

 Same function behaves differently

 Example: same method name, different actions

 Abstraction

 Hides internal details

 Shows only necessary features

 🔹 Advantages

 Code reusability

 Easy maintenance

 Better organization

 Models real-world problems effectively

 🔹 Real-life Example

 Car

 Data: speed, color

 Functions: start(), stop()


➡️Car is an object, designed using a class

 ✅ Quick Summary

 OOP = programming using objects


 Class = blueprint

 Object = real instance

 Focus = real-world modeling and reusability

 If you want, I can also provide:


✅ Short 2-mark answers
✅ MCQs
✅ Diagrams for OOP concepts

 explain with real time example

 Here is a simple explanation of OOP with a real-time example 👇

 📘 Object-Oriented Programming (OOP) – Real-Time Example

 🔹 Concept

 OOP means designing programs using real-world objects that contain:

 Data (attributes)

 Functions (methods)

 🚗 Real-Time Example: Car System

 Think of a Car 🚗

 🔹 Class

 Car is a class (blueprint)

 🔹 Object

 Specific cars like:

 Honda City

 Toyota Innova
➡️These are objects

 🔹 Properties (Data)

 Color

 Speed
 Fuel

 🔹 Methods (Functions)

 start()

 stop()

 accelerate()

 🔑 OOP Features with Real Example

 1. Encapsulation

 👉 Combining data + methods

 🔹 Example:

 Car engine details are hidden

 You only use steering, brake

 2. Inheritance

 👉 One class gets features of another

 🔹 Example:

 Vehicle → Car
➡️Car inherits wheels, engine

 3. Polymorphism

 👉 Same function, different behavior

 🔹 Example:

 start():

 Bike → kick start

 Car → key/remote start

 4. Abstraction

 👉 Hiding complexity

 🔹 Example:
 You drive a car without knowing engine mechanism

 💻 Simple Python Example

 class Car:
def __init__(self, color):
[Link] = color

def start(self):
print("Car started")

c1 = Car("Red")
[Link]()

 ✅ Easy Summary

 Class = blueprint (Car)

 Object = real item (Honda car)

 OOP = real-world modeling

 Focus = data + behavior together

 Classes in Python (Detailed Explanation)

🔹 What is a Class?

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


It groups:

 Data (attributes/variables)

 Functions (methods)

➡️It helps in implementing Object-Oriented Programming (OOP).

🔹 Real-Time Analogy 🏠

 Class = House blueprint

 Object = Actual house built from blueprint

You can create many houses (objects) using one blueprint (class).
🔹 Structure of a Class

class ClassName:
def __init__(self, parameters):
[Link] = parameters

def method(self):
# code

🔹 Important Components

1. Class Definition

 Defined using class keyword

class Student:
pass

2. Constructor (__init__)

 Special method automatically called when object is created

 Used to initialize variables

def __init__(self, name):


[Link] = name

3. Object Creation

s1 = Student("John")

 s1 is an object of class Student

4. Instance Variables

 Variables defined using self

 Unique for each object

[Link] = name

5. Methods

 Functions defined inside a class

 Used to perform operations

def display(self):
print([Link])
🔹 Complete Example

class Student:
def __init__(self, name, marks):
[Link] = name
[Link] = marks

def display(self):
print("Name:", [Link])
print("Marks:", [Link])

s1 = Student("John", 90)
s2 = Student("Ravi", 85)

[Link]()
[Link]()

🔹 Output

Name: John
Marks: 90
Name: Ravi
Marks: 85

🔹 Types of Variables in Class

1. Instance Variables

 Belong to object

 Defined using self

2. Class Variables

 Shared by all objects

class Student:
school = "ABC School" # class variable

🔹 Types of Methods

1. Instance Methods

 Work with object data

2. Class Methods

 Work with class variables


 Use @classmethod

3. Static Methods

 Independent of class/object

 Use @staticmethod

🔹 Example with All Methods

class Demo:
x = 10 # class variable

def __init__(self, val):


[Link] = val

def show(self): # instance method


print([Link])

@classmethod
def display(cls):
print(cls.x)

@staticmethod
def greet():
print("Hello")

🔹 Key Features of Classes

 Encapsulation (data + methods together)

 Code reusability

 Easy maintenance

 Modular programming

🔹 Advantages

 Models real-world entities

 Reduces code duplication

 Improves readability

🔹 Real-Time Example 🚗

Class: Car
 Attributes: color, speed

 Methods: start(), stop()

Objects:

 Red Car

 Blue Car

Each car has its own data but same behavior.

✅ Final Summary

 Class = blueprint

 Object = instance

 __init__() = constructor

 self = current object

 Used to implement OOP concepts

 nheritance is an important concept in Object-Oriented Programming (OOP) where one class


(called the child class or derived class) acquires the properties and methods of another class
(called the parent class or base class). This helps in code reuse and reduces duplication.

 In Python, inheritance allows us to use existing code and extend it with new features. The
child class can use all the functions and variables of the parent class and can also add its own
features or modify existing ones.

 🔹 Syntax

 class Parent:
def show(self):
print("This is parent class")

class Child(Parent):
def display(self):
print("This is child class")

 🔹 Example

 class Animal:
def sound(self):
print("Animals make sound")

class Dog(Animal):
def bark(self):
print("Dog barks")

d = Dog()
[Link]()
[Link]()

 🔹 Output

 Animals make sound


Dog barks

 🔹 Types of Inheritance

 Inheritance (Overview)

 Inheritance is a feature of OOP where one class (child) acquires properties and methods of
another class (parent).
It helps in:

 Code reuse

 Reducing redundancy

 Better organization

 📘 1. Single Inheritance

 🔹 Definition

 A child class inherits from only one parent class.

 💻 Program

 class Animal:
def eat(self):
print("Animal is eating")

class Dog(Animal):
def bark(self):
print("Dog is barking")

d = Dog()
[Link]()
[Link]()
 🔍 Explanation

 Animal is the parent class with method eat()

 Dog is the child class inheriting from Animal

 Dog automatically gets access to eat()

 ▶ Execution Flow

 Object d is created

 [Link]() → Python searches in Dog → not found → checks Animal → found

 [Link]() → found in Dog

 🌍 Real-Time Example

 Person → Student
Student inherits basic details like name, age

 📘 2. Multiple Inheritance

 🔹 Definition

 A child class inherits from more than one parent class.

 💻 Program

 class Father:
def skill1(self):
print("Father: Driving")

class Mother:
def skill2(self):
print("Mother: Cooking")

class Child(Father, Mother):


def skill3(self):
print("Child: Playing")

c = Child()
c.skill1()
c.skill2()
c.skill3()

 🔍 Explanation

 Father and Mother are parent classes

 Child inherits from both

 Child gets methods from both parents


 ▶ Execution Flow

 c.skill1() → found in Father

 c.skill2() → found in Mother

 c.skill3() → found in Child

 ⚠ Important Concept: Method Resolution Order (MRO)

 Python checks parents left to right

 Example: Child(Father, Mother) → Father checked first

 🌍 Real-Time Example

 A child inherits:

 Skills from father

 Skills from mother

 📘 3. Multilevel Inheritance

 🔹 Definition

 A class inherits from a parent, and another class inherits from that child (chain structure).

 💻 Program

 class Grandparent:
def house(self):
print("Grandparent owns house")

class Parent(Grandparent):
def car(self):
print("Parent owns car")

class Child(Parent):
def bike(self):
print("Child owns bike")

c = Child()
[Link]()
[Link]()
[Link]()

 🔍 Explanation

 Grandparent → base class

 Parent inherits Grandparent

 Child inherits Parent


 ▶ Execution Flow

 [Link]() → not in Child → not in Parent → found in Grandparent

 [Link]() → found in Parent

 [Link]() → found in Child

 🌍 Real-Time Example

 Grandfather → Father → Son


Properties pass through generations

 📘 4. Hierarchical Inheritance

 🔹 Definition

 Multiple child classes inherit from one parent class.

 💻 Program

 class Animal:
def eat(self):
print("Animal eats food")

class Dog(Animal):
def bark(self):
print("Dog barks")

class Cat(Animal):
def meow(self):
print("Cat meows")

d = Dog()
c = Cat()

[Link]()
[Link]()

 🔍 Explanation

 Animal is the parent class

 Dog and Cat are child classes

 Both inherit eat()

 ▶ Execution Flow

 [Link]() → Dog → not found → Animal

 [Link]() → Cat → not found → Animal


 🌍 Real-Time Example

 Vehicle → Car, Bike, Bus


All share common features (engine, speed)

 📊 Comparison Table

 Type  Structure  Key Idea

 Single  One → One  Simple inheritance

 Multiple  Many → One  Combines features

 Multilevel  Chain  Step-by-step inheritance

 Hierarchical  One → Many  Shared parent


 🧠 Key Points to Remember

 Inheritance promotes code reuse

 Child class can:

 Use parent methods

 Add new methods

 Python follows MRO (left to right) in multiple inheritance

 Reduces duplication and improves readability

 ✅ Final Summary

 Inheritance is a powerful OOP feature that allows classes to share and reuse code. Different
types of inheritance represent different real-world relationships like parent-child, multiple
sources, generation chains, and shared hierarchies.

 If you want next:

 🔹 Real-Time Example 🚗

 Vehicle (Parent Class) → has speed, engine

 Car (Child Class) → inherits these features and adds more like AC, music system

 🔹 Advantages

 Code reusability

 Easy maintenance

 Improves readability

 Supports hierarchical classification

 ✅ Summary

 Inheritance allows one class to reuse the properties and methods of another class, making
programs more efficient, organized, and easier to maintain.

1. Single Inheritance
🔹 Diagram Idea

Parent

Child

🔹 Explanation

 One parent class → one child class


 Child gets all features from parent

🌍 Example

Person → Student

📘 2. Multiple Inheritance
🔹 Diagram Idea

Parent1 Parent2
\ /
\ /
Child

🔹 Explanation

 One child inherits from multiple parents

 Combines features from both

🌍 Example

Father + Mother → Child

📘 3. Multilevel Inheritance
5

🔹 Diagram Idea

Grandparent

Parent

Child

🔹 Explanation

 Inheritance happens in levels (chain)

 Each level passes features to next

🌍 Example

Grandfather → Father → Son

📘 4. Hierarchical Inheritance
4

🔹 Diagram Idea

Parent
/ \
Child1 Child2

🔹 Explanation

 One parent → multiple children

 All children share same parent features

🌍 Example

Vehicle → Car, Bike, Bus

📊 Final Visual Summary

Single Multiple Multilevel Hierarchical


A A B A A
| \/ | / \
B C B B C
|
C
🧠 Easy Memory Trick

 Single → Straight line

 Multiple → V-shape

 Multilevel → Chain

 Hierarchical → Tree

1. Namespace – Example Program

Explanation:

A namespace stores variables and their values. Python checks variables using the LEGB rule (Local →
Enclosing → Global → Built-in).

Program:

x = 50 # Global namespace

def outer():
x = 30 # Enclosing namespace

def inner():
x = 10 # Local namespace
print("Local x:", x)

inner()
print("Enclosing x:", x)

outer()
print("Global x:", x)

Output:

Local x: 10
Enclosing x: 30
Global x: 50

Explanation:

 Inside inner() → Python uses local x = 10

 In outer() → Python uses enclosing x = 30

 Outside → Python uses global x = 50

Real-Time Idea:
Think of same name "x" used in different departments of a company → each has its own meaning.

2. Shallow Copy – Example Program

Explanation:

Creates a new object, but nested objects are shared.

Program:

import copy

original = [[1, 2], [3, 4]]


shallow = [Link](original)

# Modify nested element


shallow[0][0] = 100

print("Original:", original)
print("Shallow Copy:", shallow)

Output:

Original: [[100, 2], [3, 4]]


Shallow Copy: [[100, 2], [3, 4]]

Explanation:

 Only outer list is copied

 Inner lists are shared

 So change affects both

Real-Time Example:

Two people sharing the same Google Doc → edits affect both.

Visual Idea:
3. Deep Copy – Example Program

Explanation:

Creates a new object and copies everything recursively.

Program:

import copy

original = [[1, 2], [3, 4]]


deep = [Link](original)

# Modify nested element


deep[0][0] = 100

print("Original:", original)
print("Deep Copy:", deep)

Output:

Original: [[1, 2], [3, 4]]


Deep Copy: [[100, 2], [3, 4]]
Explanation:

 Both outer and inner objects are copied

 Changes do not affect original

Real-Time Example:

Two people having separate notebooks → changes are independent.

Visual Idea:
Final Summary

 Namespace → Controls variable scope (LEGB rule)

 Shallow Copy → Copies outer object, shares inner objects

 Deep Copy → Fully independent copy of all object

You might also like