Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to have Object class as baseclass for all the classes?

Tags:

java

c#

oop

Either in C# or Java or in any other language which follows oops concepts generally has 'Object' as super class for it by default. Why do we need to have Object as base class for all the classes we create?

When multiple inheritance is not possible in a language such as C# or Java how can we derive our class from another class when it is already derived from Object class. This question may look like silly but wanted to know some experts opinions on it.

like image 710
JPReddy Avatar asked Jun 24 '10 09:06

JPReddy


1 Answers

Having a single-rooted type hierarchy can be handy in various ways. In particular, before generics came along, it was the only way that something like ArrayList would work. With generics, there's significantly less advantage to it - although it could still be useful in some situations, I suspect. EDIT: As an example, LINQ to XML's construction model is very "loose" in terms of being specified via object... but it works really well.

As for deriving from different classes - you derive directly from one class, but that will in turn derive indirectly from another one, and so on up to Object.

Note that the things which "all objects have in common" such as hash code, equality and monitors count as another design decision which I would question the wisdom of. Without a single rooted hierarchy these design decisions possibly wouldn't have been made the same way ;)

like image 176
Jon Skeet Avatar answered Sep 25 '22 16:09

Jon Skeet