2. Accessors & Mutators
■ If we want a class/program external to our class to be able to access the value of our
(always!) private member variables, we provide accessors.
■ If we want a class/program external to our class to be able to change the value of our
(always!) private member variables, we provide mutators.
■ Accessors and mutators are both class methods.
■ An accessor (by convention) starts with get. A mutator (by convention) starts with set.
■ When we write a class, it is up to us to determine whether we provide access or allow
mutations.
■ JAVA IDE provides functionality to generate accessors & mutators automatically.
– Right click and select generate, then select getter and setter.
3. toString() Method
■ in Java, it is customary to create a string
representation of an object in a class.
■ Let us create a string like the following
example:
– Name: John Jones
– Age : 21
– Major Code:175
– Rank: Soph
– Traditional Age
4. Test Class for
Method
■ In line 4, we use the new operator to create an instance of
(object in) the class Student.
■ The object's name is aStudent. It uses the constructor that
accepts age, major code, name and rank.
■ Line 5 prints the toString value for aStudent.
■ Lines 6 and 7 use mutators to change values of two member
variables
■ Line 8 prints the toString value for the updated object.
5. What we did…
■ We created a class called Student.
■ We gave Student
– constructor(s)
– Accessors
– Mutators
– toString() method
6. Output of Test Program
Note the output of toString for the
initial values of the object aStudent.
Note the output after we used
mutators to change age and name.
7. The equals() Method
■ We almost never use ==, !=, <=, >= when we compare Objects from any class.
■ We use the equals method which is also a method of Object.
■ In the class Object, the equals method returns
true if the two Objects are located at the same memory location
false otherwise
■ Fortunately, we can override the equals method and give our own definition of equality
for objects in our class.
8. Equals
Method for
Person
■ Suppose we decide that two instances of Person are equal if
they have the same name and same age. Note that we
could have made a different definition of equality.
■ Below is the equals method for this definition. Note the
signature!!
■ Other is the “other object” we are checking for equality
with this.
10. Better equals() Method
■ The equals method we wrote for the abstract class Shape is dangerous.
■ Why? The signature indicates that we will always be comparing one Shape to another
for equality.
■ What if our user (inadvertently or unknowingly) used our equals method to compare a
Shape to a Double or a Person or a Student or ...
– Our program may Boom!
11. ■ The signature for equals should become:
– public boolean equals(Object object)
– Since everything is an Object, this is safe.
■ We use the instanceof operator to determine if object is an object in the Shape class.
■ If so,
– Create a Shape object by typecasting object as a Shape.
– Compare the just created Shape to this.
■ If not,
– return false.
Better equals() Method
13. Code Explanation
■ Line 18 is the amended prototype for the class Shape.
■ Line 20 uses instanceof to determine if the Object is a Shape.
■ If so,
– Line 22 typecasts object as a Shape.
– Line 23 calculates a boolean value to return.
■ Line 25 returns false in the case that object is not a Shape.
14. JAVA IDE Features
■ We learned about accessor & mutators, how to create toString(), and equals()
methods.
■ The JAVA IDEs provide many features that will help you to write code easily & safely.
■ Research about your JAVA IDE and use it properly.
■ JAVA IDEs provide the functionality to generate accessor & mutators, toString(), and
equals() method.
■ Just right click on code writing pane and select Generate, then you will see all the
functions that provided by your JAVA IDE.