SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (17)

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++ Programming
Preeti Kashyap
 
12pointerin c#
12pointerin c#12pointerin c#
12pointerin c#
Sireesh K
 
Introduction to OOP in Python
Introduction to OOP in PythonIntroduction to OOP in Python
Introduction to OOP in Python
Aleksander Fabijan
 
Data types IN JAVA
Data types IN JAVAData types IN JAVA
Data types IN JAVA
garishma bhatia
 
Rational functions day 1.pptx worked
Rational functions day 1.pptx workedRational functions day 1.pptx worked
Rational functions day 1.pptx worked
Jonna Ramsey
 
Chapter 2 java
Chapter 2 javaChapter 2 java
Chapter 2 java
Ahmad sohail Kakar
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
CPP15 - Inheritance
CPP15 - InheritanceCPP15 - Inheritance
CPP15 - Inheritance
Michael Heron
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
Sumit Srivastava
 
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 #2
Diego 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 algorithms
Nico 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 singh
Harmeet Singh(Taara)
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
Nico Ludwig
 
Generics of JAVA
Generics of JAVAGenerics of JAVA
Generics of JAVA
Jai Marathe
 
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++ Programming
Preeti 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 worked
Jonna Ramsey
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud 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 #2
Diego 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 algorithms
Nico 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 singh
Harmeet Singh(Taara)
 
(4) collections algorithms
(4) collections algorithms(4) collections algorithms
(4) collections algorithms
Nico Ludwig
 
Generics of JAVA
Generics of JAVAGenerics of JAVA
Generics of JAVA
Jai Marathe
 

Similar to Some Important Methods in JAVA (20)

Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
Berk Soysal
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptxModule 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
CHAPTER 3 part2.pdf
CHAPTER 3 part2.pdfCHAPTER 3 part2.pdf
CHAPTER 3 part2.pdf
FacultyAnupamaAlagan
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
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
anandanand521251
 
Overview of Java
Overview of Java Overview of Java
Overview of Java
AnandMHadoop
 
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.pptx
Epsiba1
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
Vince Vo
 
Oop
OopOop
Oop
dilshod1988
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Object Oriented Programming in Python.pptx
Object Oriented Programming in Python.pptxObject Oriented Programming in Python.pptx
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
Chapter- 2 Introduction to Class and Object.pdf
Chapter- 2 Introduction to Class and Object.pdfChapter- 2 Introduction to Class and Object.pdf
Chapter- 2 Introduction to Class and Object.pdf
joshua211619
 
Constructors
ConstructorsConstructors
Constructors
shravani2191
 
Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
Berk Soysal
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptxModule 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
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
anandanand521251
 
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.pptx
Epsiba1
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
Vince Vo
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Object Oriented Programming in Python.pptx
Object Oriented Programming in Python.pptxObject Oriented Programming in Python.pptx
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
Chapter- 2 Introduction to Class and Object.pdf
Chapter- 2 Introduction to Class and Object.pdfChapter- 2 Introduction to Class and Object.pdf
Chapter- 2 Introduction to Class and Object.pdf
joshua211619
 
Ad

More from SAGARDAVE29 (12)

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 JAVA
SAGARDAVE29
 
Exception Handling
Exception HandlingException Handling
Exception Handling
SAGARDAVE29
 
JAVA Multithreading
JAVA MultithreadingJAVA Multithreading
JAVA Multithreading
SAGARDAVE29
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
SAGARDAVE29
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
SAGARDAVE29
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
SAGARDAVE29
 
Exception handling
Exception handlingException handling
Exception handling
SAGARDAVE29
 
More oop in java
More oop in javaMore oop in java
More oop in java
SAGARDAVE29
 
Coding Style & Tips for JAVA
Coding Style & Tips for JAVACoding Style & Tips for JAVA
Coding Style & Tips for JAVA
SAGARDAVE29
 
Recursion
RecursionRecursion
Recursion
SAGARDAVE29
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
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 JAVA
SAGARDAVE29
 
Exception Handling
Exception HandlingException Handling
Exception Handling
SAGARDAVE29
 
JAVA Multithreading
JAVA MultithreadingJAVA Multithreading
JAVA Multithreading
SAGARDAVE29
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
SAGARDAVE29
 
Exception handling
Exception handlingException handling
Exception handling
SAGARDAVE29
 
More oop in java
More oop in javaMore oop in java
More oop in java
SAGARDAVE29
 
Coding Style & Tips for JAVA
Coding Style & Tips for JAVACoding Style & Tips for JAVA
Coding Style & Tips for JAVA
SAGARDAVE29
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
SAGARDAVE29
 
Ad

Recently uploaded (20)

Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
How to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 InventoryHow to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 Inventory
Celine George
 
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDM & Mia eStudios
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
INDIA QUIZ FOR SCHOOLS | THE QUIZ CLUB OF PSGCAS | AUGUST 2024
Quiz Club of PSG College of Arts & Science
 
libbys peer assesment.docx..............
libbys peer assesment.docx..............libbys peer assesment.docx..............
libbys peer assesment.docx..............
19lburrell
 
PUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for HealthPUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for Health
JonathanHallett4
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
How to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 InventoryHow to Manage Manual Reordering Rule in Odoo 18 Inventory
How to Manage Manual Reordering Rule in Odoo 18 Inventory
Celine George
 
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDMMIA Reiki Yoga S6 Free Workshop Money Pt 2
LDM & Mia eStudios
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)INSULIN.pptx by Arka Das (Bsc. Critical care technology)
INSULIN.pptx by Arka Das (Bsc. Critical care technology)
ArkaDas54
 
libbys peer assesment.docx..............
libbys peer assesment.docx..............libbys peer assesment.docx..............
libbys peer assesment.docx..............
19lburrell
 
PUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for HealthPUBH1000 Slides - Module 11: Governance for Health
PUBH1000 Slides - Module 11: Governance for Health
JonathanHallett4
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 

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.
  翻译: