Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

9.7 Object Superclass

2 min readjanuary 2, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Introduction to the Object Superclass

Now that we have learned about inheritance, what even allows our classes and objects that we have created to work the way they do? Where do the general characteristics of all objects come from? The answer lies in the Object class.

The Object class is the superclass of all other classes as well as arrays and other data types. The Object class is part of the java.lang package.

When we call a constructor to a "top-level class" that the coder hasn't declared a superclass for, the Object constructor is implicitly called. In other words, the Object constructor is implicitly called when we call a constructor in a class that doesn't explicitly extend another class. This will give the object some properties and methods that are common to all classes.

Object Methods

In the Object class, there are also a few methods that are provided by default. The three that we will look at are as follows:

  • hashCode()
  • equals()
  • toString()

hashCode()

The hashCode() method returns what is known as a hash code, an integer that is used to represent the memory location of the object. This is usually based on the properties of an object and any two objects with the same properties should have the same hash code.

equals()

This goes along with the equals() method that we first learned about in Unit 3. This method determines whether two objects are equal to each other based on whether they have the same properties or not. If two objects are determined to be equal, then they must have the same hash code.

toString()

Finally, we have the toString() method from Unit 5. This method returns a string representation of the object that the method is called on. This usually prints the properties of the object, but by default, the method returns objectName + "@" + hashCode() , which in most situations, does not give any useful information about an object.

For the three methods above to achieve their intended results, we usually override these methods and write our own class-specific implementation of these methods to make sure that the methods work the way we want them to.

Key Terms to Review (8)

equals()

: The equals() method is used to compare two objects for equality. It checks if the values of the objects are the same, rather than comparing their memory addresses.

hashCode()

: The hashCode() method is used in Java to generate a unique integer value representing an object's identity. It is often overridden in classes when custom equality comparisons need to be made.

java.lang package

: The java.lang package is a built-in package in Java that provides fundamental classes and interfaces for the Java language. It includes classes such as String, Integer, and Math.

Methods

: Methods are functions defined within a class that perform specific tasks or actions when called upon by an object. They can manipulate data, interact with other objects, or return values.

Object superclass

: The Object superclass is the root class in Java that all other classes inherit from. It provides basic functionality and methods that are common to all objects.

override

: Overriding refers to providing a different implementation for a method inherited from a superclass or interface in its subclass or implementing class respectively. It allows the subclass to customize the behavior of inherited methods.

Properties

: Properties are characteristics or attributes of an object that define its state. They store data and can be accessed and modified by the object's methods.

toString()

: The toString() method is used to convert an object into a string representation. It returns a string that represents the state of the object.

9.7 Object Superclass

2 min readjanuary 2, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Introduction to the Object Superclass

Now that we have learned about inheritance, what even allows our classes and objects that we have created to work the way they do? Where do the general characteristics of all objects come from? The answer lies in the Object class.

The Object class is the superclass of all other classes as well as arrays and other data types. The Object class is part of the java.lang package.

When we call a constructor to a "top-level class" that the coder hasn't declared a superclass for, the Object constructor is implicitly called. In other words, the Object constructor is implicitly called when we call a constructor in a class that doesn't explicitly extend another class. This will give the object some properties and methods that are common to all classes.

Object Methods

In the Object class, there are also a few methods that are provided by default. The three that we will look at are as follows:

  • hashCode()
  • equals()
  • toString()

hashCode()

The hashCode() method returns what is known as a hash code, an integer that is used to represent the memory location of the object. This is usually based on the properties of an object and any two objects with the same properties should have the same hash code.

equals()

This goes along with the equals() method that we first learned about in Unit 3. This method determines whether two objects are equal to each other based on whether they have the same properties or not. If two objects are determined to be equal, then they must have the same hash code.

toString()

Finally, we have the toString() method from Unit 5. This method returns a string representation of the object that the method is called on. This usually prints the properties of the object, but by default, the method returns objectName + "@" + hashCode() , which in most situations, does not give any useful information about an object.

For the three methods above to achieve their intended results, we usually override these methods and write our own class-specific implementation of these methods to make sure that the methods work the way we want them to.

Key Terms to Review (8)

equals()

: The equals() method is used to compare two objects for equality. It checks if the values of the objects are the same, rather than comparing their memory addresses.

hashCode()

: The hashCode() method is used in Java to generate a unique integer value representing an object's identity. It is often overridden in classes when custom equality comparisons need to be made.

java.lang package

: The java.lang package is a built-in package in Java that provides fundamental classes and interfaces for the Java language. It includes classes such as String, Integer, and Math.

Methods

: Methods are functions defined within a class that perform specific tasks or actions when called upon by an object. They can manipulate data, interact with other objects, or return values.

Object superclass

: The Object superclass is the root class in Java that all other classes inherit from. It provides basic functionality and methods that are common to all objects.

override

: Overriding refers to providing a different implementation for a method inherited from a superclass or interface in its subclass or implementing class respectively. It allows the subclass to customize the behavior of inherited methods.

Properties

: Properties are characteristics or attributes of an object that define its state. They store data and can be accessed and modified by the object's methods.

toString()

: The toString() method is used to convert an object into a string representation. It returns a string that represents the state of the object.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.