oop

15. The Object Class: Root of All

Analyze the code, write your predicted output, and then reveal the solution.

Scenario

Every class in Java implicitly extends java.lang.Object. Learn how overriding toString() and equals() changes default behavior.

Code Block
class Book { String title; Book(String title) { this.title = title; } @Override public String toString() { return "Book Title: " + title; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Book)) return false; return this.title.equals(((Book) obj).title); } } public class Main { public static void main(String[] args) { Book b1 = new Book("Java Mastery"); Book b2 = new Book("Java Mastery"); System.out.println(b1); // Calls toString() System.out.println("Memory Compare: " + (b1 == b2)); System.out.println("Value Compare: " + b1.equals(b2)); } }
Take Your Shot
Predicted Output

Type exactly what you think will be printed in the console.

15. The Object Class: Root of All | DevExCode