MorkaLork Development

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

Inheritance

2009-04-16 17:56:40 | 213 views | inheritence inheritance polymorhp inherit base this declare

The basics



Inheritance in C# can save you alot of time. What it means is that you can create a root class with methods and properties, then make a new class, inherit the old class, and the new class will have access to everything in the root class.
This article will try to explain the basics of the C# inheritance concept.

Getting into it



Right.
First of all, when creating a class, we can start by sorting out the base of that class. If we, for example, have to create a Car class, a class that defines a car with methods and properties, then we can say that it is, at ground level, a vehicle. If you also have to create a Bicycle then you can, as well, say that it is a vehicle. If you can create a vehicle class with some basic methods and properties, then we just might be able to inherit that afterwards.

Let's see this in code.

Vehicle.cs




using System;

namespace TestApp
{

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

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


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;}
}
}
}


As we can see, the Car class inherits the Vehicle class by adding ": Vehicle" after the class declaration.
Note: You can only inherit one class per class.
This means that we don't have to declare properties for weight and owner since the Car class inherits them properties from Vehicle.
If we create a bicylce class, it can inherit the vehicle class as well, removing the need to declare properties for weight and owner.

Bicycle.cs




using System;

namespace TestApp
{
public class Bicycle : Vehicle
{
private bool handBreaks;
private bool gears;

public bool HandBreaks
{
get{return handBreaks;}
set{handBreaks = value;}
}
public bool Gears
{
get{return gears;}
set{gears = value;}
}
}


Now Bicycle has, besides the properties HandBreaks and Gears, also Weight and Owner since it inherits Vehicle.
This will be shown if we use intellisense when using the Bicycle class:
imagehttp://admin.morkalork.com/uploads/images/csharp/Inheritance.png

As we can see, our instantiation of bicycle has inherited the properties of Vehicle. The same will go for Car.

This picture might illustrate even better the relationship between the classes:
imagehttp://admin.morkalork.com/uploads/images/csharp/Inheritance2.png

The chain can go further down. If you create a class for company cars you can inherit the Car class. The CompanyCar class will then inherit from both Car and Vehicle since Car inherits from Vehicle meaning that the class CompanyCar will start off with the properties Weight, Owner, Brand and YearsSinceLastService. You can after that add the specific properties of that class.



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.