MorkaLork Development

Interesting stuff I've picked up over the years...

Override

2009-04-17 07:20:57 | 193 views | csharp class method tutorial overwrite rewrite keyword virtual

The basics



In C# you can rewrite existing methods to create more class specific methods. This is called overriding and can be done to all inherited methods that contains the keyword virtual in the method header.
Before reading this article you should have read the main article concerning inheritance.


Getting into it



We will continue with the Vehicle, Car and Bicycle class from the previous article (Inheritance) and focus mainly on Vehicle and Car.

We want to create a method to output the current information about the object at hand. First, we create such a method in our Vehicle class:


public virtual void Description()
{
Console.WriteLine("Weight: {0}", Weight);
Console.WriteLine("Owner: {0}", Owner);
}


As we can see, we use the keyword virtual to create a overrideable method. This method will output the Vehicle properties weight and owner.
Now, we want a similar method in Car as well, so we create one there.


public void Description()
{
Console.WriteLine("Weight: {0}", Weight);
Console.WriteLine("Owner: {0}", Owner);
Console.WriteLine("Brand: {0}", Brand);
Console.WriteLine("Years since last service: {0}", YearsSinceLastService);
}


We now use the inherited members Weight and Owner from Vehicle. This can, however, be more neatly coded by using the override technique:


public override void Description()
{
base.Description();
Console.WriteLine("Brand: {0}", Brand);
Console.WriteLine("Years since last service: {0}", YearsSinceLastService);
}


Now we have overridden the Description() method by using the keyword override. both Visual Studio and SharpDevelop will automatically add the line base.Description(). This line fetches all the code in the original method, then continues with the code below. Code can be added before this line, but then that code will be executed before the base method code.

This method can, in turn, be overriden by another class that inherits car. If you don't want it to be overrideable then add the keyword sealed in the method header, like this:


public sealed override void Description()


Now this method cannot be overridden.

The Full Code



Vehicle.cs





using System;

namespace TestApp
{

public class Vehicle
{
private int weight;
private string owner;

public int Weight
{
get{return weight;}
set{weight = value;}
}
public string Owner
{
get{return owner;}
set{owner = value;}
}

public virtual void Description()
{
Console.WriteLine("Weight: {0}", Weight);
Console.WriteLine("Owner: {0}", Owner);
}
}
}


Car.cs





using System;

namespace TestApp
{
public class Car : Vehicle
{
private string brand;
private int yearsSinceLastService;

public string Brand
{
get{return brand;}
set{brand = value;}
}
public int YearsSinceLastService
{
get{return yearsSinceLastService;}
set{yearsSinceLastService = value;}
}

public sealed override void Description()
{
base.Description();
Console.WriteLine("Brand: {0}", Brand);
Console.WriteLine("Years since last service: {0}", YearsSinceLastService);
}

}
}


Program.cs





using System;

namespace TestApp
{

class Program
{


public static void Main(string[] args)
{
Car car = new Car();
car.Weight = 1000;
car.Owner = "Joe Cuhp";
car.Brand = "Volvo";
car.YearsSinceLastService = 5;

car.Description();
Console.Read();
}
}
}



Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.