Why should every class in java extend Object class?

Showing Answers 1 - 10 of 10 Answers

new user

  • Apr 20th, 2006
 

Object class is the super/base/parent class of every class(including user-defined classes).

So even if u don,t mention it, the user-defined classes extends Object class by default

  Was this answer useful?  Yes

ravi

  • Apr 24th, 2006
 

In java u can access the data through objects.Here Object class is a super/base/parent class for all the java classes,so it will be hold all class objects of java.And also whenever u can serialize the object into the Stream we must store as form of ObjectStream object only and at the time of Deserialize the object from the Streams, u must get it as form of Object class object only.After that u must casting the object to u r Knowing class.

example:  here file.txt is a file. And we can write the object into it,and read the object from it.

//write the object into the file,txt,and our class name is MyClass it object is "mco"

FileOutputStream fis=new FileOutputStream("file.txt");

ObjectOutpurStream oss=new ObjectOutputStream(fis);

os.writeObject(mco);

//read the object from the file.txt

FileIutputStream fos=new FileIutputStream(file.txt);

ObjectInputStream ois=new ObjectInputStream(file.txt);

MyClass myObj=(MyClass)ois;//here ois return the Object Class object only that's way we must the casting.

  Was this answer useful?  Yes

premisa

  • Apr 28th, 2006
 

Hi

In order to reduce the excution time of a java program,we can make use of Constructor method to create an object which will extend to the given java class.

Hence  by using object class we can acceess the java program, while the class has the number of repeated function.

Thus Instead of calling the function/expreesion evertime bymake use of extending object class we can reduce the time consumption of the execution of the program quickly.

  Was this answer useful?  Yes

Sandhya

  • May 26th, 2006
 

Hey Guys,

Object class implements a set of methods and variables which are common to all the objects being created in the application. This is the main reason why we have Object class as a base class for all the other classes.

Some of the methods are:

hashCode() - this method creates a unique identity (or number) for each of the object being created in JVM.

  Was this answer useful?  Yes

sushant

  • Nov 17th, 2012
 

If you see the definition of class Object, it is defined as "Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class."

Now the question is "why every class in java extend Object class? "

Its because the class Object implements all the basic methods java compiler/runtime may be calling.

Example:
Assume that you have a class X which does not extends class Object, and you have not implemented finalize() method.
Every time garbage collector tries to collect any instance of X...and ties to call finalize()...bad things happens!
Which will not happen otherwise :-)

  Was this answer useful?  Yes

Reddy

  • Feb 4th, 2017
 

Object class provides some common behaviors to all the objects
Cloning
Serialization
Garbage Collection etc.,

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions