Why Object is the Super Class of All Classes in Java ?

(2598 Views)


Why Object is the Super Class of All Classes in Java

The Object class is the base class for all Classes in Java Language and we all are reading the below sentences from day one of our Java Programming.

  1. Every class is a Child of the Object class.
  2. Every class in Java extends Object class.
  3. Object class is the super class of every Java Class.
  4. Every Class is sub-class of Object Class

We all have read the above lines and stopped there without trying to find the reason. Only few of them have a return question about the reason behind making the Object class as a super class. In fact, there is no secret reason behind making Object as a super class. The only reason is Inheritance. Yes, The same key feature of OOP, inheritance theory applies here too. It is just to reduce the code duplication.

If you observe the methods in Object class, contains the basic methods that every class should contain and they are very basic features or functionalities that every class should have and support (like getting Class name, String representation of Object etc. ).

Object class provides the basic functionality methods which are already implemented in it. Now, its upto the programmer if he/she wants to provide implementation for those methods or not. If programmer wants to provide implementation to Object class method, he/she only needs to override those methods in their Class. Otherwise the default implementation in Object class will be executed.

Let's see some of the methods of Object class:

For string representation of Class :
toString();
For copy of an object :
clone();
For checking equality of an object :
equals();
To support hash techniques on object :
hashCode();
To support garbage collection :
finalize();
To give the Class name of object :
getClass();
To support syncronization on object :
public final void notify() public final void notifyAll() public final void wait() public final void wait() public final void wait()

If you see, all the methods mentioned above is a part of basic feature for every class and Java designers already implemented that for you. Otherwise every developer needs to write these implementation methods on their own and end of the day, there will be no time to write the business logic. And also each person will implement code in their own style and there will be no uniqueness either. So designers separated these basic functions and made an Object Class and made sure that all classes will extend Object class implicitly.

Other than these reasons, there are many other reasons too which java designers kept in mind while making it. For Ex, to make generic functions with Object as a parameter in methods, then to provide default implementations as well, since every Class in Java Is-A Object.

public final void writeObject(Object obj)

Look at a function written above, as its parameter type is Object you can pass any instance to it, since it accept all types of Objects. But by accepting Object type parameter we are giving open invitation to other problems too. Due to this, Java has added generics from Java version 1.5

If you want to add some points or want to correct me, you can surely do it in the comment section
        

Solution Didn't Worked 1 DownvotesDownvote



Comments



Search
Play 2048 Game Online

Search Tags