0

Since every class is derived from the system.object,does the derived class objects refer to the classes in their hierarchy? (classes above them only of course)

for e.g

public class A { //contains some attributes and methods } public class B: A { //contains some attributes and methods } public class C: B { //contains some attributes and methods } 

Now does the OBJECT OF CLASS "C" refer to A,B as well as the system.object class?

6
  • 1
    What do you mean by "refer to"? Object of class C will inherit from class A as well as B. Commented Feb 16, 2012 at 16:36
  • see when an object of class c created; then first the object of class system.object is created(as its the highest leveled base class) and a refrence to that object must be send to SOMEWHERE (where does this refrence go??), now the ooject of class A is created, a refrecne to it must be send where somewhere, then following the heirarchy the object of class B created address of this object must be sent finally C class object created, my question here is does all these refrences stored in the object of C?? (reference of all the classes from which C is derived) Commented Feb 16, 2012 at 17:52
  • 1
    No.. there's only one instance, and it's of class C. That object "is" a "C" - and it's therefore also a B, an A, and a System.Object - because that's part of what it means to be a C. Commented Feb 16, 2012 at 18:15
  • To put it another way, the Class C refers to Class B which refers to Class A which refers to System.Object. So when you create an instance of class C, the instance just needs to "refer" to Class C. Commented Feb 16, 2012 at 18:16
  • @SanaZehra: If you have a second question, open another question. But, briefly: yes; the base class constructors are called in order from base to derived. (But field initializers are run in order from derived to base.) Commented Feb 17, 2012 at 0:48

5 Answers 5

6

It took me a while but I figured it out. You believe that C# is a "prototype inheritance" language. It is not.

In a prototype inheritance language, like JavaScript, every object has a reference to its "prototype object". So in JavaScript you might say:

function Dog() {} Dog.prototype.legs = 4; var rover = new Dog(); var spot = new Dog(); spot.legs = 3; // Poor spot! print(rover.legs); // 4 print(spot.legs); // 3 

rover and spot both have references to Dog.prototype. When you ask rover "how many legs do you have?" rover says "I don't know; let me ask my prototype". The object referred to by rover itself has a reference to the object referred to by Dog.prototype, and that thing has a legs property.

When you ask spot "how many legs do you have?", spot also has a reference to its prototype, but it doesn't need it, because spot already knows how many legs it has.

With a little more work we could build a system where the prototype of Dog.prototype is the "Animal prototype object". And then the chain would continue; rover would have a reference to Dog.prototype, Dog.prototype would have a reference to Animal.prototype, and Animal.prototype would have a reference to Object.prototype.

C# does not work like that at all. In JavaScript, an object is part of a linked list of prototypes and the prototype chain is searched when a property needs to be looked up. In C#, all the information for each property is stored in every instance. In C# an object does not refer to an instance of its base types and does not contain an instance of its base types; rather, it inherits all of the members of its base types. (Other than constructors and destructors.)

Sign up to request clarification or add additional context in comments.

3 Comments

ok thanks a lot. ur explainations helped me in finding answer to my question!
@ErricLipert but still constructors are inherited right? .. as in if i do not provide a zero parameterized constructor in derived class ... base class constructor is called.
@SanaZehra: Constructors are not inherited. If you fail to provide a constructor you get for free a constructor that calls the base class constructor, if there is one. You do not inherit the base class constructor; you call it.
1

From reading the comments, your misunderstanding seems to be that when you create an instance of class C, there are also separate instances of classes B, A, or Object. That's not the case. There is a single object that is an instance of class C, and it may also be treated as an instance of class B, A, or Object (read up on the Liskov Substitution Principle). To modify Eric Lippert's analogy: when a dog is created (born), it is a dog, a mammal, and an animal. But there's only one dog.

Consider this (Animal, Mammal, Dog, instead of A, B, C):

public class Animal { void Breathe(); } public class Mammal : Animal { void GrowHair(); } public class Dog { void Bark(); } 

This inheritance chain gives the following effect:

public class C { void Breathe(); void GrowHair(); void Bark(); } 

In terms of fields, and going back to A, B, C, consider this:

public class A { private int _a; } public class B: A { private int _b; } public class C: B { private int _c; } 

How does an instance of A look in memory? Some number of bytes of overhead, plus four bytes for _a:

OVERHEAD _a : four bytes 

How does an instance of B look in memory? It looks just like an instance of A plus four more bytes:

OVERHEAD _a : four bytes _b : four bytes 

And an instance of C looks like an instance of A plus eight more bytes:

OVERHEAD _a : four bytes _b : four bytes _c : four bytes 

What's that overhead? Not surprisingly, that's the stuff defined by the Object class! So every time you add another class to the inheritance chain, the fields defined in that class are just tacked onto the end of the memory structure of its parent class.

An instance of a derived class does not refer to the data defined by its parent class; it contains the data defined by its parent class.

3 Comments

thank you so muchhhh!!! =) thats what i wanted to know. also when i do not mention any default (zero parameterized) constructor in class "C" then are the default constructors of A,B as well as SYSTEM.OBJECT called when i create an object of "C"? –
@SanaZehra yes; if you don't specifically call another base class constructor, the base class's default constructor is called.
@SanaZehra you're most welcome. Please upvote answers that you find helpful, and accept the most helpful answer.
0

