What is a Class in Python?
A class is like a blueprint or a template to create objects.
Real-Life Example:
class called Car. The class defines:
properties/Attributes b) functions/methods
What attributes/properties each car will have (like color, brand, mileage)
What methods/actions it can do (like start(), stop())
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def start(self):
print(f"{self.color} {self.brand} car
is starting")
obj1=Car()
An object is a runtime instance of a class.
It contains the actual data and can access both attributes (variables) and methods (functions)
defined in the class.
Every object in Python:
Occupies memory
Has a unique identity (like an ID)
Belongs to a specific class
What is self
It refers to the object through which the method is called.
We must include self in method definitions, even though you don't pass it manually when
calling the method.
class student:
#properties
#functions
def hello(self
print("My name is Gaju")
def bye(self):
print("Good night everyone")
def square(n):
return n*n
mounika = student()
abhi = student()
satya = student()
What is the use of __init__() function in Python?
The __init__() method is known as the constructor in Python.
Main Use:
It is automatically called when an object is created from a class.
Its main purpose is to initialize the object’s attributes with default or user-provided values.
What is the difference between class variables and instance variables in Python?
Class variables are unique for each object instance variables are shared across objects.
it is defined inside the class but outside any methods.
]
class Car:
wheels = 4 # class variable (shared)
def __init__(self, brand, color):
self.brand = brand # instance variable
self.color = color
c1 = Car("Maruti", "Black")
c2 = Car("Mercedez", "Red")
print(c1.wheels)
print(c2.wheels)
Four pillars of OOPs
1)Encapsulation
2)inheritance
3)polymorphism
4)Abstraction
What is encapsulation, and how is it implemented in Python?
Answer:
Encapsulation restricts access to certain attributes and methods, protecting an object’s internal
state. In Python, attributes can be made "protected" by prefixing them with a single
underscore (_protected) or "private" with double underscores (__private). Protected attributes
signal that they shouldn’t be accessed directly outside the class, and private attributes are
name-mangled, making it difficult to access them outside the class.
class BankAccount:
def __init__(self, balance):
# initializing private property - will be accessible within
the class.
self.__balance = balance # private
def withdraw(self, amount):
self.__balance -= amount
def deposit(self, amount):
self.__balance += amount
def check_balance(self):
return self.__balance
What is polymorphism in Python, and how does it work?
Polymorphism allows different classes to be used with a common interface or method name. In Python,
polymorphism can be achieved through method overriding (having different implementations of the same method in
subclasses) and operator overloading (customizing behavior for built-in operators).
class Football:
def play(self):
return "Kick the football"
class Guitar:
def play(self):
return "Strum the guitar"
class VideoGame:
def play(self):
return "Press buttons on the controller"
# Polymorphism in action
toys = [Football(), Guitar(), VideoGame()]
for t in toys:
print(t.play())
What is inheritance in Python, and how does it work?
Inheritance in Python is a feature of object-oriented programming that allows a class (child class) to inherit attributes
and methods from another class (parent class). This promotes code reuse and hierarchy. The child class can override
or extend the functionality of the parent class.
Inheritance is implemented by specifying the parent class in parentheses when defining the child class.
class Parent:
def __init__(self):
print("Parent class")
class Child(Parent): #inheritance
def __init__(self):
super(). __init__()
print("Child class")
What is abstraction in Python, and how is it different from encapsulation?
Abstraction is an OOP principle that hides implementation details and only exposes essential features of an object. It
helps in simplifying complex systems by breaking them down into more manageable parts. In Python, abstraction can
be achieved through abstract classes and methods, which act as blueprints for other classes.
Encapsulation, on the other hand, is about restricting access to an object's inner workings and allowing modification
only through well-defined interfaces.