Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,
I read article on Mehtod overiding posted in the CodeProject .In the article it is stated:" function is invoked based on type of the reference and not to what the reference variable X refers to"..If it si so what is the use of creating object and asigning it's reference to the variable in the example provided in the article,For quick reference here is the example:

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();

     b = new DC();------here it is..
     b.Display();
  }
}


Can anyone please comment on this..


Regards
Chaithanya M
Posted
Updated 21-Apr-11 23:48pm
v3

The new keyword hides the base method, it doesn't make it virtual.

In your example, "BC::Display" will be displayed because b was declared as BC.

If you want polymorphism to work, change your code like that:
C#
class BC
{
  virtual public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}
 
class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}


M.CHAITHANYA wrote:
you didnt get my question i guess,my question is what actually happens internally when you make a method virtual..how does the complier treat it?


If a method is marked as virtual, at the time of the call, the compiler will check the object's type (its real type, not the type of the variable making the call), and will call the most derived implementation of that method from that real type.
Check these pages to understand how virtual methods work:
http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.71).aspx[^]
http://msdn.microsoft.com/en-us/library/aa645767(v=vs.71).aspx[^]
 
Share this answer
 
v4
Comments
M.CHAITHANYA 22-Apr-11 5:59am    
Hi Olivier,

What actually is making Virtual?

Regards
Chaithnaya M
Olivier Levrey 22-Apr-11 6:02am    
The virtual keyword. Checkout Display full definition in BC class.
M.CHAITHANYA 22-Apr-11 6:06am    
HI ,
I am sorry..you didnt get my question i guess,my question is what actually happens internally
when you make a method virtual..how does the complier treat it?
Olivier Levrey 22-Apr-11 6:13am    
Oh I see. I updated my answer.
M.CHAITHANYA 22-Apr-11 6:43am    
Hi Olivier,

I know Polymorphism which is same name but differnt signatures right!!. Can you please tell me what is Polymorphic behaviour with an example.


Regards
Chaithanya M
When you call the Display the second time on b, the display method of DC (Derived class) gets called. This is called polymorphic behavior and is one of the enablers of the open close design principle.

For example it is now possible to do this.

C#
List<bc> myList = new List<bc>();
myList.Add(new BC());
myList.Add(new DC());
myList.Add(new DC());
myList.Add(new BC());

for each (BC bc in myList)
{
  bc.Display();
} 
</bc></bc>


The drawing routine itself (the for each) doesn't have to change anymore. Each type that derives from BC implements its own dDisplay routing. Therefore we say that this is open for extension and closed for modification, you could add new derived classes without changing the drawing routine.

Hope this helps in your understanding.
 
Share this answer
 
Comments
Olivier Levrey 22-Apr-11 6:04am    
"When you call the Display the second time on b, the display method of DC (Derived class) gets called". This is not correct. Read OP code carefully: he didn't use virtual, nor override, but new, hence there is no polymorphism there.
M.CHAITHANYA 22-Apr-11 6:14am    
yahh.that's correct ..Olivier
Patrick Kalkman 22-Apr-11 6:24am    
You're correct, I missed it. :-(
Olivier Levrey 22-Apr-11 6:28am    
That's OK, it happens to everyone ;)

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