Procedural vs object-oriented programming
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Procedural programming builds a program from steps that work on separate data. Object-oriented programming (OOP) builds it from objects that keep their own data and actions together. Java Basics used the first style; large Java programs use the second.
Procedural programming
Procedural programming is a style of programming in which a program is a sequence of steps written as procedures (in Java, methods), and the data is kept separately and passed to those procedures.
Every program in Java Basics followed one plan: keep the data in variables, then call static methods to do each step. The steps come first, and the data is handed to them from outside.
Example 1 · A procedural marks report
public class ProceduralReport { static int total(int maths, int physics, int chemistry) { return maths + physics + chemistry; }
static void printReport(String name, int totalMarks) { System.out.println(name + " scored " + totalMarks + " out of 300"); }
public static void main(String[] args) { String name = "Aarav"; int maths = 72; int physics = 65; int chemistry = 80;
int sum = total(maths, physics, chemistry); printReport(name, sum); }}Aarav scored 217 out of 300
What this code does
- The data (
nameand three marks) sits in plain variables insidemain. - The steps are
staticmethods:total()adds,printReport()prints. Each one gets the data it needs from outside, as arguments. - This is the procedural pattern from Why use methods? (opens in a new tab): steps first, data passed in.
This is short and clear for one student. Procedural code suits small programs like calculators, converters and the Basics mini projects. The trouble starts when the program must hold many students, each with many details.
When procedural code grows: loose variables and parallel arrays
Parallel arrays are two or more arrays in which the values at the same index belong to the same thing, such as one student's name in a names array and marks in a marks array.
Store three students the procedural way and you get loose variables: name1, marks1, name2, marks2. Java does not know that name1 and marks1 belong together; only the digit in their names links them.
Example 2 · Loose variables for each student
public class LooseVariablesDemo { public static void main(String[] args) { String name1 = "Aarav"; int marks1 = 72; String name2 = "Priya"; int marks2 = 91; String name3 = "Kavya"; int marks3 = 85;
System.out.println(name1 + ": " + marks1); System.out.println(name2 + ": " + marks2); System.out.println(name3 + ": " + marks3); }}Aarav: 72 Priya: 91 Kavya: 85
What this code does
- Two variables per student, and three almost identical print lines. A class of 60 students needs 120 such variables.
- Nothing in the code ties
name2tomarks2. Printname2withmarks3by mistake and the program still compiles and runs, with a wrong report.
Example 3 · Parallel arrays
public class ParallelArraysDemo { public static void main(String[] args) { String[] names = {"Aarav", "Priya", "Kavya"}; int[] marks = {72, 91, 85}; int[] attendance = {80, 95, 70};
for (int i = 0; i < names.length; i++) { System.out.println(names[i] + ": " + marks[i] + " marks, " + attendance[i] + "%"); } }}Aarav: 72 marks, 80% Priya: 91 marks, 95% Kavya: 85 marks, 70%
What this code does
- Arrays shorten the code: one loop prints every student, however many there are.
- These are parallel arrays: one student's details are spread over three arrays and joined only by the index
i.
Example 4 · Sorting one array breaks the link
public class BrokenLinkDemo { public static void main(String[] args) { String[] names = {"Aarav", "Priya", "Kavya"}; int[] marks = {72, 91, 85};
for (int i = 0; i < marks.length - 1; i++) { for (int j = i + 1; j < marks.length; j++) { if (marks[j] > marks[i]) { int temp = marks[i]; marks[i] = marks[j]; marks[j] = temp; } } } System.out.println("Topper: " + names[0] + " with " + marks[0]); }}Topper: Aarav with 91
What this code does
- The loops sort
marksfrom highest to lowest, butnamesis never touched, so it still starts with Aarav. - Aarav gets Priya's 91. No error and no crash, just a wrong answer: the most dangerous kind of bug.
Every change must touch every array in exactly the same way. Sort one array and the names go to the wrong marks, as Example 4 shows. Add a student to names but not to marks, and a loop over both stops with ArrayIndexOutOfBoundsException, as in A bad index (opens in a new tab).
The root problem is the same in both: one student's details live in different places, and nothing in the code keeps them together.
Object-oriented programming: data and actions together
Object-oriented programming (OOP) is a way of designing a program as a set of objects that work together. Each object holds its own data and the actions that use that data.
OOP fixes the root problem by keeping each student's details and actions in one place. Here is the same student list written as Student objects. Read it for the idea, not the syntax.
Example 5 · The same students as objects
class Student { String name; int marks;
void show() { System.out.println(name + ": " + marks); }}
public class StudentObjectsDemo { public static void main(String[] args) { Student s1 = new Student(); s1.name = "Aarav"; s1.marks = 72;
Student s2 = new Student(); s2.name = "Priya"; s2.marks = 91;
s1.show(); s2.show();
Student topper = s1; if (s2.marks > s1.marks) { topper = s2; } System.out.println("Topper: " + topper.name + " with " + topper.marks); }}Aarav: 72 Priya: 91 Topper: Priya with 91
What this code does
class Studentsays what every student has (name,marks) and what it can do (show()). Eachnew Student()makes one student object.s1.nameands1.markslive inside the same object, so they cannot drift apart, andshow()sits right next to the data it prints.topperpicks a whole student, so Priya's name comes with Priya's 91. Example 4 printed Aarav with 91.
You do not need to write this syntax yet. Classes as blueprints (opens in a new tab) shows how to write a class, and Objects and instances (opens in a new tab) shows how to create objects with new.
To find the classes in any problem, use the nouns and verbs trick. The nouns (student, book, account) become classes, and their details become fields, the data each object keeps. The verbs (pay, issue, show) become methods.
Procedural vs object-oriented: side by side
Here is how the two styles compare on the points examiners and interviewers ask about most.
- Focus: procedural starts from the steps (read, calculate, print); OOP starts from the things (student, account, ticket).
- Data: procedural keeps data in separate variables and arrays and passes it to methods; OOP keeps each thing's data inside its object, next to its methods.
- Data hiding (security): procedural data is open, so any line can change it; OOP can hide an object's data and allow changes only through its own methods.
- Reuse: procedural reuses single methods; OOP reuses whole classes: write
Studentonce, create 60 students from it. - Real-world modelling: procedural code shows steps, so the real things are hard to see; in OOP each real thing becomes a class.
- Suited for: procedural fits small programs and one-off calculations; OOP fits large programs that grow, like banking, railway booking or college apps.
C is the best-known procedural language. Java is object-oriented, but its methods still run step by step inside each class.
Why OOP: the benefits
Real apps have thousands of students, payments or tickets and are changed every month by many people. That is where OOP pays off:
- Easier to maintain: a change to how a student works is made in one class, not in every array and method that touches student data.
- Reuse: one class gives any number of objects; the same
Studentclass serves a class of 60 or a university of 6,000. - Models the real world: a railway app has passengers, trains and tickets, and the code has
Passenger,TrainandTicket, so the design is easy to explain. - Protects data: an object can refuse wrong changes, like marks above 100 or a wallet balance below Rs. 0. The Encapsulation module shows how.
- Teamwork in big projects: each developer can own a few classes, and others use them through their methods without reading every line.
OOP is not always better. A program that converts Celsius to Fahrenheit or adds two numbers is clearer as a few steps in main. Choose OOP when there are many things with their own data and the program will keep growing.
Next step
Check your understanding
11 quick questions