SlideShare a Scribd company logo
Overloading methods
Steps to override equals()
● Do this check -- if yes then return true.
● Do null check -- if yes then return false.
● Do the instanceof check, if instanceof return false than return false from equals
in Java ,we can use getClass() method for type identification because instanceof
check returns true for subclass also.
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
● Type cast the object; note the sequence instanceof check must be prior to
casting object.
● Compare individual attribute starting with numeric attribute because comparing
numeric attribute is fast and use short circuit operator for combining checks.
Sample code
Cont'd
Overriding hashCode()
● Both equal() and hashCode() belong to Object
class.
● It is not always necessary to override hashcode
and equals.
● But if you think you need to override one, then
you need to override both of them.
● We must overload hashCode() when equals() is
overrided because to access the collection class
effectively without violating the rules.
Example code for overriding
hashCode()
Cont'd
output
Overridding toString()
Reason to override toString()
● It is useful in printing the object in the format
what we need.
Example code to override toString()
Sample output

More Related Content

PDF
Insertion sort
PPTX
Insertion sort
PPTX
Insertion sort
PPT
Odersky week1 notes
PPTX
Attacking GraphQL
PPTX
GraphQL Misconfiguration
PDF
ODP
operators in c++
Insertion sort
Insertion sort
Insertion sort
Odersky week1 notes
Attacking GraphQL
GraphQL Misconfiguration
operators in c++

What's hot (20)

PPTX
Code craftsmanship saturdays second session
DOC
PDF
Reactive programming
PPTX
Introduction to RxJava on Android
PPTX
Stack application
PPTX
Object to input stream transformer
PPSX
CS106 Lab 6 - While and Do..While loop
PDF
TDD - Test Driven Development in Swift (iOS) with Cuckoo, Quick and Nimble
PPTX
Qtp training session IV
PPTX
Quick and radix sort
PPTX
Advanced guide to Quartz plugin
PDF
Investigation of the Characteristics of the Zeros of the Riemann Zeta Functio...
PPTX
Lecture 1 sorting insertion & shell sort
PDF
Static analysis should be used regularly
PDF
RxJava@Android
PPTX
Evaluation of prefix expression with example
ODP
Functional programming in Javascript
PPTX
Quicksort Algorithm..simply defined through animations..!!
PDF
Bubble Sort algorithm in Assembly Language
ODP
Quick sort
Code craftsmanship saturdays second session
Reactive programming
Introduction to RxJava on Android
Stack application
Object to input stream transformer
CS106 Lab 6 - While and Do..While loop
TDD - Test Driven Development in Swift (iOS) with Cuckoo, Quick and Nimble
Qtp training session IV
Quick and radix sort
Advanced guide to Quartz plugin
Investigation of the Characteristics of the Zeros of the Riemann Zeta Functio...
Lecture 1 sorting insertion & shell sort
Static analysis should be used regularly
RxJava@Android
Evaluation of prefix expression with example
Functional programming in Javascript
Quicksort Algorithm..simply defined through animations..!!
Bubble Sort algorithm in Assembly Language
Quick sort
Ad

Viewers also liked (13)

PPT
‘ Final ‘ to avoid overriding
PPT
Algorithms with-java-advanced-1.0
PPTX
Super keyword in java
PPT
Ch13 Binary Search Tree
PDF
Java keywords
PPTX
Binary search
PPS
Interface
PPT
Linear Search & Binary Search
PPT
Chapter 11 - Sorting and Searching
DOCX
Method overriding in java
PPTX
encapsulation, inheritance, overriding, overloading
PPT
Object Oriented Programming Concepts
PPT
Chapter 9 Abstract Class
‘ Final ‘ to avoid overriding
Algorithms with-java-advanced-1.0
Super keyword in java
Ch13 Binary Search Tree
Java keywords
Binary search
Interface
Linear Search & Binary Search
Chapter 11 - Sorting and Searching
Method overriding in java
encapsulation, inheritance, overriding, overloading
Object Oriented Programming Concepts
Chapter 9 Abstract Class
Ad