An object of type C would also be of type B, A, and System.Object.

It will have all the members (public or protected methods, properties, and fields) of the other classes it inherits from. This is why all objects have a "ToString" method - they inherit it from System.Object.

9 Comments

but does that derived object refer to these classes b,a and system.object? ...i mean does a C class object contains reference to all of these classes in memory .. ? ..
@SanaZehra: I think people are confused by your question because it is completely unclear what you mean by "refer to". Suppose you have a bowl of rice. Now I ask you "does your bowl of rice refer to container? And does the rice refer to food?" You would be perfectly justified in asking me to clarify that question. Suppose I asked you "does the phrase 'the White House' refer to the same house as '1600 Pennslyvania Avenue, Washington DC'? That is a much more clear question. It is not at all clear what you mean by "refer to"; try using "refer to" in the "White House" sense.
It's not stored as 4 separate objects in memory, it's stored as one object (who's definition is a combo of the members of all 4 objects). A Golden Retriever is a Dog, and is also an Animal. When I say "Golden Retriever" I'm not referring to animal, I'm referring to a specific TYPE of animal.
see when an object of class c created; then first the object of class system.object is created(as its the highest leveled base class) and a refrence to that object must be send to SOMEWHERE (where does this refrence go??), now the ooject of class A is created, a refrecne to it must be send where somewhere, then following the heirarchy the object of class B created address of this object must be sent finally C class object created, my question here is does all these refrences stored in the object of C?? (reference of all the classes from which C is derived)
@SanaZehra: The White House certainly does not contain a house. It is a house. The White House is a house, it contains the Oval Office, and "1600 Pennsylvania Avenue" refers to the same house that "The White House" refers to. You're getting "is", "refers to" and "contains" all mixed up; they are very different.
|
0

Yes. All classes refer to the system.object class implicitly by default.

1 Comment

but does that derived object refer to these classes b,a and system.object? ...i mean does a C class object contains reference to all of these classes in memory .. ? ..
0

Not exactly, but it's more semantics than anything else.

An object of class C refers to an instance of the class definition named C. However, it may be type cast into objects of type A, B or just plain old System.Object.

There is no "reference" between C and A/B/System.Object. It's more of an amalgamation of all of those class definitions into a unified "C".

What this means is that you can do the following:

function void DoSomething(A input) { // do something as A } C myObj = new C(); DoSomething(myObj); 

In the above case, C will be type cast to an object of type A in the DoSomething call.

You can also do things like:

public class A { public String MyValue { get; set; } } public class B : A{ public String AnotherValue { get; set; } } public class C : B { public void DoSomething() { MyValue = "Hello"; AnotherValue = "World"; } public String Output() { return String.Format("{0} {1}", MyValue, AnotherValue); } } C myObj = new C(); C.DoSomething(); String message = C.Output(); // value of message will be "Hello World" 

Now the reverse is not true. You cannot do the following:

function void DoSomething(C input) { // do something as C } A myObj = new A(); DoSomething(myObj); 

In this case you would have a compiler error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.