• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advantages of class Object  RSS feed

 
Lionel Rossi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I kindly ask you this simply question, in Java every class inherits directly or not directly for the object class. I think because in Java everything is an object and for give to the programmer some guide line for the classes that ll be create and also give some important methods that can be used by other classes (for example toString ecc...). There are other reason and other advantage about this thing?

Best Regards

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly because almost every object benefits from having the equals(), hashCode() and toString() methods. It also makes sense, because every object (no matter what their runtime type is) IS-AN Object.

For more detailed reasons, you should probably ask the creators of the language.
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same/similar question and asked to Joshua Bloch.
Following is the answer:
https://www.youtube.com/watch?v=EduWekviwRg
Go to 50:00 th minute on time bar of video.

  • Object makes it convenient to implement collections without worrying about exact type of the class.
  • hashcode and equals useful for comparison of objects.
  • wait() and notify() could be moved out of Object.
  •  
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lionel Rossi wrote:... in Java everything is an object

    This isn't true - Java has primitive types (byte, short, int, long, float, double, char, boolean) which are not objects.

    Also, it's not necessary for an OO programming language to have one top-level class such as java.lang.Object. For example in C++ there is no top-level class.

    Lionel Rossi wrote:... give some important methods that can be used by other classes (for example toString ecc...).

    I think that this is the main reason that in Java we have class Object - so that there can be a number of common methods that exist for all objects (hashCode, equals, toString etc.).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!