SlideShare a Scribd company logo
Core Java Training
Object Class
Page 1Classification: Restricted
Quick recap: Topics covered till now
• Data Types – Primitive (byte, short, int, long, double, float, char, boolean)
• Data Types – Wrapper (Byte, Short, Integer …)
• Data Types – Classes for String (String - immutable, StringBuilder – mutable,
StringBuffer – mutable+threadsafe); Concept of String pooling / interning
• Arrays
• Conditional statements, Loops
• OOP with Java –
• Polymorphism – Method overloading (Compile-time), Method Overriding
(Runtime)
• Inheritance – extending classes, implementing interfaces; multiple inheritance
• Abstraction – abstract classes vs interfaces
• Encapsulation – POJO classes / Beans
• Packaging, Class Modifiers (public, protected, default, private)
• Static members vs Instance members
• Exception handling
Page 2Classification: Restricted
Agenda
• Object Class
• toString()
• equals()
• Hashing
• hashCode()
Object Class
as
“The basic unit of any collection”
Page 4Classification: Restricted
Object class
• Superclass for all Java classes
• Any class without explicit extends clause is a direct subclass of Object
• Methods of Object include:
• String toString()
• boolean equals (Object other)
• int hashCode()
Page 5Classification: Restricted
5
Method toString()
• Returns String representation of object; describes state of object
• Automatically called when:
• Object is concatenated with a String
• Object is printed using print() or println()
• Object reference is passed to assert statement of the form:
assert condition : object
Page 6Classification: Restricted
6
Example
Rectangle r = new Rectangle (0,0,20,40);
System.out.println(r);
Prints out:
java.awt.Rectangle[x=0,y=0,width=20,height=40]
Page 7Classification: Restricted
7
More on toString()
• Default toString() method just prints (full) class name & hash code of object
• Not all API classes override toString()
• Good idea to implement for debugging purposes:
• Should return String containing values of important fields along with
their names
• Should also return result of getClass().getName() rather than hard-
coded class name
Page 8Classification: Restricted
Overriding toString(): example
public class Employee
{
public String toString()
{
return getClass().getName()
+ "[name=" + name
+ ",salary=" + salary
+ "]";
}
...
}
Typical String produced: Employee[name=John Doe,salary=40000]
Page 9Classification: Restricted
9
Overriding toString in a subclass
• Format superclass first
• Add unique subclass details
• Example:
public class Manager extends Employee
{
public String toString()
{
return super.toString()
+ "[department=" + department + "]";
}
...
}
Page 10Classification: Restricted
10
Example continued
• Typical String produced:
Manager[name=Mary Lamb,salary=75000][department=Admin]
• Note that superclass reports actual class name
Page 11Classification: Restricted
11
Equality testing
• Method equals() tests whether two objects have same contents
• By contrast, == operator test 2 references to see if they point to the same
object (or test primitive values for equality)
• Need to define for each class what “equality” means:
Compare all fields
Compare 1 or 2 key fields
Page 12Classification: Restricted
12
Equality testing
• Object.equals tests for identity:
public class Object {
public boolean equals(Object obj) {
return this == obj;
}
...
}
• Override equals if you don't want to inherit that behavior
Page 13Classification: Restricted
13
Overriding equals()
• Good practice to override, since many API methods assume objects have
well-defined notion of equality
• When overriding equals() in a subclass, can call superclass version by using
super.equals()
Page 14Classification: Restricted
14
Requirements for equals() method
• If x is not null, then x.equals(null) must be false
Page 15Classification: Restricted
The perfect equals() method
public boolean equals(Object otherObject)
{
if (this == otherObject) return true;
if (otherObject == null) return false;
if (getClass() != otherObject.getClass()) return false;
...
}
Page 16Classification: Restricted
16
Hashing
• Technique used to find elements in a data structure quickly, without doing
full linear search
• Important concepts:
• Hash code: integer value used to find array index for data
storage/retrieval
• Hash table: array of elements arranged according to hash code
• Hash function: computes hash code for element; uses algorithm likely to
produce different hash codes for different objects to minimize collisions
Page 17Classification: Restricted
17
Hashing in Java
• Java library contains HashSet and HashMap classes
• Use hash tables for data storage
• Since Object has a hashCode method (hash function), any type of object
can be stored in a hash table
Page 18Classification: Restricted
18
Default hashCode()
• Hashes memory address of object; consistent with default equals() method
• If you override equals(), you should also redefine hashCode()
• For class you are defining, use product of hash of each field and a prime
number, then add these together – result is hash code
Page 19Classification: Restricted
19
Example
public class Employee
{
public int hashCode()
{
return 11 * name.hashCode()
+ 13 * new Double(salary).hashCode();
}
...
}
Page 20Classification: Restricted
20
Topics to be covered in next session
• Recap of Arrays
• Introduction to Collections API
• Lists – ArrayList, Vector, LinkedList
Page 21Classification: Restricted
21
Thank you!

