The Object Class
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 24 of 87.
In Java, every class you create automatically inherits from a special class called Object. Even if you don't write extends anywhere, your class is still a child of Object. This makes Object the root of the entire class hierarchy.
public class Person {
// This implicitly extends Object
}
// Is equivalent to:
public class Person extends Object {
}Because every class inherits from Object, every object in Java has access to several built-in methods. The most commonly used ones include:
| Method | Purpose |
|---|---|
toString() | Returns a string representation of the object |
equals(Object obj) | Compares two objects for equality |
hashCode() | Returns a hash code value for the object |
getClass() | Returns the runtime class of the object |
By default, these methods have generic implementations. For example, toString() returns the class name followed by the object's memory address, which isn't very useful:
Person p = new Person();
System.out.println(p.toString()); // Output: Person@1a2b3c4dThis is why you'll often override these methods in your own classes to provide meaningful behavior. Understanding that Object is the universal parent helps explain why you can store any object in an Object variable and why certain methods are available on every object you create.
Challenge
EasyLet's explore how the Object class serves as the universal parent of all Java classes by building a simple gadget inventory system that demonstrates inherited methods from Object.
You'll create three files to organize your code:
Gadget.java: Create a class representing an electronic gadget. Your class should have:- Private fields for
name(String) andyear(int) - A constructor that initializes both fields
- Getter methods
getName()andgetYear()
- Private fields for
Smartphone.java: Create a subclass that extends Gadget and adds smartphone-specific features:- A private field for
brand(String) - A constructor that takes name, year, and brand - use
super(name, year)for the parent portion - A method
getBrand()that returns the brand - Override the
toString()method to return:Smartphone: [brand] [name] ([year])
- A private field for
Main.java: Demonstrate the difference between default and overriddenObjectmethods. You'll receive three inputs: a gadget name, year, and brand. Create both aGadgetand aSmartphoneusing the same name and year (plus brand for the smartphone), then:- Print the gadget's class name using
getClass().getSimpleName() - Print whether the gadget's default
toString()contains the "@" symbol (printtrueorfalse) - Print the smartphone using its overridden
toString()
- Print the gadget's class name using
You will receive three inputs: the gadget/smartphone name (String), year (int), and brand (String).
This challenge shows why overriding Object methods matters - the default toString() gives you something like Gadget@1a2b3c, while your custom version provides meaningful information!
Cheat sheet
In Java, every class automatically inherits from the Object class, making it the root of the entire class hierarchy:
public class Person {
// This implicitly extends Object
}
// Is equivalent to:
public class Person extends Object {
}Every object in Java has access to several built-in methods inherited from Object:
| Method | Purpose |
|---|---|
toString() | Returns a string representation of the object |
equals(Object obj) | Compares two objects for equality |
hashCode() | Returns a hash code value for the object |
getClass() | Returns the runtime class of the object |
By default, toString() returns the class name followed by the object's memory address:
Person p = new Person();
System.out.println(p.toString()); // Output: Person@1a2b3c4dYou can override these methods in your own classes to provide meaningful behavior. You can store any object in an Object variable since all classes inherit from it.
Try it yourself
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read inputs
String name = scanner.nextLine();
int year = scanner.nextInt();
scanner.nextLine(); // consume newline
String brand = scanner.nextLine();
// TODO: Create a Gadget object with name and year
// TODO: Create a Smartphone object with name, year, and brand
// TODO: Print the gadget's class name using getClass().getSimpleName()
// TODO: Print whether gadget's toString() contains "@" (true or false)
// TODO: Print the smartphone (uses overridden toString())
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System