Commit 5176e8e
committed
feat: demonstrate inheritance from Object class with custom overrides
WHAT was implemented:
- Created a custom class `MyObject` that overrides fundamental methods from `java.lang.Object`:
- `toString()` → returns a custom string `"My String"`.
- `hashCode()` → returns a constant value `100`.
- `equals(Object o)` → compares objects based on `hashCode()` equality.
- In `LangDemo2`, instantiated `MyObject` and printed it, which automatically calls the overridden `toString()` method.
WHY this matters:
- In Java, **every class implicitly inherits from `Object`**, even if not explicitly declared.
- This program proves that methods like `toString()`, `equals()`, and `hashCode()` are always available to override in user-defined classes.
- It highlights the role of `Object` as the root class in the Java class hierarchy.
KEY CONCEPTS COVERED:
1. **Inheritance from Object**
- All classes in Java extend `Object` (directly or indirectly).
- That’s why `toString()`, `equals()`, `hashCode()`, `getClass()`, `wait()`, `notify()`, etc. are available to every class.
2. **Overriding Object methods**
- `toString()` is overridden to provide a meaningful string instead of default `ClassName@hashCode`.
- `hashCode()` is overridden to return a constant for demonstration (not recommended in production).
- `equals()` is overridden to compare objects logically based on `hashCode()`.
3. **Final Methods**
- Methods like `wait()`, `notify()`, `notifyAll()` in `Object` are `final` and cannot be overridden.
- This ensures consistent thread synchronization behavior across all classes.
4. **Demonstration**
- Printing `o2` directly → invokes `toString()` implicitly → outputs `"My String"` instead of the default.
- Shows that even without explicitly extending `Object`, our custom class inherits and can override its methods.
REAL-WORLD APPLICATIONS:
- **Custom Classes**: Override `equals()` and `hashCode()` for logical equality (e.g., comparing employees by ID).
- **Collections**: Correct `equals()` + `hashCode()` implementation is crucial for `HashMap`, `HashSet`, etc.
- **Logging/Debugging**: Overriding `toString()` provides clear object state representation in logs.
- **Thread Synchronization**: `wait()`/`notify()` methods come from `Object`, proving every object can act as a monitor.
BEST PRACTICES:
- Always override `equals()` and `hashCode()` together.
- Ensure `hashCode()` is consistent and distributes values well for performance in hash-based collections.
- Provide meaningful `toString()` for debugging/logging.
- Avoid overriding methods like `wait()`/`notify()` since they are final.
KEYWORDS:
Object class, inheritance, overriding, toString, equals, hashCode, Java class hierarchy, root class.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c58835c commit 5176e8e
1 file changed
+12
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
14 | | - | |
15 | | - | |
| 13 | + | |
16 | 14 | | |
17 | 15 | | |
18 | | - | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
| |||
26 | 23 | | |
27 | 24 | | |
28 | 25 | | |
29 | | - | |
| 26 | + | |
30 | 27 | | |
31 | | - | |
| 28 | + | |
32 | 29 | | |
33 | 30 | | |
34 | 31 | | |
35 | | - | |
| 32 | + | |
| 33 | + | |
36 | 34 | | |
37 | 35 | | |
38 | | - | |
| 36 | + | |
39 | 37 | | |
40 | 38 | | |
0 commit comments