More Related Content

PPTX
Session 14 - Object Class
PPTX
Session 15 - Collections - Array List
PPSX
Collections - Sorting, Comparing Basics
PPSX
Arrays in Java
PPTX
Session 17 - Collections - Lists, Sets
PPTX
Session 05 - Strings in Java
PPTX
Session 20 - Collections - Maps
PPSX
Collections - Array List
Session 14 - Object Class
Session 15 - Collections - Array List
Collections - Sorting, Comparing Basics
Arrays in Java
Session 17 - Collections - Lists, Sets
Session 05 - Strings in Java
Session 20 - Collections - Maps
Collections - Array List

What's hot (19)

PPSX
Collections - Maps
PPSX
OOP with Java - Continued
PPSX
Collections - Lists, Sets
PPTX
Java Collection
PPTX
Java 103 intro to java data structures
PDF
Lecture 24
PDF
PDF
Scala Collections : Java 8 on Steroids
PDF
Java Collection framework
PDF
Collections In Java
PPTX
Collections - Lists & sets
PDF
Collections Java e Google Collections
PPTX
Java and SPARQL
PPTX
Java - Collections framework
PDF
Java Collections Tutorials
PPTX
Collections Array list
PDF
Collections in Java Notes
Collections - Maps
OOP with Java - Continued
Collections - Lists, Sets
Java Collection
Java 103 intro to java data structures
Lecture 24
Scala Collections : Java 8 on Steroids
Java Collection framework
Collections In Java
Collections - Lists & sets
Collections Java e Google Collections
Java and SPARQL
Java - Collections framework
Java Collections Tutorials
Collections Array list
Collections in Java Notes
Ad

Similar to Object Class (20)

PPTX
Object Class
PPT
Java Tutorials
PPT
java training faridabad
PPTX
Lecture-10_PHP-OOP.pptx
PPT
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
PPT
Introduction to Java(basic understanding).ppt
PPTX
Class and Object.pptx from nit patna ece department
PPTX
OOP_with_Java_Beginner explanation .pptx
PPTX
classes and objects in C++
PDF
Java OOP Programming language (Part 4) - Collection
ODP
Synapseindia reviews.odp.
PPT
Core java by a introduction sandesh sharma
PPTX
PPTX
Introduction to java
PPTX
Taxonomy of Scala
PPTX
PDF
4java Basic Syntax
PDF
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Object Class
Java Tutorials
java training faridabad
Lecture-10_PHP-OOP.pptx
Class and Object JAVA PROGRAMMING LANG .pdf
Introduction to Java(basic understanding).ppt
Class and Object.pptx from nit patna ece department
OOP_with_Java_Beginner explanation .pptx
classes and objects in C++
Java OOP Programming language (Part 4) - Collection
Synapseindia reviews.odp.
Core java by a introduction sandesh sharma
Introduction to java
Taxonomy of Scala
4java Basic Syntax
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Ad

More from Hitesh-Java (20)

