Tuesday 5 May 2015

What is overriding methods:-


Hey guys,
This blog we explain what is overriding and  how we use it.
There may be  occasions when we want an object to response to the same method but behave differently when that method is called. That means we should override the method defined in the superclass.  This is possible by defining a method in the subclass that has the same name, same arguments and same return type as a method in the superclass.
Creating a method with derived class with same signature as a method in base class is called as method overriding. Here same signature means methods must have same name , same number of arguments and same type of arguments. It is possible with derived class but not within the same class.
Example of overriding:-
using System;

namespace methodoverriding
{
    
class A
    {
        
public virtual  string City()
        {
            
return "New York";
        }
    }

    
class BA    {
        
public override string City()
        {
            
return "London";
        }
    }

    
class Program
    {
       
        
static void Main(string[] args)
        {
            
B obj = new B();
            
string city = obj. City();
            
Console.WriteLine(city);
            
Console.Read();
        }
    }
}

No comments:

Post a Comment