Basics of OOP (Object Oriented Programming)
What is Object-Oriented Programming?
It is a programming paradigm that organizes software design around objects. Objects have both attributes (data) and behaviour (method). It models real-world problems by representing entities as interacting objects, making code more modular, reusable, and maintainable. OOP is built on four pillars: Abstraction, Encapsulation, Inheritance, and Polymorphism. Java, Python, C++, and Ruby support it.
Class and Object
Class
It is a blueprint or template that defines the behaviour of an object. It has attributes (fields) and methods (functions).
#python code
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def drive(self):
print(f"The {self.color} {self.brand} is driving.")
Car is a class that has two attributes: brand and color. It has one function or method called drive that operates on color and brand.
Object
An instance of a class is an object. It is a concrete entity created in memory that holds specific data.
#python code
my_car = Car("Toyota", "Red") # Object creation
my_car.drive()
my_car is a object of class Car (defined above) on which drive is applied.
What is Encapsulation?
It is the bundling of the data and operations that operate on that data into a single unit. Encapsulation restricts access to some of the object’s components by using access modifiers like public, private, and protected.
#Java code
public class BankAccount {
private double balance; // private field
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance; // controlled access
}
}
A class name BankAccount is defined here with fields balance and methods deposit and getBalance. The class encapsulates the functions and attributes. Access to some of the fields has been restricted using a private keyword.
What is Inheritance? Why is it useful?
It allows one class (child/subclass) to acquire the properties and behavior of another class (parent/superclass). It promotes code reuse, hierarchical classification, and polymorphic behavior. This reduces code duplication, improves maintainability, and allows generalization and specialisation.
#python code
class Animal:
def speak(self):
print("Some sound")
class Dog(Animal):
def speak(self):
print("Bark")
dog = Dog()
dog.speak() # Output: Bark
Animal is the class that is being inherited by Dog, which allows Dog to reuse features of the Animal class.
What is Polymorphism?
Polymorphism means many forms. It enables the same method to behave differently depending on the object type.
Run-time Polymorphism (Dynamic)
It is done using method overriding. Here, a subclass provides a specific implementation of a parent method. During runtime, the system determines which method will be executed.
#Java code
class Animal { void sound() { System.out.println("Some sound"); } }
class Dog extends Animal { void sound() { System.out.println("Bark"); } }
Compile-time Polymorphism (Static)
It is done using method overloading. The method names are the same, but the parameters are different. The compiler decides which method to execute.
#Java code
class MathUtil {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}

What is Abstraction?
It means hiding complex implementation details and exposing only the essential features of an object. It helps reduce complexity and isolate the impact of changes. Interfaces and abstract classes are the ways to implement abstraction.
What are access specifiers/modifiers (public, private, protected)?
Access specifiers (also called access modifiers) control the visibility and scope of classes, variables, and methods.
Modifier | Access Level | Description |
---|---|---|
public | Everywhere. | Accessible from anywhere |
protected | Access in the same package, same class and subclass. | Used in inheritance |
private | Access in the same class. | Used to hide implementation details |
default | Access in the same package. (Applicable to Java) | Accessible within same package only |
What is the difference between inheritance and composition?
Inhertiance | Composition |
---|---|
Is-A relationship | Has-A relationship |
It forms a hierarchy which can be tightly coupled. | It means that one class contains a field of another class. |
Example of Composition
class Engine:
def start(self):
print("Engine starts")
class Car:
def __init__(self):
self.engine = Engine()
def drive(self):
self.engine.start()
print("Car drives")
Example of Inheritance
class Animal:
def speak(self):
print("Some sound")
class Dog(Animal):
def speak(self):
print("Bark")
dog = Dog()
dog.speak() # Output: Bark
What is the difference between an abstract class and an interface?
Abstract Class | Interface |
---|---|
Defines a contract with some implementation. | It defines a contract. |
Defines methods with some implementation. It can be concrete and abstract. | Defines only methods. It has only abstract methods. |
It can have instance variables. | Variables are static and final. |
Class can extend only one abstract class. | A class can extend only one abstract class. |