PPSX
Spring - Part 4 - Spring MVC
PPSX
Spring - Part 3 - AOP
PPSX
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
PPSX
Spring - Part 1 - IoC, Di and Beans
PPSX
JSP - Part 2 (Final)
PPSX
JSP - Part 1
PPSX
Struts 2 - Hibernate Integration
PPSX
Struts 2 - Introduction
PPSX
Hibernate - Part 2
PPSX
Hibernate - Part 1
PPSX
JDBC Part - 2
PPSX
PPSX
Java IO, Serialization
PPSX
Inner Classes
PPSX
Review Session - Part -2
PPSX
Review Session and Attending Java Interviews
PPSX
Exception Handling - Continued
PPSX
Exception Handling - Part 1
PPSX
OOPs with Java - Packaging and Access Modifiers
PPSX
OOP with Java - Abstract Classes and Interfaces
Spring - Part 4 - Spring MVC
Spring - Part 3 - AOP
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 1 - IoC, Di and Beans
JSP - Part 2 (Final)
JSP - Part 1
Struts 2 - Hibernate Integration
Struts 2 - Introduction
Hibernate - Part 2
Hibernate - Part 1
JDBC Part - 2
Java IO, Serialization
Inner Classes
Review Session - Part -2
Review Session and Attending Java Interviews
Exception Handling - Continued
Exception Handling - Part 1
OOPs with Java - Packaging and Access Modifiers
OOP with Java - Abstract Classes and Interfaces

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
KodekX | Application Modernization Development
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Spectroscopy.pptx food analysis technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
Understanding_Digital_Forensics_Presentation.pptx
KodekX | Application Modernization Development
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
Empathic Computing: Creating Shared Understanding
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Spectroscopy.pptx food analysis technology
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Object Class

  • 2. Page 1Classification: Restricted Quick recap: Topics covered till now • Data Types – Primitive (byte, short, int, long, double, float, char, boolean) • Data Types – Wrapper (Byte, Short, Integer …) • Data Types – Classes for String (String - immutable, StringBuilder – mutable, StringBuffer – mutable+threadsafe); Concept of String pooling / interning • Arrays • Conditional statements, Loops • OOP with Java – • Polymorphism – Method overloading (Compile-time), Method Overriding (Runtime) • Inheritance – extending classes, implementing interfaces; multiple inheritance • Abstraction – abstract classes vs interfaces • Encapsulation – POJO classes / Beans • Packaging, Class Modifiers (public, protected, default, private) • Static members vs Instance members • Exception handling
  • 3. Page 2Classification: Restricted Agenda • Object Class • toString() • equals() • Hashing • hashCode()
  • 4. Object Class as “The basic unit of any collection”
  • 5. Page 4Classification: Restricted Object class • Superclass for all Java classes • Any class without explicit extends clause is a direct subclass of Object • Methods of Object include: • String toString() • boolean equals (Object other) • int hashCode()
  • 6. Page 5Classification: Restricted 5 Method toString() • Returns String representation of object; describes state of object • Automatically called when: • Object is concatenated with a String • Object is printed using print() or println() • Object reference is passed to assert statement of the form: assert condition : object
  • 7. Page 6Classification: Restricted 6 Example Rectangle r = new Rectangle (0,0,20,40); System.out.println(r); Prints out: java.awt.Rectangle[x=0,y=0,width=20,height=40]
  • 8. Page 7Classification: Restricted 7 More on toString() • Default toString() method just prints (full) class name & hash code of object • Not all API classes override toString() • Good idea to implement for debugging purposes: • Should return String containing values of important fields along with their names • Should also return result of getClass().getName() rather than hard- coded class name
  • 9. Page 8Classification: Restricted Overriding toString(): example public class Employee { public String toString() { return getClass().getName() + "[name=" + name + ",salary=" + salary + "]"; } ... } Typical String produced: Employee[name=John Doe,salary=40000]
  • 10. Page 9Classification: Restricted 9 Overriding toString in a subclass • Format superclass first • Add unique subclass details • Example: public class Manager extends Employee { public String toString() { return super.toString() + "[department=" + department + "]"; } ... }
  • 11. Page 10Classification: Restricted 10 Example continued • Typical String produced: Manager[name=Mary Lamb,salary=75000][department=Admin] • Note that superclass reports actual class name
  • 12. Page 11Classification: Restricted 11 Equality testing • Method equals() tests whether two objects have same contents • By contrast, == operator test 2 references to see if they point to the same object (or test primitive values for equality) • Need to define for each class what “equality” means: Compare all fields Compare 1 or 2 key fields
  • 13. Page 12Classification: Restricted 12 Equality testing • Object.equals tests for identity: public class Object { public boolean equals(Object obj) { return this == obj; } ... } • Override equals if you don't want to inherit that behavior
  • 14. Page 13Classification: Restricted 13 Overriding equals() • Good practice to override, since many API methods assume objects have well-defined notion of equality • When overriding equals() in a subclass, can call superclass version by using super.equals()
  • 15. Page 14Classification: Restricted 14 Requirements for equals() method • If x is not null, then x.equals(null) must be false
  • 16. Page 15Classification: Restricted The perfect equals() method public boolean equals(Object otherObject) { if (this == otherObject) return true; if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; ... }
  • 17. Page 16Classification: Restricted 16 Hashing • Technique used to find elements in a data structure quickly, without doing full linear search • Important concepts: • Hash code: integer value used to find array index for data storage/retrieval • Hash table: array of elements arranged according to hash code • Hash function: computes hash code for element; uses algorithm likely to produce different hash codes for different objects to minimize collisions
  • 18. Page 17Classification: Restricted 17 Hashing in Java • Java library contains HashSet and HashMap classes • Use hash tables for data storage • Since Object has a hashCode method (hash function), any type of object can be stored in a hash table
  • 19. Page 18Classification: Restricted 18 Default hashCode() • Hashes memory address of object; consistent with default equals() method • If you override equals(), you should also redefine hashCode() • For class you are defining, use product of hash of each field and a prime number, then add these together – result is hash code
  • 20. Page 19Classification: Restricted 19 Example public class Employee { public int hashCode() { return 11 * name.hashCode() + 13 * new Double(salary).hashCode(); } ... }
  • 21. Page 20Classification: Restricted 20 Topics to be covered in next session • Recap of Arrays • Introduction to Collections API • Lists – ArrayList, Vector, LinkedList