Sana Zehra
Sana Zehra

Reputation: 25

In what way does the newly created object of a certain class refer to classes from which it is derived?

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?

Upvotes: 0

Views: 279

Answers (5)

Eric Lippert
Eric Lippert

Reputation: 660038

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

Upvotes: 6

phoog
phoog

Reputation: 43046

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.

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88064

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.

Upvotes: 0

Kalaji
Kalaji

Reputation: 425

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

Upvotes: 0

Arbiter
Arbiter

Reputation: 1024

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.

Upvotes: 0

Related Questions