Comparing "override" and "new" in C#

gpeipman
1,215 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Welcome!

This example explains members versioning in C#. Members that support versioning are marked as virtual and therefore they can be overriden. From extended class it is possible to call member with same name in base class and this means that we have more than one version on given member.

Let's take a look at the following code sample where Animal, Dog, Kangaroo and Cat classes are defined.

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
using System;
public abstract class Animal
{
public virtual void Move()
{
Console.WriteLine("Ouch! Unknown animal:" + GetType());
}
}
public class Dog : Animal
{
public override void Move()
{
Console.WriteLine("Dog runs");
}
}
public class Kangaroo : Animal
{
public override void Move()
{
Console.WriteLine("Kangaroo jumps");
}
}
public class Cat : Animal
{
}
class Hello
{
static void Main()
{
var dog = new Dog();
var kangaroo = new Kangaroo();
var cat = new Cat();
dog.Move();
kangaroo.Move();
cat.Move();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Calling base version

Let's rewrite the code above to be more realistic and let's assume that most of animals run by default and those who don't will override the move method. Cat class calls the same base class method as Dog class does but it has local version of Move() method too.

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
using System;
public abstract class Animal
{
public virtual void Move()
{
Console.WriteLine(GetType() + " runs");
}
}
public class Dog : Animal
{
}
public class Kangaroo : Animal
{
public override void Move()
{
Console.WriteLine("Kangaroo jumps");
}
}
public class Cat : Animal
{
public override void Move()
{
base.Move();
}
}
class Hello
{
static void Main()
{
var dog = new Dog();
var kangaroo = new Kangaroo();
var cat = new Cat();
dog.Move();
kangaroo.Move();
cat.Move();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Hiding original member

It is also possible to hide existing versions behind new ones. Take a look at LazyCat class in following example. It uses "new" keyword to define local version of Move() method. How it is different from overridden one? Run the code sample and see what happens with LazyCat when it is casted to Cat.

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
46
47
48
49
50
51
52
53
using System;
public abstract class Animal
{
public virtual void Move()
{
Console.WriteLine(GetType() + " runs");
}
}
public class Dog : Animal
{
}
public class Kangaroo : Animal
{
public override void Move()
{
Console.WriteLine("Kangaroo jumps");
}
}
public class Cat : Animal
{
public override void Move()
{
base.Move();
}
}
public class LazyCat : Cat
{
public new void Move()
{
Console.WriteLine("Lazy cat walks");
}
}
class Hello
{
static void Main()
{
var dog = new Dog();
var kangaroo = new Kangaroo();
var cat = new Cat();
var lazyCat = new LazyCat();
dog.Move();
kangaroo.Move();
cat.Move();
lazyCat.Move();
((Cat)lazyCat).Move();
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content