Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the essencial advantage of Object Class  RSS feed

 
ramesh kothakonda
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi java ranchers,

I know Object is the super class of all classes. All classes having this class methods. Is there any extra advantage other than that.

if you give answers I will be very thank full to you
thanking you
K.ramesh.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The word "advantage" implies that one choice is better than another choice. I don't understand from your question what the choices are. For Object to have an "advantage", it must have an advantage compared to something else: what is that something else that you're asking about?
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the question should be �what is the need of Object class in Java?�.

Well, Java you required some common behaviors and single identity to all object created by you. To fulfill this requirement, they have made Object class.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I think Ramesh is asking about "Why every java class need to be inherited from java.lang.Object".

Well, Its pretty cool.Lets put in this way. If you want all your development team to get some operations in default which are essential and required, then what shall you do? ... thats what the Object will do. It contains the methods which are essential for any java program or class you code, and they will provide the implementations through the native methods.

For more informatin you can see the java source code for java.lang.Object.

More inputs are welcome.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you notice then you will find that all the methods which are listed in java.lang.Object are required by all the object running under the jvm.

for example: finalize - is applicalble to all objects.
wait , notify , notify - all objects have lock , so this methods are also part of all objects.
hashcode - unique number , calculated from the memory location of this in the heap.
equals - all objects consume some physical memory in the heap , so there should be some way to compare those.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no advantage to java.lang.Object - there is only disadvantage. Unfortunately it is not entirely escapable, but so long as you never use collections or the equals/hashCode/toString methods, you will not suffer too much. Certainly, I practice this when I use Java (which is not too often these days thankfully). Unfortunately, lack of critical analysis skills on the part of many software developers results in an oversight when the use of java.lang.Object imposes some adverse consequence, where typically, that consequence is attributed elsewhere in order to legitimise the selected context. This, and many more irrational behaviours, have compelled me to engage in lobbying universities to change the way they teach (and since I am a university teacher myself, I practice it where possible).

Sun admitted that a common supertype was a mistake by introducing generics - there is no longer a need for a common supertype with parameterised types - despite how screwy they are.

I encourage you to think a little bit more about your question if the right answer is not the one that your teacher wants to hear (as is often the case). I'll be happy to answer any questions you might have.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having a single ancestor is a language designer's choice. From a quick Google, it looks as though Smalltalk, Java and Delphi Pascal do, but C++, Objective-C, and Apple Object Pascal don't.

Jave designers seem to have decided that having a place to put common behavior was worth the problems. Plenty of Java choices favored practicality, ease of use, size of implementation, convenenience, etc. over beauty and purity. I can usually put up with that long enough to get paid.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!