The Object Class
The Object class is the superclass of all other classes. Classes, such as Circle and String, are subclasses of Object: superclaes
Subclasses inherit, or receive, the methods of its superclass. The Object class includes methods for comparing objects and representing an object as a string:
Class Object (java.lang.Object)
equals (Object obj) returns true if obj is equal to the object. toString ( )
returns a String that represents the object.
A subclass typically contains its own version of the equals() and toString() superclass methods to better suit the object of the subclass. For example, two Circle objects are equal when they both have the same radius, and two String objects are equal when they consist of the same set of characters. When a subclass redefines a superclass method, the subclass method is said to override the superclass method. The Circle class should contain an equals() method that compares the state of the object to another Circle object and a toString() method that returns a String describing the object:
* Determines if the object is equal to another * Circle object. * pre: c is a Circle object.
* post: true has been returned if the objects have
* the same radii. false has been returned otherwise.
public boolean equals(Object c)
Circle testObj = (Circle)c;
if (testObj.getRadius() == radius)
* Returns a String that represents the Circle object. * pre: none * post: A string representing the Circle object has * been returned. public String toString()
circleString = "Circle has radius " + radius;
The equals() method requires an Object parameter. In the body of the method, the obj parameter must be cast as the appropriate type, in this case Circle, and then assigned to an object of the appropriate type. If an Object variable is cast with an incompatible class, then the exception ClassCastException will be generated. To convert an object to its superclass Object, no class casting is required.
The code below creates two Circle objects, compares them, and displays information about the objects. An object's toString() method is invoked when an object is passed to the println() method: public static void main(String args)
Circle spotl = new Circle(3);
Circle spot2 = new Circle(4);
System.out.println("Objects are equal.");
System.out.println("Objects are not equal.");
The code above displays the output:
Objects are not equal.
Circle has radius 3.0
Circle has radius 4.0
Modify the Circle class to override the equals() and toString() methods, as shown in the previous section. Modify existing client code to test the new methods.
Modify the Rectangle class to override the equals() and toString() methods. Two rectangles are equal when they both have the same length and width. Modify the existing client code to test the new method.
Classes Using Classes
A class may contain member variables that are class data types. Complex data can be easily represented in this way. A class that contains class member variables demonstrates a has-a relationship. The class "has a" class. For example, a class with a String member variable demonstrates a has-a relationship. The Bank program specification on the next page is best implemented with two classes. One class (Account) has a member variable (Customer) for representing the customers that hold the accounts: A bank maintains accounts where account holders can deposit money and withdraw money. The account holders are customers with a first and last name...
Please join StudyMode to read the full document