tags:

views:

324

answers:

9

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.

+12  A: 

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 ;)

Jon Skeet
And for c# at least there is boxing - as everything derives from object, everything can be stored in an object. A bit better than void*
simendsjo
@simendsjo: That's mostly covered in my first paragraph, isn't it? That's why `ArrayList` was feasible. The question is, with generics, would you still need Object for this?
Jon Skeet
@Jon Skeet: For heterogeneous collections, I suspect.
Matthias Benkard
@Jon: I came into the .Net game just recently, so I've never actually used an ArrayList :)
simendsjo
@Matthias: Hmm... possibly... I suspect there are various little corner cases like that. Can't say I use heterogeneous collections very often, but I'm sure they're handy sometimes :)
Jon Skeet
+8  A: 

The fact that every class inherits object ensured by the compiler.
Meaning that is you write:

class A {}

It will compile like:

class A : Object{}

But if you state:

class B : A {}

Object will be in the hierarchy of B but not directly - so there is still no multiple inheritance.

Itay
+1  A: 

Regarding the first part of your question, it's how classes receive common properties and methods. It's also how we can have strongly-typed parameters to functions that can accept any object.

Regarding your second question, you simply derive your class from the other class; it will then be a descendant of that class, which is in turn a descendant of Object. There's no conflict.

T.J. Crowder
+1  A: 

You have the Object base class because amongst others because the Object class has methods (like, in .NET, GetHashCode(), which contain common functionality every object should have).

Multiple inheritance is indeed not possible, but it is possible to derive class A from class B, because A may not directly derive from Object, but B does, so all classes ultimately derive from Object, if you go far enough in the class' inheritance hierarchy.

ErikHeemskerk
+2  A: 

In short

1) The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.

2) You can have B extend C, and A extend B. A is the child class of B, and B is the child class of C. Naturally, A is also a child class of C.

Cambium
+1  A: 

Duplicate. See this.

Zaki
And http://meta.stackoverflow.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers ;-)
Gorpik
I wouldnt say an exact duplicate as that question is pretty specific to Java.
Zaki
I agree with Zaki, also it doesn't state about multiple inheritance part which I asked
JPReddy
+2  A: 

Well, the multiple inheritance of Object does not apply - you can think of it as:

"If a type doesn't have a base type, then implicitly inject Object".

Thus, applying the rule ad-nauseam, all types inherit from object once and once only - since at the bottom of the hierarchy must be a type that has no base; and therefore which will implicitly inherit from Object.

As for why these languages/frameworks have this as a feature, I have a few reasons:

1) The clue's in the name 'Object Oriented'. Everything is an object, therefore everything should have 'Object' (or equivalent) at it's core otherwise the design principle would be broken from the get-go.

2) Allows the framework to provide hooks for common operations that all types should/might need to support. Such as hash-code generation, string output for debugging etc etc.

3) It means that you can avoid resorting to nasty type casts that can break stuff - like (((int *)(void*))value) - since you have a nice friendly supertype for everything

There's probably loads more than this - and in the time it's taken me to write this 6 new answers have been posted; so I'll leave it there and hope that better people than I can explain in more detail and perhaps better :)

Andras Zoltan
1) object oriented still works when not singly rooted. 2) the features could be provided by the language instead of a single root object of the library. 3) it might improve some designs _not_ to allow a "friendly super type for" _everything_
nicerobot
@nicerobot: 1) I never said that multiple inheritance violates OO. I said that a platform that pupports to be pure OO should have an Object supertype. Having more than one root supertype is entirely valid. 2) Of course they can be provided by the language - again I never said they couldn't (who cares where the functionality comes from?). In the case of .Net and Java, however, they *are* implemented by the supertype-which also means compilers have less work to do. 3) Perhaps so - but then you probably wouldn't have a pure OO environment?
Andras Zoltan
@andras-zoltan I wasn't attacking you, but just elaborating on possibilities. As for "pure OO", i was just saying that it doesn't require a common supertype to still be pure OO and that doesn't imply supporting multiple inheritance. For example, in my root-less language, `class A{}` and `class B{}` don't have a common ancestor but the language can still enforce objects as the only types. I could, for example, implement `class Number{}` and `class Integer : Number{}` which have no hierarchical relationship to `class String{}` and i'm still a purely OO environment.
nicerobot
@nicerobot: i apologise for taking offence! :) You've made your point elegantly there for sure - and I completely agree with you.On a side note - I wish I could have multiple inheritance in C#. :)
Andras Zoltan
+1  A: 

Just to compare, let's take a look at a language that doesn't enforce a single root class - Objective-C. In most Objective-C environments there will be three root classes available (Object, NSObject and NSProxy), and you can write your own root class by just not declaring a superclass. In fact Object is deprecated and only exists for legacy reasons, but it's informative to include it in this discussion. The language is duck typed, so you can declare a variable's type as "any old object" (written as id), then it doesn't even matter what root class it has.

OK, so we've got all of these base classes. In fact, even for the compiler and runtime libraries to be able to get anywhere they need some common behaviour: the root classes must all have a pointer ivar called isa that references a class definition structure. Without that pointer, the compiler doesn't know how to create an object structure, and the runtime library won't know how to find out what class an object is, what its instance variables are, what messages it responds to and so forth.

So even though Objective-C claims to have multiple root classes, in fact there's some behaviour that all objects must implement. So in all but name, there's really a common primitive superclass, albeit one with less API than java.lang.Object.

N.B. as it happens both NSObject and NSProxy do provide a rich API similar to java.lang.Object, via a protocol (like a Java interface). Most API that claims to deal with the id type (remember, that's the "any old object" type) will actually assume it responds to messages in the protocol. By the time you actually need to use an object, rather than just create it with a compiler, it turns out to be useful to fold all of this common behaviour like equality, hashing, string descriptions etc. into the root class.

Graham Lee
A: 

Well multiple inheritance is a totally different ball game.

An example of multiple inheritance:-

class Root
{
public abstract void Test();
}

class leftChild : Root
{
public override void Test()
{
}
}

class rightChild : Root
{
public override void Test()
{
}
}

class leafChild : rightChild, leftChild
{
}

The problem here being leafChild inherits Test of rightChild and leftChild. So a case of conflicting methods. This is called a diamond problem.

But when you use the object as super class the hierarchy goes like this:-

class Object
    {
        public abstract void hashcode();
        //other methods
     }

    class leftChild : Object
    {
    public override void hashcode()
    {
    }
    }

    class rightChild : Object
    {
    public override void hashcode()
    {
    }
    }

So here we derive both classes from Object but that's the end of it.

A_Var