Introduction_to_Object_Oriented_Programming__OOP_
Introduction_to_Object_Oriented_Programming__OOP_
In the previous terms, we have worked extensively with arrays in Java. Today, we'll expand on
this knowledge to understand why we need Object-Oriented Programming (OOP) concepts and
how they help us efficiently organize and manage data.
Suppose we need to store the marks of all students in a particular subject. We can easily do
this using an array.
Example:
● Here, we are using an array to store marks of 100 students for a single subject.
Now, if we want to analyze marks of all students across multiple subjects (e.g., Math,
Science, English), we could create multiple arrays for each subject. However, this approach is
not efficient when we need to analyze all marks of a specific student across different subjects.
Example:
What is a Class?
Explanation:
● The Student class above is not the data itself; it is just a template that defines what
attributes every student object will have.
● To actually store data for a specific student, we need to create an object of the
Student class.
Explanation:
class Student {
String name;
String email;
String phoneNumber;
int[] marks;
// Parameterized Constructor
public Student(String name, String email, String phoneNumber,
int[] marks) {
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.marks = marks;
}
}
Now, let's see how we can use this constructor to create an object:
Output:
Explanation:
● The parameterized constructor allows us to set initial values for the object’s attributes at
the time of creation.
● The this keyword is used to refer to the current object's attributes, differentiating them
from the constructor's parameters.