Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
what is use of runtime binding? i mean not it's definition, it's practical advantage

What I have tried:

C#
public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Base d = new Derived();
d.SomeMethod();

it will call derive class method. but what is need of it. if i can do it by creating derivd class instance. and declaring base class method as sealed.

i can code like this

C#
public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Derved d = new Derived();
d.SomeMethod();

it will called base class as well as derived class methods.
if i declared it as sealed then it will be ok...

C#
public class Base
{
  public virtual void SomeMethod()
  {
  }
}

public class Derived : Base
{
  public override void SomeMethod()
  {
  }
}

...

Derived d = new Derived();
d.SomeMethod();  //it will called derived method

base b=new base();
b.somemethod(); //it will call base method




then what is need of virtual ? for runtimebinding ? if yes then what is use of rutime binding or what is practical advantage of run time binding?
Posted
Updated 13-Mar-17 2:47am
v2

Quote:
i can do it by creating derivd class instance. and declaring base class method as sealed.

If you do that, you cannot create a derived class - a sealed class is one that specifically cannot be used as a base, it cannot be inherited from.
Quote:
it will called base class as well as derived class methods.

No, it won't: Because you have overridden the base class method, it will call the "latest" method.
And if you do this:
C#
Derived d = new Derived();
d.SomeMethod();
Base b = new Base();
b.SomeMethod();
b = d;
b.SomeMethod();
It will call the Derived method, then the Base method, then the Derived method.

The virtual keyword tells the compiler that a derived class can override the method. If you omit it, you can't use override in the derived class method definition.
 
Share this answer
 
Comments
Kishor-KW 2-Dec-16 6:02am    
"i can do it by creating derivd class instance. and declaring base class method as sealed."
i am saying method to be a sealed not a class.

Derived d = new Derived();
d.SomeMethod();
Base b = new Base();
b.SomeMethod();

"it will call the Derived method, then the Base method, then what is need of virtual keyword here.

"The virtual keyword tells the compiler that a derived class can override the method. If you omit it, you can't use override in the derived class method definition."

what if i did not used any of this? and what are advantages of using it?

Its best use case is for abstraction - you don't need to know what the derived class is to get the correct method. A classic example I like to use is that of a Vehicle base class, with an abstract (which is just virtual that is not defined in the base class) of int GetWheels(). A bike, derived from vehicle, has 2 wheels. A car has 4, and a van has 6. The tyre shop books in a vehicle, and needs to know how many tyres. It can just call GetWheels on the vehicle, and know how many tyres will be needed without knowing, as yet, what sort of vehicle it is.
 
Share this answer
 

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