728x90
virtual
  • Operator preceding the parent class
  • Can be redefined by child class

 

override
  • Operator preceding the child class.
  • is required to extend or modify the abstract of virtual implementation of an inherited method, property, indexer, or event.
  • provides a new implementation of a member is inherited from a base class.
  • Often(Always?) dependent on parent class's function => can call the parent class.
  • must match the parent class of function, name, and signature.

 

  • Tony Stark creates an ArmorSuite class with a method called Initialize ().
  • And he decided to upgrade this weapon class.
  • Create a derived class that inherits ArmorSuite. (:)
  • IronMan and WarMachine new weapon with new function.
  • They cannot equip their weapons with the Initialize () method inherited from ArmorSuite.
  • So they need to Overriding! for equip their weapons

 

 

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
using System;
namespace example
class ArmorSuite
  // 'ArmorSuit' Basic Fuction : Equipped with gloves to protect people
{
    public virtual void Initialize()
    {
    }
}
 
class IronMan : ArmorSuite      // new function with diff weapon
{
    public override void Initialize()
    {
        base.Initialize();      // also need armed inherited from ArmorSuite.
        Console.WriteLine("Rays Armed");
    }
}
 
class WarMachine : ArmorSuite   // new function with diff weapon
{
    public override void Initialize()
    {
        base.Initialize();
        Console.WriteLine("Cannons Armed");
    }
}
 
cs
728x90

BELATED ARTICLES

more