Similar to Overriding methods (20)

PPTX
Java tutorial part 4
PDF
CHAPTER 3 part2.pdf
PPT
An introduction to javascript
PPT
JAVA Tutorial- Do's and Don'ts of Java programming
PPT
JAVA Tutorial- Do's and Don'ts of Java programming
ODP
Java for newcomers
DOCX
Files to submitProperQueue.javaCreate this file and implement .docx
PPTX
Exploring Kotlin language basics for Android App development
ODT
Java%20 new%20faq.doc 0
PPTX
Methods common to all objects
PPTX
OCA Java SE 8 Exam Chapter 3 Core Java APIs
DOCX
JAVA CONCEPTS AND PRACTICES
DOCX
Jist of Java
PPTX
Java Tutorial Lab 3
PDF
Javantura v2 - Making Java web-apps Groovy - Franjo Žilić
PPTX
JavaScript Coding Guidelines
PPTX
JavaScript / Web Engineering / Web Development / html + css + js/presentation
PPTX
Easy mock
DOCX
ObjectivesMore practice with recursion.Practice writing some tem.docx
PPTX
Salesforce DUG - Queueable Apex
Java tutorial part 4
CHAPTER 3 part2.pdf
An introduction to javascript
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
Java for newcomers
Files to submitProperQueue.javaCreate this file and implement .docx
Exploring Kotlin language basics for Android App development
Java%20 new%20faq.doc 0
Methods common to all objects
OCA Java SE 8 Exam Chapter 3 Core Java APIs
JAVA CONCEPTS AND PRACTICES
Jist of Java
Java Tutorial Lab 3
Javantura v2 - Making Java web-apps Groovy - Franjo Žilić
JavaScript Coding Guidelines
JavaScript / Web Engineering / Web Development / html + css + js/presentation
Easy mock
ObjectivesMore practice with recursion.Practice writing some tem.docx
Salesforce DUG - Queueable Apex

More from Muthukumaran Subramanian (20)

PPTX
Struts introduction
PDF
Operators in java
PDF
Threads concept in java
PDF
StringTokenizer in java
PDF
Oops (inheritance&interface)
PDF
Non access modifiers
PDF
Jdbc connectivity in java
PDF
Garbage collection in java
PDF
PDF
Exception handling
PDF
Currency class
PDF
Timer class in java
PDF
Calendar class in java
PDF
Classes and objects in java
PDF
intorduction to Arrays in java
PDF
Access modifiers in java
PDF
Abstract classes
PDF
Java introduction
PDF
Collections and generics
Struts introduction
Operators in java
Threads concept in java
StringTokenizer in java
Oops (inheritance&interface)
Non access modifiers
Jdbc connectivity in java
Garbage collection in java
Exception handling
Currency class
Timer class in java
Calendar class in java
Classes and objects in java
intorduction to Arrays in java
Access modifiers in java
Abstract classes
Java introduction
Collections and generics

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.

Overriding methods

  • 2. Steps to override equals() ● Do this check -- if yes then return true. ● Do null check -- if yes then return false. ● Do the instanceof check, if instanceof return false than return false from equals in Java ,we can use getClass() method for type identification because instanceof check returns true for subclass also. if((obj == null) || (obj.getClass() != this.getClass())) return false; ● Type cast the object; note the sequence instanceof check must be prior to casting object. ● Compare individual attribute starting with numeric attribute because comparing numeric attribute is fast and use short circuit operator for combining checks.
  • 5. Overriding hashCode() ● Both equal() and hashCode() belong to Object class. ● It is not always necessary to override hashcode and equals. ● But if you think you need to override one, then you need to override both of them. ● We must overload hashCode() when equals() is overrided because to access the collection class effectively without violating the rules.
  • 6. Example code for overriding hashCode()
  • 9. Overridding toString() Reason to override toString() ● It is useful in printing the object in the format what we need.
  • 10. Example code to override toString()