Difference between override-virtual and new components

The override modifier is used to extend the functionality of an abstract or virtual method. To easy understand this consider the following example.

In this example the base class is abstract along with two methods one is abstract and the other is virtual. When we use inherit from this base class we are bound to override the ‘Act’ method which is tagged as abstract. But we really don’t have to modify the functionality of the virtual method. But if we want to modify the functionality of the virtual method we just either implement it or use a ‘new’ keyword to implement the new functionality.

// override
	public abstract class AbstractBaseClass
	{
		public abstract void Act();
 
		public virtual void DoNotAct()
		{ }
	}
 
	public class AbstractSubClass : AbstractBaseClass
	{
		public override void Act()
		{ }
 
		public new  void DoNotAct()
		{ }
	}

 

If you use conventional inheritance and still want to override to modify the behavior of existing base class method you can do so by using the New keyword consider the following example.

//new 
	public class BaseClass
	{
		public void Hello()
		{
			//I am base class
		}
	}
 
	public class DerivedClass : BaseClass
	{
		public new void Hello()
		{
			//I am derived class
		}
 
	}