0

What is the function of the class Object in java? All the "objects" of any user defined class have the same function as the aforementioned class .So why did the creators of java create this class? In which situations should one use the class 'Object'?

15
  • 5
    What class do you think should be the superclass of all other classes? Commented Aug 22, 2013 at 17:42
  • 1
    Are you asking why every type is a subtype of Object, or why it's concrete rather than abstract or an interface? Or perhaps something else entirely? Commented Aug 22, 2013 at 17:42
  • 2
    Where do you think all user created objects get those methods? they inherit them because they are subclasses of Object. Commented Aug 22, 2013 at 17:44
  • 5
    There are literally thousands of hits on Google answering this exact question. This is java 101 stuff. Commented Aug 22, 2013 at 17:44
  • 1
    @redFIVE I've seen quite a few questions on this forum that have many hits on google . Yet people have answered them and given exceedingly better explanations than those google links.Surprisingly, they have got many upvotes too! Commented Aug 22, 2013 at 17:50

4 Answers 4

3

Since all classes in Java are obligated to derive (directly or indirectly) from Object, it allows for a default implementation for a number of behaviours that are needed or useful for all objects (e.g. conversion to a string, or a hash generation function).

Furthermore, having all objects in the system with a common lineage allows one to work with objects in a general sense. This is very useful for developing all sorts of general applications and utilities. For example, you can build a general purpose cache utility that works with any possible object, without requiring users to implement a special interface.

Sign up to request clarification or add additional context in comments.

Comments

2

Pretty much the only time that Object is used raw is when it's used as a lock object (as in Object foo = new Object(); synchronized(foo){...}. The ability to use an object as the subject of a synchronized block is built in to Object, and there's no point to using anything more heavyweight there.

Comments

1

Object provides an interface with functionality that the Java language designers felt all Java objects should provide. You can use Object when you don't know the subtype of a class, and just want to treat it in a generic manner. This was especially important before the Java language had generics support.

There's an interesting post on programmers.stackexchange.com about why this choice was made for .NET, and those decisions most likely hold relevance for the Java language.

Comments

1

What Java implements is sometimes called a "cosmic hierarchy". It means that all classes in Java share a common root.

This has merit by itself, for use in "generic" containers. Without templates or language supported generics these would be harder to implement.

It also provides some basic behaviour that all classes automatically share, like the toString method.

Having this common super class was back in 1996 seen as a bit of a novelty and cool thing, that helped Java get popular (although there were proponents for this cosmic hierarchy as well).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.