For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
45. Packages in Java
52. Java Collection
55. Generics In Java
56. Java Interfaces
59. Streams in Java
62. Thread in Java
66. Deadlock in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
77. JUnit Testing
80. Jar file in Java
81. Java Clean Code
85. Java 8 features
86. String in Java
92. HashMap in Java
97. Enum in Java
100. Hashcode in Java
104. Linked List in Java
108. Array Length in Java
110. Split in java
111. Map In Java
114. HashSet in Java
117. DateFormat in Java
120. Java List Size
121. Java APIs
127. Identifiers in Java
129. Set in Java
131. Try Catch in Java
132. Bubble Sort in Java
134. Queue in Java
141. Jagged Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
150. parseInt in Java
152. Abstraction in Java
153. String Input in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
163. Deque in Java
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
176. Square Root in Java
189. Javafx
In Java, everything revolves around classes and objects. As a beginner, one of the first concepts you'll encounter is inheritance, which allows one class to acquire the properties of another. But did you know that every class in Java, even the ones you create, inherits from a single universal class?
In this blog, we’ll explore which class is inherited by all Java classes, understand why it matters, and look at some simple examples and terminology to clarify the concept.
Learning Java is just the beginning. Take the next step with upGrad’s Software Engineering program designed for future-ready developers.
In Java programming, the answer to the question of which class is inherited by all other classes lies in the fundamental class hierarchy. All classes in Java inherit from java.lang.Object class, either directly or indirectly.
The "Object" class is the root class of the Java class hierarchy and serves as the ultimate superclass for all other classes. This means that every class in Java is implicitly a subclass of the "Object" class
The "Object" class provides a set of methods and functionalities that are common to all Java objects. These methods include "equals()", "hashCode()", "toString()", and more. From the "Object" class, all other classes inherit these fundamental methods, allowing for consistent behavior and interoperability among objects.
This design provides a common structure and behavior across all classes. Here’s why it’s important:
Already know Java? Expand your skill set with data science and AI through IIIT-B’s Executive Diploma program on upGrad.
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
Output:
No output yet – just a class definition.
Explanation:
Even though we didn’t write extends Object, the Animal class still inherits from Object automatically.
class Animal {
String name = "Tiger";
}
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
System.out.println(a.toString());
System.out.println(a.hashCode());
System.out.println(a.getClass());
}
}
Output (will vary):
Animal@6d06d69c
12345678
class Animal
Explanation:
Ready to transition from Java developer to data scientist? Kickstart your journey with the Executive Diploma in Data Science & AI by IIIT-B.
A superclass is a class that is extended by other classes. In Java, the Object class is the superclass of all other classes.
A subclass is a class that extends another class.
Example:
class Dog extends Animal {}
Describes inheritance: a subclass is a type of its superclass. E.g., A Dog is an Animal.
In mutiple inheritance, a class inherits from a class which itself inherits from another.
class A {}
class B extends A {}
class C extends B {}
Here, C indirectly inherits from A.
The base (or root) class of all Java classes is java.lang.Object. This class is automatically included in every Java project and does not require importing.
It provides key methods that every Java object can use without additional code.
Let’s explore some common methods of the Object class with examples:
Returns a string representation of the object.
class Sample {}
public class Main {
public static void main(String[] args) {
Sample s = new Sample();
System.out.println(s.toString());
}
}
Output:
Sample@1b6d3586
Explanation:Shows class name and memory address (hashcode in hex).
Checks whether two objects are equal.
public class Main {
public static void main(String[] args) {
String a = new String("hello");
String b = new String("hello");
System.out.println(a.equals(b));
}
}
Output:
true
Explanation:The equals() method in the String class is overridden to compare the actual content of the strings. Since both a and b contain "hello", the result is true, even though they are different objects in memory.
Returns a unique hash value for the object.
class Sample {}
public class Main {
public static void main(String[] args) {
Sample s = new Sample();
System.out.println(s.hashCode());
}
}
Output:
12345678 (varies each time)
Explanation:The hashCode() method returns an integer hash value of the object, which is used in hashing-based collections like HashMap. The actual number may vary each time the program is run because it’s based on the object’s memory address or internal hashing logic.
Returns the runtime class of the object.
class Sample {}
public class Main {
public static void main(String[] args) {
Sample s = new Sample();
System.out.println(s.getClass());
}
}
Output:
class Sample
Explanation:
The getClass() method (inherited from the Object class) returns the runtime class of the object. In this case, the output shows that the object s is an instance of the Sample class.
Parent Class in Java: How It Works Internally
When you write a class like this:
class MyData {}
The compiler internally treats it as:
class MyData extends Object {}
You can verify this by checking decompiled bytecode. This is how Java ensures every class has a base set of methods.
To sum it up:
Whether you're just starting or brushing up your basics, this concept is fundamental to mastering Java's object-oriented system.
All classes in Java implicitly inherit from the Object class, either directly or indirectly. It is the root class of the Java class hierarchy and provides common methods like toString(), equals(), hashCode(), and getClass().
Yes, every class created in Java without explicitly extending another class will automatically inherit from the Object class. This ensures that all Java classes have access to the basic utility methods provided by Object.
No, inheritance from the Object class is not optional. It’s enforced by the Java language specification to maintain consistency and ensure that all objects share a common base type.
Common methods inherited from the Object class include toString(), equals(), hashCode(), getClass(), clone(), wait(), notify(), and finalize(). These provide basic object behavior across all Java classes.
Yes, a class can extend another user-defined or library class. However, all such classes will still eventually inherit from Object either directly or indirectly through the extended class.
Yes, the Object class is part of the java.lang package, which is automatically imported in every Java program. You don’t need to manually import it to use its features.
The Object class provides a base for Java’s inheritance hierarchy. It allows all classes to be treated uniformly and enables polymorphism, reflection, serialization, and interoperability with collection classes like ArrayList and HashMap.
Yes, you can explicitly extend the Object class, but it’s usually unnecessary since every class already inherits from it by default. Explicitly writing extends Object doesn’t change the behavior.
Interfaces themselves do not extend the Object class. However, when a class implements an interface, it still inherits the Object class, so the interface's implementing class can use Object's methods.
No, the Object class is not abstract. You can create instances of it directly using new Object(), although it’s uncommon to use it in this way without subclassing.
Inheriting from the Object class ensures that every class has a standard set of methods and can interact with core Java features like collections, serialization, and runtime type identification uniformly.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.