In the world of programming, different approaches exist to solve problems and build software. One of the most powerful and widely used approaches is Object-Oriented Programming, commonly called OOP. Unlike traditional procedural programming, which focuses on step-by-step instructions, OOP organizes software design around objects-real-world entities or concepts represented in code.
Why is OOP important? It helps programmers create modular, reusable, and maintainable code by mimicking how we naturally perceive the world: as objects with properties and behaviors. This approach simplifies complex software development and is the foundation of many modern programming languages like Java, C++, Python, and C#.
In this section, we will explore the core ideas of OOP, starting from the basics and gradually moving to advanced concepts, all explained with clear examples and diagrams to help you master the topic for your competitive exams.
At the heart of OOP are two fundamental concepts: classes and objects.
A class is like a blueprint or template. Think of it as a plan for creating something. For example, consider the idea of a Car. A class named Car defines what a car is in terms of its attributes (such as color, speed) and methods (actions it can perform, like start or stop).
An object is a specific instance of a class. If the class is the blueprint, then the object is the actual car built from that blueprint. Each object has its own set of attribute values but shares the structure defined by the class.
For example, you might have two car objects:
Both are cars but have different attribute values.
Attributes (also called properties or fields) are variables that hold data related to an object. For example, a Car has attributes like color and speed.
Methods are functions defined inside a class that describe the behaviors or actions an object can perform. For example, start() and stop() are methods that control a car's movement.
Encapsulation is the concept of bundling data (attributes) and methods that operate on that data into a single unit - the class - and controlling access to that data. It helps protect the internal state of an object from unintended interference and misuse.
Access to class members (attributes and methods) is controlled using access modifiers. The common access modifiers are:
This control ensures that sensitive data is hidden and can only be accessed or modified through well-defined interfaces (methods), maintaining data integrity.
| Access Modifier | Within Class | Within Subclass | Outside Class |
|---|---|---|---|
| Private | Yes | No | No |
| Protected | Yes | Yes | No |
| Public | Yes | Yes | Yes |
Inheritance is a mechanism where a new class, called the child class or subclass, acquires the properties and behaviors (attributes and methods) of an existing class, called the parent class or superclass. This allows code reuse and establishes a natural hierarchy.
For example, consider a parent class Vehicle with attributes like speed and methods like move(). Two child classes, Car and Bike, can inherit these features and also add their own specific attributes or methods.
classDiagram Vehicle <|-- Car Vehicle <|-- Bike class Vehicle { +int speed +void move() } class Car { +int numberOfDoors +void openTrunk() } class Bike { +bool hasCarrier +void ringBell() }Types of Inheritance:
Polymorphism means "many forms." In OOP, it allows methods to behave differently based on the context, even if they share the same name. This makes programs more flexible and extensible.
There are two main types of polymorphism:
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Same method name, different parameters in the same class | Subclass provides new implementation of superclass method |
| Class Relationship | Within the same class | Between superclass and subclass |
| Purpose | To perform similar tasks with different inputs | To change or extend behavior of inherited methods |
| Example | print(int x), print(String s) | Subclass overrides print() method of superclass |
Abstraction is the process of hiding complex implementation details and showing only the essential features to the user. It helps reduce complexity and allows the programmer to focus on what an object does instead of how it does it.
In OOP, abstraction is achieved using abstract classes and interfaces:
Abstraction allows programmers to design flexible and extensible systems.
Book with attributes title and author, and a method displayDetails() that prints these details. Create an object of this class and call the method. Step 1: Define the class Book with attributes and method.
class Book { String title; String author; void displayDetails() { System.out.println("Title: " + title); System.out.println("Author: " + author); }} Step 2: Create an object of Book and assign values.
Book myBook = new Book();myBook.title = "Introduction to OOP";myBook.author = "John Doe";
Step 3: Call the method to display details.
myBook.displayDetails();
Answer: The output will be:
Title: Introduction to OOP
Author: John Doe
Animal with a method sound() that prints "Animal makes a sound". Create a subclass Dog that inherits from Animal and adds a method bark() that prints "Dog barks". Demonstrate calling both methods from a Dog object. Step 1: Define the parent class Animal.
class Animal { void sound() { System.out.println("Animal makes a sound"); }} Step 2: Define the subclass Dog that inherits Animal.
class Dog extends Animal { void bark() { System.out.println("Dog barks"); }} Step 3: Create a Dog object and call methods.
Dog myDog = new Dog();myDog.sound(); // inherited methodmyDog.bark(); // subclass method
Answer: The output will be:
Animal makes a sound
Dog barks
Calculator with two methods named add: one that adds two integers and another that adds three integers. Demonstrate calling both methods. Step 1: Define the Calculator class with overloaded methods.
class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; }} Step 2: Create an object and call both methods.
Calculator calc = new Calculator();int sum1 = calc.add(10, 20);int sum2 = calc.add(5, 15, 25);
Step 3: Print the results.
System.out.println("Sum1: " + sum1);System.out.println("Sum2: " + sum2); Answer: The output will be:
Sum1: 30
Sum2: 45
Shape with a method draw() that prints "Drawing Shape". Create a subclass Circle that overrides draw() to print "Drawing Circle". Show how calling draw() on a Circle object uses the overridden method. Step 1: Define the superclass Shape.
class Shape { void draw() { System.out.println("Drawing Shape"); }} Step 2: Define the subclass Circle overriding draw().
class Circle extends Shape { @Override void draw() { System.out.println("Drawing Circle"); }} Step 3: Create a Circle object and call draw().
Circle c = new Circle();c.draw();
Answer: The output will be:
Drawing Circle
Student with a private attribute rollNumber and public getter and setter methods to access and modify it. Show how encapsulation protects the attribute. Step 1: Define the Student class with private attribute.
class Student { private int rollNumber; public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { if (rollNumber > 0) { this.rollNumber = rollNumber; } else { System.out.println("Invalid roll number"); } }} Step 2: Create an object and use getter/setter.
Student s = new Student();s.setRollNumber(25);System.out.println("Roll Number: " + s.getRollNumber()); Step 3: Attempt to set an invalid roll number.
s.setRollNumber(-5); // Should print error message
Answer: The output will be:
Roll Number: 25
Invalid roll number
When to use: For quick recall of core OOP concepts during exams.
When to use: To understand abstract concepts more concretely.
When to use: To quickly differentiate between overloading and overriding in questions.
When to use: When designing classes or answering questions on encapsulation.
When to use: During exam preparation to improve problem-solving skills.
private for attributes and public getter/setter methods to control access.Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.
Go to practice →