SlideShare a Scribd company logo
1 of 15
JAVA Useful Methods
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.
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
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.
What we did…
■ We created a class called Student.
■ We gave Student
– constructor(s)
– Accessors
– Mutators
– toString() method
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.
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.
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.
Testing equals() Method
Output: Equal
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!
■ 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
Old way
New way
Example of equals() Method
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.
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.
Thank You

More Related Content

What's hot

OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.Questpond
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ ProgrammingPreeti Kashyap
 
12pointerin c#
12pointerin c#12pointerin c#
12pointerin c#Sireesh K
 
Rational functions day 1.pptx worked
Rational functions day 1.pptx workedRational functions day 1.pptx worked
Rational functions day 1.pptx workedJonna Ramsey
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011Mahmoud Alfarra
 
15reflection in c#
15reflection  in c#15reflection  in c#
15reflection in c#Sireesh K
 
Doppl development iteration #2
Doppl development   iteration #2Doppl development   iteration #2
Doppl development iteration #2Diego Perini
 
functions over-loading, over-hiding and over-riding
functions over-loading, over-hiding and over-riding functions over-loading, over-hiding and over-riding
functions over-loading, over-hiding and over-riding zaidurrehman5
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithmsNico Ludwig
 
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singhJava generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singhHarmeet Singh(Taara)
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithmsNico Ludwig
 
Generics of JAVA
Generics of JAVAGenerics of JAVA
Generics of JAVAJai Marathe
 

What's hot (17)

OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
 
12pointerin c#
12pointerin c#12pointerin c#
12pointerin c#
 
Introduction to OOP in Python
Introduction to OOP in PythonIntroduction to OOP in Python
Introduction to OOP in Python
 
Data types IN JAVA
Data types IN JAVAData types IN JAVA
Data types IN JAVA
 
Rational functions day 1.pptx worked
Rational functions day 1.pptx workedRational functions day 1.pptx worked
Rational functions day 1.pptx worked
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
 
CPP15 - Inheritance
CPP15 - InheritanceCPP15 - Inheritance
CPP15 - Inheritance
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
15reflection in c#
15reflection  in c#15reflection  in c#
15reflection in c#
 
Doppl development iteration #2
Doppl development   iteration #2Doppl development   iteration #2
Doppl development iteration #2
 
functions over-loading, over-hiding and over-riding
functions over-loading, over-hiding and over-riding functions over-loading, over-hiding and over-riding
functions over-loading, over-hiding and over-riding
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
 
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singhJava generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singh
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
 
Generics of JAVA
Generics of JAVAGenerics of JAVA
Generics of JAVA
 

Similar to Some Important Methods in JAVA

Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3Berk Soysal
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3Mahmoud Alfarra
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
What is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfWhat is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfanandanand521251
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13Vince Vo
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingshinyduela
 
Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Languageale8819
 

Similar to Some Important Methods in JAVA (20)

Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 
CHAPTER 3 part2.pdf
CHAPTER 3 part2.pdfCHAPTER 3 part2.pdf
CHAPTER 3 part2.pdf
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
What is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdfWhat is a constructorAns)A constructor is also type of method whi.pdf
What is a constructorAns)A constructor is also type of method whi.pdf
 
Overview of Java
Overview of Java Overview of Java
Overview of Java
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
Oop
OopOop
Oop
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Learning core java
Learning core javaLearning core java
Learning core java
 
Constructors
ConstructorsConstructors
Constructors
 
Oop scala
Oop scalaOop scala
Oop scala
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Java is an Object-Oriented Language
Java is an Object-Oriented LanguageJava is an Object-Oriented Language
Java is an Object-Oriented Language
 

More from SAGARDAVE29

Graphical User Interface (GUI)
Graphical User Interface (GUI)Graphical User Interface (GUI)
Graphical User Interface (GUI)SAGARDAVE29
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVASAGARDAVE29
 
Exception Handling
Exception HandlingException Handling
Exception HandlingSAGARDAVE29
 
JAVA Multithreading
JAVA MultithreadingJAVA Multithreading
JAVA MultithreadingSAGARDAVE29
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVASAGARDAVE29
 
Exception handling
Exception handlingException handling
Exception handlingSAGARDAVE29
 
More oop in java
More oop in javaMore oop in java
More oop in javaSAGARDAVE29
 
Coding Style & Tips for JAVA
Coding Style & Tips for JAVACoding Style & Tips for JAVA
Coding Style & Tips for JAVASAGARDAVE29
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & PolymorphismSAGARDAVE29
 

More from SAGARDAVE29 (12)

Graphical User Interface (GUI)
Graphical User Interface (GUI)Graphical User Interface (GUI)
Graphical User Interface (GUI)
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
JAVA Multithreading
JAVA MultithreadingJAVA Multithreading
JAVA Multithreading
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Exception handling
Exception handlingException handling
Exception handling
 
More oop in java
More oop in javaMore oop in java
More oop in java
 
Coding Style & Tips for JAVA
Coding Style & Tips for JAVACoding Style & Tips for JAVA
Coding Style & Tips for JAVA
 
Recursion
RecursionRecursion
Recursion
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
 

Recently uploaded

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 

Recently uploaded (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Some Important Methods in JAVA

  • 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
  • 12. Old way New way Example of 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.