Chapter 2 OOP in Java (Part A) - Data Hiding, Objects in Arrays
Chapter 2 OOP in Java (Part A) - Data Hiding, Objects in Arrays
OM1102
Programming and
Data Structures
Chapter 2
Page 1 of 60
Page 2 of 60
Quick Revision: What is an Object
An object:
Can contain possibly multiple value(s) (of various types)
Can contain some methods, which can do something for you. E.g.,
Do some calculations and tell you the result.
Tell you the values of its variables.
Update the values of its variables.
Print something to the screen.
In programming, we can store some complex data using Objects, not
just using variables.
Page 3 of 60
Quick Revision: Object-oriented Programming
Thus, Student is a structured data type (with many smaller Student John
SID = S190001
components) year = 3
major = “Eng”
grade = ‘A’
Page 4 of 60
Defining The Student Class of Objects (informally)
We start with giving the new type of object a Class name, then we define its
attributes
Class name: Student
Attributes: We have a class of objects,
- SID: String called “Student”, and every
Student object will have a
- Year: int SID, a Year, a Major and a
- Major: String Grade
- Grade: char
ID = S190001
major = Eng
Defining the Student Class of objects
Page 5 of 60
Page 6 of 60
Creating New Student Objects
After defining what a Student Class looks like, we can use it to create
Student Objects.
You can imagine the Student Class is like an object factory that can create Student Objects.
Student Mary
The Student Object Factory! SID =?
year =?
major =?
grade =?
Page 7 of 60
Assigning values to attributes in student Objects
After creating Student objects, we can set the values of each object’s attributes.
Student John
SID = S230001 John the Student Object, your SID is “S230001”,
year = 3 your year is 3, your major is “Eng”, your grade is ‘A’
major = “Eng”
grade = ‘A’
Student Mary
SID = S230002
year = 1
major = “Chi”
grade = ‘B’
Page 8 of 60
The Student Class in Java
Class name: Student Defining the Student Class (File name: Student.java)
Attributes:
- SID: String
- Year: int
- Major: String
- Grade: char
ID = S190001
Notes:
Each file defines one class. (to be more accurate: one public class)
This class defines what a Student object looks like and what attributes it contains.
(Attributes are also called instance variables.)
An instance variable can be of a primitive data type (e.g., int) or of a class (e.g.,
String)
The keyword “public” indicates they can be accessed (seen) by other class’s objects.
Page 9 of 60
Page 10 of 60
The Student Class and Student Objects in Java
1. Defining the Student Class
Class name: Student
Attributes:
- SID: String
- Year: int
- Major: String
- Grade: char 2. Create a new Student object, and assign it to a variable
(You may create as many objects as you like)
ID = S190001
major = Eng
grade = ‘A’
3. Assign values to the attributes in the object
Page 11 of 60
A program that uses a Student object
Student.java
Note:
As you may have realized already, the scanner is also an object.
Page 12 of 60
Attributes initialization (New!)
Exercise 2.1
Page 13 of 60
Attribute Type Value
SID, major String
year int
grade char
Exercise 2.1:
Copy and paste your answer here:
Page 14 of 60
Attributes (instance variables) initialization
In the previous example, the variables (SID and major) are al-
ready defined, but their values are null, which means that the ob-
jects do not yet exist.
Page 15 of 60
Methods
Page 16 of 60
Quick Revision: Methods
For example, let’s add a method in our Student class for calculat-
ing the student’s GPA:
Class name: Student
Attributes:
- SID: String
- Year: int
- Major: String
- Grade: char Student John
SID = S190001
Methods: year = 3
- getGPA( ): return a double major = “Eng”
grade = ‘A’
Page 17 of 60
getGPA() method
This method will compute and return a GPA score based on
the grade attribute of the student object.
Notes:
Page 18 of 60
Calling the getGPA() method
For example:
Student John
SID = S190001
year = 3 “Get-GPA, please!”
major = “Eng”
grade = ‘A’
Page 19 of 60
Null Pointer Exceptions
Before you call an object’s method, make sure the object already exists
(not NULL)!
Page 20 of 60
Attributes vs Local Variables
Page 21 of 60
Recall: Attributes
Also called instance variables.
Variables that belong to an object.
Declared OUTSIDE any methods
Created when an object is created (“new”)
Can be accessed in any method of the object (or even other
objects if it is public)
It is printed out: 1
Page 22 of 60
Local variables
Attribute a is still 1
What is the output?
Output:
Page 24 of 60
Local variables and Objects
In this example, we have an attribute and a local variable of the
same name, but they refer to two separate objects.
Output:
Page 25 of 60
Local variables and Objects
In this example, we have an attribute and a local variable of the
same name, but they refer to two separate objects.
Output:
Page 26 of 60
Next, if the local variable in line 6 of the previous example is re-
moved, line 7 would refer to the Attribute s instead.
Output:
Page 27 of 60
Similarly, TWO local variables with the same name.
Output:
Page 28 of 60
This time, the VALUE of a local variable (which refers to a Student
object) is returned.
Page 29 of 60
Output:
Page 30 of 60
public modifier
Student.java
Recall: public means that the attribute (or method) can be ac-
cessed in codes written in another class. For example, we can
read or even modify those values in a Student object from the
StudentMain class.
Page 31 of 60
private modifiers
Student.java
Note: in Java, apart from public and private, there are two more types of modi-
fiers, namely protected, and friendly (without a modifier), which we will not
use in this module.
Page 32 of 60
MyDate Class
• Defining a new class called MyDate:
public class MyDate {
/* SKIPPED */ //Method
currentDate.month = 7;
currentDate.year = 2020;
currentDate.nextDay();
Page 33 of 60
Data Hiding
• Suppose you have written the following MyDate class:
1 public class MyDate {
6 }
7 }
2 d.day = 31;
3 d.month = 4;
4 d.year = 2020;
Page 34 of 60
Data Hiding
• A better approach:
return day;
}
Page 35 of 60
Data Hiding
A better approach:
return day;
}
}
public class DateUser {
mydate.setDay(21); // ok!
System.out.println(Mydate.getDay());
}
Page 36 of 60
public vs private methods
public
The variable, or method is visible to objects any class, anywhere.
private
The variable or method can be accessed only by objects in the same class as current
object.
Note: if neither the private or public modifier is declared before a variable or method (i.e., no modifier is provided), the
default access right would be applied. In this case, the method can be accessed by any class in the same package. This
has the same effect as using the “friendly” modifier, which is not further discussed in this module).
Data Hiding
Page 37 of 60
• Make your instance variables private, and provide getter and setter methods
for accessing the data where necessary. (To provide a separation between
the interface and implementation)
• Advantages:
o The object can control how its attributes can be changed!
o The object can decide what can be seen by other objects!
o Other people do not need to know how data is represented internally. They only need
to know how to call the (public) methods!
o Prevent unintentional (or illegal) changes to the data (instance variables)
Using data hiding does not make your code run faster, but it helps to make your code easier to
maintain.
Page 38 of 60
Introduction to Object-oriented Programming:
Constructor methods
Overloading
Page 39 of 60
Constructor methods
Page 40 of 60
Example: A Circle Class
Let’s first define a class of Circle objects
(Circle.java)
Page 42 of 60
Constructor Overloading
It is possible to provide more than one constructor. For example:
Circle.java
Page 43 of 60
Method Overloading
Just like constructors, any methods in a class can be overloaded too by providing more than one version.
For example:
Here, the method named displayMessage( ) is overloaded. Which version would be called depends
Page 44 of 60
Calling overloaded methods
(A)
(B)
Given the same class definition as above, and the following calling
method:
My name is Peter
I am 18 years old.
Page 45 of 60
Introduction to Object-oriented
Programming:
Page 46 of 60
The this keyword
Sometimes, a local variable in a method has the same
An instance variable
(attribute)
A local variable
Here, the radius in line 4 and the radius in line 6 are two differ-
ent variables.
The second one (line 6) is a local variable and an argument, that ex-
ists in the method only.
Page 47 of 60
The this keyword
Under such situation, the local variable will be referred to.
If we need to refer to the instance variable, we can use the this keyword, for example:
In this case, this.radius refers to the attribute in line 4, whereas the un-qualified version (the second ra-
dius line 8) refers to the local variable (the argument) in line 6.
The this keyword is not needed (optional) in other situations where there are no such confusions.
Page 50 of 60
Saving BankAccount Objects in an Array
Use a For Loop to Print out a report of all accounts with bal-
ance >= 10000.
Page 51 of 60
Exercise 2.2 (continue):
accounts
Page 52 of 60
Exercise 2.4 (continue):
accounts
Page 53 of 60
Create 3 accounts (with names, account number and balance)
and put them into the first 3 positions of the array.
accounts
“Peter”
“00001”
5000
ac1
Page 54 of 60
Create 3 accounts (with names, account number and balance)
and put them into the first 3 positions of the array.
accounts
“Peter”
“00001”
5000
ac1
Page 55 of 60
Use a For Loop to Print out a report of all accounts
Remember to check for null as some slots are empty!
accounts
Page 56 of 60
a
Use a For Loop to Print out all account details with balance >= 10000
Page 57 of 60
Completed Program Code
Page 58 of 60
Alternative version using Enhanced For Loop
Page 59 of 60
Page 60 of 60