0

I have researched a little bit about the Object class but don't have an explicit answer to my questions (mostly documentations on the class members).

What is the benefit of having an "object" class at the root of the class hierarchy and basically why does a class has such an Object?

My guess is, because java is a strongly object oriented programming language and having an "object" at the root would be ideal to this concept. Doesn't coupling increase every time we inherit further from the root?

localplutonium
  • 260
  • 3
  • 13
  • 1
    The benefits? You get a language that actually works the way it's intended to work. inheritance, not having to put all the basics in every single class, stuff like that. – Stultuske Mar 15 '16 at 07:07
  • http://stackoverflow.com/questions/11844012/they-say-in-java-every-thing-is-an-object-is-that-true – Pillz Mar 15 '16 at 07:12
  • @aBottleOfPills this thread doesn't explicitly describe what and why object is at the root or how it benefits coding in java in general. it just explains that not everything is an object or that you cant create functions outside of a class which i already understand. im looking for a more specific answer to why java is created this way / what are the essential benefits of creating a language this way. – localplutonium Mar 15 '16 at 07:19
  • All the containers were originally designed to take Objects for input. With every class deriving from Object, any class could be added to any container. It only required the Object to be cast back to the actual class when it was removed from the container. – Scooter Jul 21 '16 at 14:34

2 Answers2

2

Well the benefit is, that everything (except primitives) are an Object. So there are certain things you can do with every Object, like synchronizing on it, or comparing two of them for equality or converting one to a String.

Of course this could just work by some kind of build in language feature. But in OO there is already a feature for that: inheritance, so it makes the language simpler by using this concept.

Of course one can have lengthy discussions, about each and every method of Object, if it was a good idea to include it.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

So that all objects can inherit the basic methods from the main Objects class and you have the option to override them. Ex. toString();

Eli Per
  • 1
  • 1