0% found this document useful (0 votes)
19 views4 pages

OOPS

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including classes, objects, method overloading and overriding, encapsulation, data abstraction, inheritance, and composition. It includes examples demonstrating each concept, such as creating classes, using private variables, and customizing inherited methods. Key takeaways highlight the importance of these concepts in structuring code and promoting reuse.

Uploaded by

anujsonawane49
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)
19 views4 pages

OOPS

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including classes, objects, method overloading and overriding, encapsulation, data abstraction, inheritance, and composition. It includes examples demonstrating each concept, such as creating classes, using private variables, and customizing inherited methods. Key takeaways highlight the importance of these concepts in structuring code and promoting reuse.

Uploaded by

anujsonawane49
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/ 4

OOPS

Object-Oriented Programming (OOP) in Python


Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects,
which bundle data and functionality together. Here’s an overview of the key OOP concepts:

5.1 Creating Classes and Objects


Classes serve as blueprints for objects, defining attributes and behaviors.

Example: Creating a Class


Python

class Car:
def __init__(self, brand, model): # Constructor method
self.brand = brand
self.model = model

def display_info(self):
print(f"Car: {self.brand} {self.model}")

# Creating an object (instance) of Car class


my_car = Car("Toyota", "Corolla")
my_car.display_info()

✔ Output: Car: Toyota Corolla

5.2 Method Overloading and Overriding


✔ Method Overloading – Same method name, different parameter count (not natively supported in
Python).✔ Method Overriding – Child class modifies a parent class method.

Example: Method Overriding


Python

class Animal:
def speak(self):
print("Animals make sounds")

class Dog(Animal):
def speak(self): # Overriding the parent method
print("Dogs bark")

d = Dog()
d.speak()

✔ Output: Dogs bark


5.3 Data Hiding (Encapsulation)
Encapsulation restricts direct access to object properties.

Example: Using Private Variables


Python

class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private variable

def get_balance(self):
return self.__balance # Access through method

account = BankAccount(1000)
print(account.get_balance()) # Output: 1000
print(account.__balance) # ❌ Error: Private variable

5.4 Data Abstraction


Data abstraction hides unnecessary details and exposes only essential functionalities.

Example: Abstract Class


Python
from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius * self.radius

c = Circle(5)
print(c.area()) # Output: 78.5

5.5 Inheritance and Composition


✔ Inheritance – A child class inherits from a parent class.✔ Composition – Instead of inheritance, a
class contains an instance of another class.

Example: Inheritance
Python

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

class Child(Parent):
pass # Inherits everything from Parent

c = Child()
c.show() # Output: This is the parent class

Example: Composition
Python
class Engine:
def start(self):
print("Engine starting...")

class Car:
def __init__(self):
self.engine = Engine() # Composition

def start_car(self):
self.engine.start()

my_car = Car()
my_car.start_car() # Output: Engine starting...

5.6 Customization via Inheritance (Specializing Inherited Methods)


A subclass can modify or extend inherited methods.

Example: Specialized Method


Python

class Vehicle:
def move(self):
print("Vehicle is moving")

class Bicycle(Vehicle):
def move(self): # Specialization of inherited method
print("Bicycle is pedaling")

bike = Bicycle()
bike.move() # Output: Bicycle is pedaling

Key Takeaways
✔ Classes define object structure.
✔ Overriding lets subclasses modify behavior.
✔ Encapsulation hides sensitive data.
✔ Abstraction simplifies interfaces.
✔ Inheritance and composition allow code reuse.
✔ Specialization refines inherited methods.

You might also like