Object-Oriented Programming (OOP): Introduction to classes and objects, inheritance, polymorphism, and encapsulation
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects. In OOP, objects are instances of classes, which act as blueprints for creating objects. Let's dive into the core concepts of OOP:
Classes and Objects
A class is a template for creating objects. It defines the properties (attributes) and behaviors (methods) that all objects created from that class will have. Here's an example of a simple class in Python:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"{self.make} {self.model}")
To create an object from the Car
class:
my_car = Car("Toyota", "Corolla")
my_car.display_info()
Output:
Toyota Corolla
Inheritance
Inheritance allows a class to inherit properties and behaviors from another class. The class that is inherited from is called the parent class, while the class that inherits is called the child class. Here's an example of inheritance in Python:
class ElectricCar(Car):
def __init__(self, make, model, battery_capacity):
super().__init__(make, model)
self.battery_capacity = battery_capacity
The ElectricCar
class inherits from the Car
class. We can create an object from the ElectricCar
class and access properties from both classes:
my_electric_car = ElectricCar("Tesla", "Model S", "100 kWh")
my_electric_car.display_info()
print(my_electric_car.battery_capacity)
Output:
Tesla Model S
100 kWh
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. This enables different classes to be used interchangeably. Here's an example of polymorphism in Python:
def display_car_info(car):
car.display_info()
We can pass objects of both the Car
and ElectricCar
classes to the display_car_info
function:
display_car_info(my_car)
display_car_info(my_electric_car)
Output:
Toyota Corolla
Tesla Model S
Encapsulation
Encapsulation is the principle of restricting access to certain properties and methods of a class. This is achieved by using access modifiers like public, private, and protected. Here's an example of encapsulation in Python:
class Person:
def __init__(self, name, age):
self.name = name
self.__age = age
def display_info(self):
print(f"{self.name}, {self.__age} years old")
We can access the name
property directly, but the age
property is private and can only be accessed via methods:
person = Person("Alice", 30)
print(person.name)
person.display_info()
print(person.__age) # This will raise an error
Output:
Alice
Alice, 30 years old
Common Use Cases
Object-Oriented Programming is widely used in software development for creating modular, reusable, and maintainable code. It is particularly useful in developing large-scale applications where code organization and scalability are important.
Importance in Interviews
Understanding Object-Oriented Programming concepts like classes, objects, inheritance, polymorphism, and encapsulation is crucial for software development interviews. Interviewers often ask questions related to OOP to assess a candidate's understanding of fundamental programming concepts.
Conclusion
Object-Oriented Programming is a powerful paradigm that helps in creating well-structured and efficient code. By understanding the concepts of classes and objects, inheritance, polymorphism, and encapsulation, developers can build robust and scalable applications.