Java's Common Root: Object Superclass
The Object superclass is Java's common parent class: every class other than Object extends it directly or indirectly. This shared starting point explains why objects from very different classes support certain common methods. You meet it when a method accepts an Object parameter, when you print an object, or when a simple class appears to have methods you never wrote. Here, the goal is to recognize that common foundation and use it correctly.
A superclass without an explicit declaration
Consider a class used by a ticketing program:
class Ticket {
}Because this ordinary class declaration has no extends clause, its direct superclass is Object. You do not need to write extends Object to establish that relationship. Object itself is the root and has no superclass.
Object belongs to java.lang, a package whose public classes are available without an explicit import. Both a library class such as String and a programmer-defined class such as Ticket share this root. This observation does not require you to design a new inheritance structure.
An Object reference still refers to a specific object
The following declaration is valid:
Object item = new Ticket();The variable's declared type is Object, but the object created is a Ticket. Assigning its reference to item does not construct another object or change that object's class.
The declared type matters when Java checks a method call. Through an Object variable, you can call accessible methods provided by Object. You cannot call a method declared only in Ticket merely because the referenced object is a ticket. Check the variable's declared type when deciding whether a method call compiles.
An Object variable can also refer to an array. Primitive types such as int and boolean are not classes and do not extend Object; Java has separate rules for converting primitive values to wrapper objects. Those conversions are outside this lesson.
Common methods and their limits
Three useful public methods illustrate the common foundation:
Method | What it provides | Important limit |
|---|---|---|
toString() | A String representation of the object | The representation need not be a useful description of its contents |
equals(Object other) | A boolean equality result | Object's own implementation checks whether references identify the same object |
getClass() | Information identifying the object's runtime class | An Object variable does not make the object's runtime class Object |
Reference identity means that two references identify the very same object. For non-null references to objects using Object's own equals implementation, the governing rule is:
Example: If Object copy = item;, both variables refer to the same object. The assignment copies a reference; it does not duplicate the object. Two separate evaluations of new Object() create distinct objects, so their inherited equals comparison returns false.
Classes such as String supply their own equality behavior. Therefore, do not apply Object's default equality rule to every class without checking its behavior.
What to remember
- Object is the common superclass of Java classes other than itself.
- An Object reference can identify an object of a more specific class.
- The declared type controls which method calls Java accepts.
- Object's default equals checks reference identity; null cannot receive a method call.
Example: Tokens in a museum kiosk
A museum kiosk uses the Java class below. The class declares no methods, so its objects use Object's default equality behavior. Assume all statements appear in a valid surrounding program.
class Token {
}
Object first = new Token();
Object second = first;
Object third = new Token();The Object superclass supplies the common methods available to these Token objects.
a) Identify Token's direct superclass and explain why all three variable declarations are valid.
Token has no explicit extends clause, so its direct superclass is Object. Each new Token() expression creates a Token object, and an Object variable can store a reference to that object.
The declaration of second copies the reference held by first. It does not create a new Token. All three declarations are valid, and Token's direct superclass is Object.
b) Determine the two boolean values printed by the following statements.
System.out.println(first.equals(second));
System.out.println(first.equals(third));First trace object creation. The first new Token() expression creates ________. Assigning first to second copies its reference. The later new Token() expression brings the total to ________.
Next apply Object's default equals rule. The references first and second identify the same object, so first.equals(second) returns true. The references first and third identify different objects, so first.equals(third) returns false. Matching classes alone do not establish that references identify the same object.
The printed values, in order, are:
true
falsec) Consider first.toString(), first.equals(third), and first.getLabel() independently. Which expressions compile, and how many compile?
Use the declared type Object to check available methods. Object provides public toString() and equals(Object) methods. Therefore, first.toString() compiles and returns a String, while first.equals(third) compiles and returns a boolean.
Object has no getLabel() method, so first.getLabel() does not compile. The supplied Token class does not declare it either. The two valid expressions are first.toString() and first.equals(third), giving a total of 2 expressions that compile.
A ticketing program uses the Java class below. It inherits Object.equals without replacing it. The declarations following the class run inside a method.
class Ticket {
int number = 48;
}Ticket first = new Ticket();
Ticket second = new Ticket();
Ticket saved = first;Complete the comparison results using true or false: first.equals(second) returns , and first.equals(saved) returns .
A reception display stores a String reference in an Object variable using the declaration below. For each expression, enter yes if it compiles or no if it does not compile. Treat each expression independently.
Object text = new String("Arrival");| Expression | Compiles: yes or no |
| text.toString() | |
| text.equals(text) | |
| text.length() | |
| text.getClass() |