Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Please take look at the following code:

C#
public class Panda
{
public string Name;
public override string ToString() { return Name; }
}
...
Panda p = new Panda { Name = "Petey" };
Console.WriteLine (p); // Petey


as i know override methods only work when reference of type superclass points to object of type subclass.Also i know that ToString method is member of System.Object class.

I want you to describe this code for me(boxing,override scenarios).

thank you
Posted
Updated 11-Aug-14 9:06am
v2

1 solution

Your overriding is just fine. Your concern is about the very hard of OOP, which you should understand well, otherwise you won't be able to proceed correctly with any kind of .NET works.
First, you need to see the difference between compile-time types and runtime types.
C#
public class Mammal {
   public override string ToString() { return /* ... */; } // does not have to be overridden, just for example
}

public class Panda: Mammal {
   public override string ToString() { return /* ... */; } // something else
}

//...

Panda panda = new Panda(); // panda's runtime type is the same as design-time type

Mammal someMammal = new Panda(); // someMammal has the compile-time type Mammal,
// this is how the type is seen by a compiler;
// it won't access the names specific to Panda directly
// but the object's runtime type is Panda

string someString = someMammal.ToString(); // which ToString() will be called?

The answer to the question in the above comment: the call to ToString() will be dispatched to Panda.ToString(), despite the fact the variable type is Mammal. If the method wasn't virtual, the method of the variable compile-time would be called, the one of Mammal. This is the whole point of OOP: virtual methods are dispatch dynamically to the ones of the actual run-time type; for non-virtual, the method according to the compile-time type is called. Virtual methods are dispatches dynamically (they are late-bound), non-virtual — statically.

[EDIT]

See also:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^],
http://en.wikipedia.org/wiki/Virtual_function[^].

[EDIT]

Please see my other past answer:
http://www.codeproject.com/Answers/597424/wherepluscanplusIplusfindplusinformationplusaboutp#answer1

—SA
 
Share this answer
 
v5
Comments
Amirreza Hashemieh 11-Aug-14 15:54pm    
Thank you man.I know the concepts of virtual and override but i would like to know that when the runtime wants to run my class,why it calls override method? i didn't create reference of type System.Object to point to my class,now why override works there?
Sergey Alexandrovich Kryukov 11-Aug-14 16:07pm    
Your phrase "as i know override methods" suggested that you didn't clearly understand it, but maybe it could be just bad phrasing. Unfortunately, your question I'm answering right now is a clear evidence that you just don't know the idea at all. Not even close. It's better to admit it, and not to fool yourself, to be able to learn things and go forward.

The runtime calls the overridden method due to dynamic dispatch mechanism through indirection provided by the virtual method table. When you create derived class, the entries in the table are replaced with a reference to overridden method (during compile time). And the call is indirect: instead of calling a method, implementation looks at "this" reference (do you know how "this works?), by this reference, finds run-time reference to VMT, and finds correct method address.

This is explained here:
http://en.wikipedia.org/wiki/Dynamic_dispatch,
http://en.wikipedia.org/wiki/Virtual_method_table.

Now, will you accept my answer formally (green "Accept" button)? Of course, your follow-up questions will be welcome anyway.

—SA
Amirreza Hashemieh 11-Aug-14 16:23pm    
i want to know that than when runtime(jit) wants to create object from my class? does it use object a=new Panda() so override here works in this scenario. Is this true or not?
Sergey Alexandrovich Kryukov 11-Aug-14 16:55pm    
An object of your class is created when the constructor is called. If you write
object a=new Panda(), the object of the runtime-type Panda is created, but the reference to it is assigned to the variable of type object (or Mammal, whatever variable or member the compiler initialize).
You should also understand that in such cases (when you have a class), two objects are created 1) object itself, 2) reference to is (managed pointer). Usually, you don't have distinction, but often you do:

object[] animals = new object[] { new Panda(), new Panda(), new Giraffe, new Aadvark, };
You are given just one reference to array type. What happens if you write animals[0] = null?
First panda still exist, but you loose reference to it. What happens later? If you have some other references to the same object (for example you had object firstPanda = animals[0] and keep this reference), the object will continue to live. If you don't have other reference, GC will eventually detect the situation and will destroy this panda instance. GC will work this way even if you have N objects referencing each other in circular manner, but no other references to them.

Note that GC is specific only to managed platforms, line CLR or JVM. Other system will require you to call destructors.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900