Tutorial Playlist
Java, a powerful and widely used programming language, follows the principles of object-oriented programming (OOP). In this, classes form the foundation of OOP, allowing developers to create objects with specific attributes and behaviors. But have you ever wondered which class serves as the ultimate ancestor for all Java classes? Have you ever asked yourself, ‘All classes in Java are inherited from which class?’. In this article, we will delve into the concept of class inheritance in Java and unveil the root class from which all other classes got inherited.
In Java, the answer to the question of which class is inherited by all other classes lies in the fundamental class hierarchy. All classes in Java, directly or indirectly, inherit from the "Object" class.
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.
Let's consider a couple of examples to better understand the concept of class inheritance in Java.
Suppose we want to create a custom class called "Car" that represents various car objects. We can define the class as follows:
public class Car {
private String brand;
private String color;
// Constructor
public Car(String brand, String color) {
this.brand = brand;
this.color = color;
}
// Getters and Setters
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
In this example, the "Car" class is a custom class that we have defined. Although we haven't explicitly mentioned it, this class automatically inherits from the "Object" class.
Let's explore how the "Object" class methods can be used in our custom "Car" class:
Public class Car {
// ...
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Car otherCar = (Car) obj;
return this.brand.equals(otherCar.brand) && this.color.equals(otherCar.color);
}
@Override
public int hashCode() {
return Objects.hash(brand, color);
}
@Override
public String toString() {
return "Car [brand=" + brand + ", color=" + color + "]";
}
}
The code overrides the "equals()", "hashCode()", and "toString()" provided by the "Object" class.
Before we proceed further, let's clarify some terminologies related to class inheritance.
IS-A Relationship: Inheritance establishes an IS-A relationship between classes. For example, if class B inherits from class A, we can say that B IS-A A. This means that objects of class B can be treated as objects of class A.
Inheritance in Java establishes an IS-A relationship between classes. This connection allows objects of the subclass to be treated as objects of the superclass. For example, consider the "Car" class we defined earlier. Since the "Car" class implicitly inherits from the "Object" class, we can treat a "Car" object as an "Object" object. This enables us to utilize the methods provided by the "Object" class on our "Car" objects.
public class Vehicle {
// ...
public void start() {
System.out.println("The vehicle has started.");
}
}
public class Car extends Vehicle {
// ...
public void accelerate() {
System.out.println("The car is accelerating.");
}
public static void main(String[] args) {
Car car = new Car();
car.start();
car.accelerate();
}
}
Output:
The code defines classes Vehicle and Car, and when executing main(), the Car instance calls start() and accelerate() methods, printing the corresponding messages.
Java also supports multilevel inheritance, where a subclass can inherit from a superclass, and another class can inherit from that subclass. Let's consider an example:
public class Animal {
// ...
public void eat() {
System.out.println("The animal is eating.");
}
}
public class Mammal extends Animal {
// ...
public void sleep() {
System.out.println("The mammal is sleeping.");
}
}
public class Dog extends Mammal {
// ...
public void bark() {
System.out.println("The dog is barking.");
}
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.sleep();
dog.bark();
}
}
Output:
Running the main() method will execute these method calls and produce the corresponding output.
The base class of all classes in Java is the java.lang.Object class. It is the root of the class hierarchy and serves as the ultimate superclass for all other classes, whether they are built-in classes provided by Java or user-defined classes.
Every class in Java implicitly or explicitly extends the Object class, which means that all classes inherit the methods and functionalities provided by Object. This inheritance allows objects of any class to share common behaviors, such as the ability to be used in collections, compared for equality, and converted to string representations.
Here's a basic example to demonstrate how all classes in Java implicitly extend the Object class:
class MyClass {
// MyClass implicitly extends Object
@Override
public String toString() {
return "This is MyClass";
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.println(obj.toString()); // Output: This is MyClass
}
}
In this example, the MyClass does not explicitly extend any class, so it automatically becomes a subclass of Object. Consequently, we can use the toString() method inherited from Object within the MyClass instance.
Output:
Keep in mind that the Object class provides several other essential methods like equals(), hashCode(), and getClass(), which are commonly used in Java programming for object comparison, hash code generation, and obtaining runtime class information.
The superclass of every class in Java is the Object class. It plays a fundamental role in the programming language and is the base class of all the classes. The Object class is defined in the java.lang package, and it implicitly serves as the parent class of all classes unless explicitly specified.
All Java classes are derived from the Object class, directly or indirectly. In other words, every class in Java extends Object either directly or through a chain of inheritance.
The Object class is considered the parent class of all Java classes. It provides some fundamental methods and functionalities that are inherited by all other classes. These include:
The parent class of all Java classes is the Object class. If a class is defined without explicitly specifying a superclass (using the extends keyword), it implicitly extends the Object class. For example:
The Object class serves as the base class, and it is implicitly inherited by all Java classes. This allows all objects in Java to share common methods provided by Object and enables certain functionalities such as using objects in collections, comparing objects, and more.
Here's a simple example to illustrate how every class in Java implicitly inherits from the Object class:
class MyClass {
// MyClass implicitly extends Object
@Override
public String toString() {
return "This is MyClass";
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.println(obj.toString()); // Output: This is MyClass
}
}
Output:
Here, the MyClass does not explicitly extend any class, so it automatically becomes a subclass of Object. As a result, we can use the toString() method inherited from Object within the MyClass instance.
In Java, a parent class refers to a class that serves as the basis or blueprint for other classes. It is also known as a superclass. Other classes, called subclasses or child classes, can inherit properties and behavior from the parent class. This inheritance mechanism is fundamental in object-oriented programming (OOP), allowing code reuse and creating a hierarchical relationship between classes.
To define a parent class of all classes in Java, you use the class keyword. Here's a simple example:
// Parent class (Superclass)
class Vehicle {
String brand;
int year;
void start() {
System.out.println("Vehicle starting...");
}
void accelerate() {
System.out.println("Vehicle accelerating...");
}
void stop() {
System.out.println("Vehicle stopping...");
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.start(); // Output: Vehicle starting...
vehicle.accelerate(); // Output: Vehicle accelerating...
vehicle.stop(); // Output: Vehicle stopping...
}
}
In this example, the Vehicle class is a parent class with three properties: brand and year, and three methods: start(), accelerate(), and stop().
Now, let's create a subclass that inherits from the Vehicle class:
// Subclass (Child class)
class Car extends Vehicle {
int numDoors;
void honk() {
System.out.println("Car honking...");
}
}
In this example, the Car class is a subclass of Vehicle. It inherits the properties and methods defined in the Vehicle class and introduces an additional property numDoors and a method honk().
To create objects and use these classes:
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.year = 2023;
myCar.numDoors = 4;
myCar.start();
myCar.accelerate();
myCar.honk();
myCar.stop();
}
}
Output:
Vehicle starting...
Vehicle accelerating...
Car honking...
Vehicle stopping...
This article answers the question, ‘All classes in Java are inherited from which class?’. The root class from which all classes inherit is the "Object" class. This forms the foundation of the Java class hierarchy and provides common methods and functionalities that are inherited by all other classes. Understanding class inheritance is crucial for building object-oriented applications in Java, as it allows for code reuse, modularity, and polymorphic behavior.
By leveraging inheritance, developers can create class hierarchies, establish IS-A relationships, and customize the behavior of their objects. Whether it's single-level or multilevel inheritance, the concepts of superclass and subclass play a vital role in structuring and organizing code.
So, the next time you create a class in Java, remember that it automatically inherits from the "Object" class, connecting it to the rich heritage of the Java class hierarchy.
1. How are all classes in Java related to each other?
All classes in Java are part of a class hierarchy, and they are connected through inheritance. Every class, either implicitly or explicitly, extends the java.lang.Object class, making it the ultimate superclass for all other classes in Java.
2. How does inheritance from the Object class benefit Java programmers?
Inheritance from the Object class provides Java programmers with a set of fundamental methods and functionalities that can be utilized across all classes. These include methods like toString(), equals(), hashCode(), and more. Programmers can override these methods to customize the behavior of their classes, and they can use common methods provided by Object to work with objects in a consistent manner.
3. How can I use the methods from the Object class in my custom class?
Since all classes implicitly inherit from the Object class, you can directly use methods from Object in your custom class without any additional setup. For example, you can override the toString() method to provide a meaningful string representation of your class's objects or use the equals() method to implement custom comparison logic.
4. How can I identify if an object belongs to the Object class in Java?
In Java, every object belongs to the Object class. You can use the getClass() method, inherited from Object, to get the runtime class of an object.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...