CH 07
CH 07
Badour AlBahar
Kuwait University
Why?
• Makes programs easier to understand, correct, and modify.
• Allows code reuse, increasing productivity.
A class is a blueprint for An object is an instance of a class.
creating objects.
Object: Student1
Attributes:
Name: Saleh
Major : CpE
Class: Student Year: 2022
Attributes: Methods:
Name Study, AttendClass, takeExam
Major
Year
Object: Student2
Methods:
Study, AttendClass, takeExam Attributes:
Name: Zain
Major: CpE
Year: 2021
Methods:
Study, AttendClass, takeExam
A class is a blueprint for An object is an instance of a class.
creating objects.
Object: Account1
Attributes:
Number: 102961
Balance: 1,513
Class: Bank Account
Methods:
Attributes: Withdraw, Deposit
Number
Balance
Methods:
Withdraw, Deposit
Class: Student Class: Bank Account
Attributes: Attributes:
Name Number
Major Balance
Year
Methods:
Methods: Withdraw, Deposit
Study, AttendClass, takeExam
Classes:
• Classes are reusable software components representing real-world
entities.
• Classes have attributes (e.g., name, color, size).
• Classes have methods (e.g., calculating, moving, communicating)
that house program statements to perform certain tasks.
Object: Student1 Object: Account1
Attributes: Attributes:
Name: Saleh Object: Student2 Number: 102961
Major : CpE
Attributes:
Balance: 1,513 Object: Account2
Year: 2022
Name: Zain Methods: Attributes:
Methods: Major: CpE Withdraw, Deposit Number: 883923
Year: 2021
Study, AttendClass, takeExam Balance: 93,192
Methods: Methods:
Study, AttendClass, takeExam Withdraw, Deposit
Objects:
• An object is an instance of a class.
• You can reuse a class many times to build many objects.
• Objects have their own set of attributes.
• Objects can invoke actions derived from their class.
Imagine a class as a cookie cutter, and an object as the cookie
formed when you use that cutter.
Image source
Why Do We Need Classes?
Real-world entities are complex: Often, we need to represent more
complicated data types. For example, when keeping track of a student,
we need to store:
• Name
• ID
• GPA
• Number of credits
• Class year, etc.
Why Do We Need Classes?
A bad approach:
One way to do this is by using multiple arrays to hold this information.
But this approach:
• Is complicated
• Leads to messy, hard-to-read code
• Is difficult to maintain and update over time.
Why Do We Need Classes?
The better approach:
• Create a class to define a new type.
• A class is a blueprint that combines variables (like name, ID, and GPA) and
actions (like calculating a GPA or changing the number of credits) into a single,
easy-to-use unit.
Why Do We Need Classes?
Why classes make life easier:
• Readability: Grouping related information together in one class is much
easier to understand.
• Reusability: Once defined, a class can be reused as many times as needed to
create many students.
• Maintainability: Changing or updating the class design automatically affects
all the instances, making maintenance simpler.
Why Do We Need Classes?
• Each class you create becomes a new type that can be used to declare
variables and create objects.
• You can declare new classes as needed; this is one reason Java is
known as an extensible language.
Main Components of a Class
• Class Declaration:
public class Student {
// instance variables
private String name;
private String id;
private double gpa;
private int credits;
// instance variables
private String name;
private String id;
private double gpa;
private int credits;
• Since we are just retrieving data, getters usually don’t need any parameters.
Main Components of a Class
• Constructor:
• A constructor is a special method that is called when an object of the class is
created.
• Its purpose is to initialize the object, setting up any necessary data (usually
the instance variables).
• Every time an object is created, Java requires a call to a constructor, whether
it’s a default or custom constructor.
Main Components of a Class
• Custom Constructor:
public class Student {
// instance variables
private String name;
private String id;
private double gpa;
private int credits;
} //end of main
} //end of main
String Comparison:
// Update credits after adding the grade
•intDo
newCredits
not use= getCredits()
== to compare+ 3; // Assume
stringseach course is worth 3 credit
in Java.
setCredits(newCredits); // Set the new credit count
} else {
• The == operator compares the memory reference of two strings, not
their actual value.
System.out.println("Invalid This
grade. means
Please enteritA,checks whether
B, C, D, or F."); the strings are the
} same object.
• Use .equals() to compare the values of two strings.
} // end of method addGrade
• The .equals() method compares whether the value of two strings is the
same, not whether they are the same object.v
Method to get the list of grades
public ArrayList<String> getGrades(){
return grades;
}
Test Class (Driver Class): StudentTest
public class StudentTest{
student1.addGrade("A");
student1.addGrade("A");
student1.addGrade("C");
System.out.println("Grades are " + student1.getGrades());
} //end of main