Skip to content

Commit 5176e8e

Browse files
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

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
//Proof that every class is inheriting directly or indirectly from every other class. That is Object Class.
2-
class MyObject
3-
{
4-
public String toString() //Method
5-
{
1+
/* Prove that every class is inheriting directly or indirectly from every other class. That is Object Class.
2+
*/
3+
4+
class MyObject {
5+
public String toString() {
66
return "My String";
77
}
88

9-
public int hashCode()
10-
{
9+
public int hashCode() {
1110
return 100;
1211
}
1312

14-
public boolean equals(Object o)
15-
{
13+
public boolean equals(Object o) {
1614
return this.hashCode()==o.hashCode();
1715
}
18-
1916
/*public void wait() we can not override this methods beacuse it show final */
2017
}
2118

@@ -26,15 +23,16 @@ public boolean equals(Object o)
2623

2724
public class LangDemo2 {
2825
public static void main(String[] args) {
29-
/*
26+
/*
3027
MyObject2 o1=new MyObject2();
31-
*/
28+
*/
3229

3330
MyObject o2=new MyObject();
3431

35-
/* System.out.println(o1);*/
32+
/* System.out.println(o1); */
33+
3634
System.out.println(o2);
3735

38-
/* MyObject2 o3=new MyObject2();*/
36+
/* MyObject2 o3=new MyObject2(); */
3937
}
4038
}

0 commit comments

Comments
 (0)