π§ Object-Oriented Programming (OOPs) Concepts with Examples and Interview Questions
Object-Oriented Programming (OOPs) is one of the most popular and widely used programming paradigms in modern software development. It allows developers to organize code into objects that represent real-world entities like a car, person, or bank account.
OOPs makes code modular, reusable, and easier to maintain β thatβs why itβs used in languages like Java, C++, Python, C#, Kotlin, Swift, and many others.
Letβs explore the main concepts one by one, with simple examples and common interview questions.
π‘ What is OOPs?
OOPs (Object-Oriented Programming System) is a way of designing and writing programs by using classes and objects.
- Class β Blueprint or template for creating objects.
- Object β An instance of a class that contains data and behavior.
Example:
class Car {
String color;
int speed;
void drive() {
System.out.println("Car is driving at speed " + speed);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object created
myCar.color = "Red";
myCar.speed = 100;
myCar.drive();
}
}
Output:
Car is driving at speed 100
Here,
Caris a class.myCaris an object.- The data (
color,speed) and method (drive()) define the carβs properties and behavior.
π§± 1. Class and Object
β€ Class
A class is a blueprint that defines properties (variables) and behaviors (methods) of an object.
β€ Object
An object is a real-world entity created from a class. It represents a specific instance.
Example:
class Student {
String name;
int age;
void showInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Demo {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Moutusi";
s1.age = 22;
s1.showInfo();
}
}
Output:
Name: Moutusi, Age: 22
𧬠2. Encapsulation
Encapsulation means binding data and methods into a single unit (class) and keeping the data safe from outside interference.
It is achieved by using private variables and public getter/setter methods.
Example:
class Account {
private int balance = 1000;
public int getBalance() {
return balance;
}
public void deposit(int amount) {
if (amount > 0)
balance += amount;
}
}
Here, the variable balance is private, meaning no one can directly access or modify it from outside the class.
Instead, controlled access is given through methods like getBalance() and deposit().
β Benefits:
- Data hiding
- Better control over data
- Secure and flexible design
π³ 3. Inheritance
Inheritance allows one class (child class) to inherit properties and methods from another class (parent class).
It helps in code reusability and establishing a relationship between classes.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
}
}
Output:
This animal eats food
The dog barks
β Types of Inheritance:
- Single Inheritance β One parent, one child.
- Multilevel Inheritance β A class derived from another derived class.
- Hierarchical Inheritance β Multiple classes inherit from one parent.
- Hybrid Inheritance β Combination of different types (supported via interfaces in Java).
- Multiple Inheritance β Not directly supported in Java (to avoid ambiguity).
π 4. Polymorphism
Polymorphism means one name, many forms β it allows the same method or operator to behave differently based on the object or input.
There are two main types:
(a) Compile-time Polymorphism (Method Overloading)
Occurs when multiple methods have the same name but different parameters.
class MathOperation {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Example {
public static void main(String[] args) {
MathOperation obj = new MathOperation();
System.out.println(obj.add(5, 3)); // int version
System.out.println(obj.add(2.5, 4.2)); // double version
}
}
(b) Runtime Polymorphism (Method Overriding)
Occurs when a child class provides a new implementation for a method already defined in the parent class.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Demo {
public static void main(String[] args) {
Animal obj = new Dog(); // Runtime polymorphism
obj.sound();
}
}
Output:
Dog barks
π 5. Abstraction
Abstraction means showing only essential details and hiding unnecessary parts from the user.
It helps simplify complex systems by focusing only on what an object does, not how it does it.
In Java, abstraction is achieved using abstract classes and interfaces.
Example (Abstract Class):
abstract class Shape {
abstract void draw(); // abstract method
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
Output:
Drawing a Circle
Here, we donβt need to know how draw() works β we just know it draws something.
π§© 6. Constructors
A constructor is a special method that initializes an object when itβs created.
It has the same name as the class and no return type.
Example:
class Employee {
String name;
int id;
Employee(String n, int i) {
name = n;
id = i;
}
void display() {
System.out.println("Name: " + name + ", ID: " + id);
}
}
public class Company {
public static void main(String[] args) {
Employee e1 = new Employee("Rohit", 101);
e1.display();
}
}
βοΈ 7. Interface
An interface is a collection of abstract methods. It defines what a class should do but not how.
Example:
interface Vehicle {
void start();
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike starts with a kick");
}
}
public class Demo {
public static void main(String[] args) {
Vehicle v = new Bike();
v.start();
}
}
π§ OOPs Interview Questions for Freshers
1. What is the difference between a class and an object?
- Class is a blueprint, while object is an instance of a class.
2. What is inheritance and why is it used?
- Inheritance lets a child class use properties and methods of a parent class. It helps in code reusability.
3. What is encapsulation?
- Wrapping data and code into a single unit and hiding implementation details.
4. What is polymorphism?
- The ability of an object or method to take many forms (method overloading and overriding).
5. What is abstraction?
- Showing only essential information and hiding the complex details from the user.
6. Can we achieve multiple inheritance in Java?
- Not directly through classes, but it can be achieved using interfaces.
7. What is the difference between abstraction and encapsulation?
- Abstraction focuses on hiding implementation.
- Encapsulation focuses on hiding data.
8. What are constructors and their types?
- Constructors initialize objects.
Types: Default, Parameterized, and Copy constructor (in C++).
9. Can we overload constructors?
- Yes, just like methods, constructors can also be overloaded.
10. What is method overriding?
- When a child class provides its own implementation of a method defined in the parent class.
π§Ύ Summary Table
| Concept | Description | Example |
|---|---|---|
| Class | Blueprint of object | class Car {} |
| Object | Instance of a class | Car c = new Car(); |
| Encapsulation | Data hiding | Private variables with getters/setters |
| Inheritance | Reuse parent class code | class Dog extends Animal |
| Polymorphism | Many forms | Overloading / Overriding |
| Abstraction | Hiding details | Abstract classes or interfaces |
π Final Thoughts
OOPs makes programming more organized, readable, and efficient.
When you understand concepts like Encapsulation, Inheritance, Polymorphism, and Abstraction, you can easily work on real-world applications like banking systems, hospital management, or e-commerce platforms.
For freshers, mastering OOPs is the first big step towards becoming a good developer.