Definition
Inheritance is a method used in OOP which consists in creating classes derived from a base class. Basically, you’ll have a base class which ‘shares’ it’s methods with all the derived classes.Simple Inheritance
To create a derived class from a base class you have to use something like this:
1
2
3
4
5
|
class derivedClass : baseClass
{
//some code
}
|
Example : It might be easier to understand this concept by looking at a practical way to use inheritance.
- • let’s say we have a base class called vehicles that has 2 functions (spawn & despawn)
- • we want to create from that class some new particular classes (car & plane)
- • we also want our new classes to have those 2 functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
class vehicle //this is the base class
{
//below are the 2 functions : spawn & despawn
public void spawn()
{
Console.WriteLine("Spawning vehicle...");
}
public void despawn()
{
Console.WriteLine("Despawning vehicle...");
}
}
class car : vehicle //derived class
{
// nothing here
}
class plane : vehicle //derived class
{
//nothing here
}
|
To see how this works, just create 2 objects, and call those 2 functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//------------------------
//You can instantiate objects this way
car car1 = new car();
plane plane1 = new plane();
car1.spawn();
plane1.spawn();
car1.despawn();
//------------------------
//But this works too
vehicle car1 = new car();
vehicle plane1 = new plane();
car1.spawn();
plane1.spawn();
car1.despawn();
|
Surprisingly, you can call the functions even if they aren’t directly declared in the derived classes: that’s inheritance.
Overriding methods
If you understood the way inheritance works, we can go to something more complex: overriding. It’s not very complicated: by using overriding we can customize the inherited functions: basically we can directly change their code and the way they work.Note: a function can be overrided only if it’s marked as virtual in the base class. Overriding is done by placing the modifier override in the method’s header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class baseClass
{
public virtual void myFunction()
{
//some code
}
}
class derivedClass : baseClass
{
public override void myFunction()
{
//somecode
}
}
|
You’ll have something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
class vehicle
{
public virtual void spawn() //virtual function
{
Console.WriteLine("Spawning vehicle...");
}
public virtual void despawn() //virtual function
{
Console.WriteLine("Despawning vehicle...");
}
}
//we mark the methods we want to override by placing the modifier 'override' before their name
class car : vehicle
{
public override void spawn()
{
base.spawn();
Console.WriteLine("Spawning a Car!");
}
public override void despawn()
{
base.despawn();
Console.WriteLine("Despawning a Car!");
}
}
class plane : vehicle
{
public override void spawn()
{
base.spawn();
Console.WriteLine("Spawning a Plane!");
}
public override void despawn()
{
base.despawn();
Console.WriteLine("Despawning a Plane!");
}
}
|
As you can see, the 2 functions (spawn & despawn) from the class vehicle (the base class) are marked as virtual: they can be overrided. Any derived class can customize the functions by applying the override modifier.
base.spawn() and base.despawn() are used to invoke the original functions: the ones from the base class. However those are not absolutely required in derived classes.
If we instantiate 2 objects and call those functions like in the code below:
1
2
3
4
5
6
7
|
vehicle car1 = new car();
vehicle plane1 = new plane();
car1.spawn();
plane1.spawn();
car1.despawn();
|
The output will be:
Spawning a vehicle…
Spawning a Car!
Spawning a vehicle…
Spawning a Plane!
Despawning a vehicle…
Despawning a Plane!
The End!
Sign up here with your email
ConversionConversion EmoticonEmoticon