10.7. Object Superclass¶
The Object class is the superclass of all other classes in Java and a part of the
built-in java.lang package. If a parent class isn’t specified using the extends keyword, the
class will inherit from the Object
class.
What does a class inherit from the Object
class?
There are two main methods that are most used, toString() and equals(Object), from the
Object class at the bottom, which are covered in more detail below.
String toString()
boolean equals(Object other)
10.7.1. toString() method¶
One commonly overriden Object method is toString(), which is often used to print out the attributes of an object. It is a good idea to write your own toString() method in every class. In a subclass, toString() can call the superclass toString() method using super.toString() and then add on its own attributes.
In the following code, the Person class overrides the Object toString() method and the Student class overrides the Person toString() method. They each add on their attributes.
After trying the code below, add another subclass called APStudent that extends Student with a new attribute called APscore and override the toString() method to call the superclass method and then add on the APscore. Create an APStudent object in the main method to test it.
10.7.2. equals Method¶
One of the important things that gets inherited from the Object superclass is the equals(Object obj)
method. This method is used to test if the current object and the passed object called obj
are equal. But what does that mean?
As seen in the code below, the equals
method that is inherited from the Object
class only returns true if the two objects references refer to the same object.
Try to guess what this code will print out before running it.
The equals
method inherited from the Object
class only returns true when the two references point to the same object as shown in the code above and figure 1 below.
10.7.3. Overriding the equals Method¶
If you want to change how the inherited equals
method works you can override it so that the new method is called instead of the inherited one. The String
class overrides the inherited equals method to return true when the two objects have the same characters in the same order as shown in the code below.
Try to guess what this code will print out before running it.
Any class can override the inherited equals
method by providing a method with the same method signature (method name and parameter list) and return type. The provided method will be called instead of the inherited one, which is why we say that the new method overrides the inherited method. The Person
class below overrides the inherited equals
method.
Try to guess what this code will print out before running it.
You can step through this code in the Java Visualizer by clicking on the following link: OverrideEquals Ex.
To write your own equals method, you must:
Use the
public boolean equals(Object other)
method signatureType cast other to your Classname
Return whether this object’s attribute(s) equals the other object’s attribute(s) with == for primitive types like int and double, or equals for reference types like String or another class.
public boolean equals(Object other)
{
// Type cast other to your Classname
Classname otherObj = (Classname) other;
// Check if attributes are equal
return (this.attribute == otherObj.attribute);
// or this.attribute.equals(otherObj.attribute) if attribute a String
}
If you need to check multiple attributes, for example a name and an address for Person objects, you can use && to combine tests.
return (this.attribute1 == otherObj.attribute1) &&
this.attribute2.equals(otherObj.attribute2)
If you are writing an equals method for a subclass, you can call the superclass equals using the super keyword to check the attributes in the superclass and then check the attributes in the subclass.
return super.equals(otherObj) &&
(this.attribute == otherObj.attribute)
10.7.4. Programming Challenge : Savings Account¶
In the following code, a bank account class contains the account holder’s name and the money balance in the account.
Work in pairs to write the following code and test each part before moving on to the next step:
Write a toString() method for Account that returns the name and balance with a comma in between.
Write an equals method for Account that checks that the name and balance are equal.
Write a subclass called SavingsAccount that extends Account and adds an interest rate variable.
Write a toString() method for SavingsAccount that returns a call to the super toString() method and the interest rate.
Write an equals method for SavingsAccount that calls the superclass equals method and checks that the interest rates are equal.
Complete the subclass SavingsAccount below which inherits from Account and adds an interest rate variable. Write a toSTring and an equals method for it.
10.7.5. Summary¶
The Object class is the superclass of all other classes in Java and a part of the built-in java.lang package.
The following Object class methods and constructors, including what they do and when they are used, are part of the Java Quick Reference:
String toString()
boolean equals(Object other)
Subclasses of Object often override the equals and toString methods with class-specific implementations.