Concept | Description |
---|---|
Class | A blueprint for creating objects |
Object | An instance of a class |
Encapsulation | Restricting direct access to data (hiding details) |
Abstraction | Hiding implementation details and exposing only functionality |
Inheritance | Allowing a class to acquire properties/methods from another class |
Polymorphism | Using the same method name for different functionalities |
# Creating a class
class Car:
def __init__(self, brand, model, year): # Constructor
self.brand = brand
self.model = model
self.year = year
def display_info(self): # Method
print(f"Car: {self.brand} {self.model} ({self.year})")
# Creating an object (instance)
my_car = Car("Tesla", "Model S", 2022)
# Calling a method
my_car.display_info() # Output: Car: Tesla Model S (2022)
Encapsulation restricts direct access to class attributes and methods.
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute (__)
def deposit(self, amount):
self.__balance += amount
def get_balance(self): # Getter method
return self.__balance
# Creating an object
account = BankAccount(500)
account.deposit(200)
print(account.get_balance()) # Output: 700
# print(account.__balance) # ❌ Error: Cannot access private variable directly
Abstraction hides complex details and exposes only essential features.
from abc import ABC, abstractmethod
class Animal(ABC): # Abstract class
@abstractmethod
def sound(self):
pass
class Dog(Animal): # Concrete class
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
dog = Dog()
print(dog.sound()) # Output: Bark
Inheritance allows a class to acquire properties and methods from another class.
class Parent:
def show(self):
print("This is Parent class")
class Child(Parent): # Inheriting Parent class
def display(self):
print("This is Child class")
obj = Child()
obj.show() # Output: This is Parent class
obj.display() # Output: This is Child class
? Multiple Inheritance
class A:
def method_A(self):
print("Class A method")
class B:
def method_B(self):
print("Class B method")
class C(A, B): # Inheriting both A and B
def method_C(self):
print("Class C method")
obj = C()
obj.method_A() # Output: Class A method
obj.method_B() # Output: Class B method
? Multilevel Inheritance
class Grandparent:
def grandparent_method(self):
print("Grandparent method")
class Parent(Grandparent):
def parent_method(self):
print("Parent method")
class Child(Parent):
def child_method(self):
print("Child method")
obj = Child()
obj.grandparent_method() # Output: Grandparent method
obj.parent_method() # Output: Parent method
obj.child_method() # Output: Child method
Polymorphism allows using the same method name with different implementations.
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self): # Overriding parent method
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
animals = [Dog(), Cat()]
for animal in animals:
print(animal.sound())
# Output:
# Bark
# Meow
Python does not support method overloading, but we can achieve it using default arguments.
class Math:
def add(self, a, b, c=0): # Overloading using default argument
return a + b + c
math_obj = Math()
print(math_obj.add(2, 3)) # Output: 5
print(math_obj.add(2, 3, 4)) # Output: 9
Python has special methods, also called dunder (double underscore) methods.
Method | Purpose |
---|---|
__init__() |
Constructor (initializes objects) |
__str__() |
Returns string representation |
__len__() |
Returns length of object |
__add__() |
Operator overloading for + |
__eq__() |
Checks equality (== ) |
Example: Operator Overloading
class Book:
def __init__(self, pages):
self.pages = pages
def __add__(self, other):
return self.pages + other.pages # Overloading + operator
book1 = Book(100)
book2 = Book(200)
print(book1 + book2) # Output: 300
✔ Class & Object - Blueprint & instance
✔ Encapsulation - Hides internal details
✔ Abstraction - Shows only essential details
✔ Inheritance - Reusability of code
✔ Polymorphism - Same method, different behaviors
✔ Magic Methods - Special dunder methods
Scope @ NareshIT:
At NareshIT’s Python application Development program you will be able to get the extensive hands-on training in front-end, middleware, and back-end technology.
It skilled you along with phase-end and capstone projects based on real business scenarios.
Here you learn the concepts from leading industry experts with content structured to ensure industrial relevance.
An end-to-end application with exciting features
Course :