Sunday 4 January 2015

C# Override

Same method name and same signature.
If you want to override any function, you have to add virtual key word, and while overriding, you have to use override key word.
Ex:

public class Vehicles
{
    public virtual void Greet()
    {
        Console.WriteLine("Hello, I'm some sort of vehicles!");
    }
}

public class Car: Vehicles
{
    public override void Greet()
    {
        Console.WriteLine("Hello, I'm a car!");
    }
}

In the above example, Car class has inherited Vehicles.
Like this we can inherit hierarchy wise. ex:
public class Accord: Car
{
    public override void Greet()
    {
        Console.WriteLine("Hello, I'm a Accord!");
    }
}

Accord class is inheriting Car..like this we can add more..

But we can't do multiple inheritance in C#
Ex: Accord class can't inherit Car and Vehicles.