Menu
Coddy logo textTech

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:

MethodPurpose
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@1a2b3c4d

This 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 icon

Challenge

Easy

Let'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) and year (int)
    • A constructor that initializes both fields
    • Getter methods getName() and getYear()
    Don't override any methods from Object - we want to see the default behavior first.
  • 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])
  • Main.java: Demonstrate the difference between default and overridden Object methods. You'll receive three inputs: a gadget name, year, and brand. Create both a Gadget and a Smartphone using 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 (print true or false)
    • Print the smartphone using its overridden toString()

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:

MethodPurpose
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@1a2b3c4d

You 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())
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming