0% found this document useful (0 votes)
2 views

Introduction_to_Object_Oriented_Programming__OOP_

Java Learn OOPS

Uploaded by

listypop75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction_to_Object_Oriented_Programming__OOP_

Java Learn OOPS

Uploaded by

listypop75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

1. Working with Arrays

Suppose we need to store the marks of all students in a particular subject. We can easily do
this using an array.

Example:

int[] mathMarks = new int[100]; // Stores marks of 100 students in


Mathematics

● Here, we are using an array to store marks of 100 students for a single subject.

2. Using 2D Arrays to Store Marks for Multiple Subjects

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.

Instead, we can use a 2D array where:

● Each row represents a student.


● Each column represents a subject.

Example:

int[][] marks = new int[100][5]; // Stores marks of 100 students


across 5 subjects

● Here, marks[i][j] represents the marks of student i in subject j.


● We can iterate over the array:
○ Row-wise: To get marks of all subjects for a specific student.
○ Column-wise: To get marks of all students for a specific subject.
However, even with 2D arrays, there are limitations. What if we need to store additional
information like a student's name, email, or phone number? Arrays are collections of data of the
same type, so storing diverse types of data (like integers, strings, etc.) is not possible with
arrays.

3. The Need for Classes


To overcome the limitations of arrays, we can use Classes in Java.

What is a Class?

● A Class is a blueprint or template that defines the structure of an object.


● It can contain multiple data attributes (also called fields) and methods.
● Using a class, we can store diverse types of data related to a student (marks, name,
email, phone number, etc.) together in a structured way.

Example: Defining a Student Class

Let's define a class Student to hold data related to a student:

// Defining the Student class


class Student {
// Attributes (data members)
String name;
String email;
String phoneNumber;
int[] marks;
}

Explanation:

● Here, we are defining a class called Student.


● The Student class has the following attributes:
○ name (String): to store the student's name.
○ email (String): to store the student's email address.
○ phoneNumber (String): to store the student's phone number.
○ marks (int[]): to store the marks of the student in different subjects.
Understanding the Class

● 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.

4. Creating Objects and Using the Default Constructor


● An object is an instance of a class.
● When we create an object of a class, we use the constructor to allocate memory for it.
● If we do not define any constructors, Java provides a default constructor automatically.

Example: Creating an Object and Setting Values

public class Main {


public static void main(String[] args) {
// Creating an object of the Student class
Student student1 = new Student();

// Setting values for the object's attributes


student1.name = "John Doe";
student1.email = "[email protected]";
student1.phoneNumber = "1234567890";
student1.marks = new int[]{85, 90, 78, 88, 92}; // marks in 5
subjects

// Printing the student's details


System.out.println("Name: " + student1.name);
System.out.println("Email: " + student1.email);
System.out.println("Phone: " + student1.phoneNumber);
System.out.print("Marks: ");
for (int mark : student1.marks) {
System.out.print(mark + " ");
}
}
}
Output:

Name: John Doe


Email: [email protected]
Phone: 1234567890
Marks: 85 90 78 88 92

Explanation:

● Here, student1 is an object of the Student class.


● We use the . operator to access and modify the attributes of the object.
● Notice that we did not create a constructor explicitly; the default constructor provided by
Java was used.

5. Using Parameterized Constructors


Often, we want to initialize an object with specific data at the time of creation. For this, we can
define a parameterized constructor.

Example: Adding a Parameterized Constructor

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:

public class Main {


public static void main(String[] args) {
// Creating an object using the parameterized constructor
int[] marksArray = {85, 90, 78, 88, 92};
Student student2 = new Student("Alice Smith",
"[email protected]", "9876543210", marksArray);

// Displaying the student's details


System.out.println("Name: " + student2.name);
System.out.println("Email: " + student2.email);
System.out.println("Phone: " + student2.phoneNumber);
System.out.print("Marks: ");
for (int mark : student2.marks) {
System.out.print(mark + " ");
}
}
}

Output:

Name: Alice Smith


Email: [email protected]
Phone: 9876543210
Marks: 85 90 78 88 92

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.

You might also like