Virtual Methods
Virtual methods allow a reference to the base class to access "up into" the derived class.
You can use a reference to a base class to call a method in the derived class if the following are true:
The code result following display:
Important things to know about the virtual and override modifiers are the following:
You can use a reference to a base class to call a method in the derived class if the following are true:
- The method in the derived class and the method in the base class each have the same signature and return type.
- The method in the base class is labeled virtual.
- The method in the derived class is labeled override.
class person { public string firstName; public string lastName; public ushort age; public virtual void greet() { Console.WriteLine("Person say: Hello "+ firstName + " " + lastName + ". I'm " + age); } } class employee : person { public string company; public override void greet() { Console.WriteLine("Employee say: Hello " + firstName + " " + lastName + ". I'm " + age); Console.WriteLine("I work for " + company); } } class Program { static void Main(string[] args) { employee me = new employee() { firstName="Yang",lastName="Sopiana",age=29,company="Company.inc"}; Console.WriteLine("Calling from derived class"); me.greet(); person personMe = (person)me; Console.WriteLine("Calling from base class"); personMe.greet(); } }
The code result following display:
Calling from derived class
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Calling from base class
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Calling from base class
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Important things to know about the virtual and override modifiers are the following:
-
The overriding and overridden methods must have the same accessibility. In other words, the overridden method cannot be, for example,
private
, and the overriding methodpublic
. - Can not override a method that is static or is not declared as virtual.
- Methods, properties, and indexers and event can all be declared virtual and override.
Abstract Members
An abstract member is a function member that is designed to be overridden. An abstract member has the following characteristics:
The code will display:
- It must be a function member. That is, fields and constants cannot be abstract members.
- It must be marked with the abstract modifier.
- It must not have an implementation code block. The code of an abstract member is represented by a semicolon.
- Four types of members can be declared as abstract: Methods, Properties, Events, Indexers
abstract class person { public string firstName; public string lastName; public ushort age; public abstract void greet(); //abstract doesn't have implementation } class employee : person { public string company; public override void greet() //abstract method should be overridden { Console.WriteLine("Employee say: Hello " + firstName + " " + lastName + ". I'm " + age); Console.WriteLine("I work for " + company); } } class Program { static void Main(string[] args) { employee me = new employee() { firstName="Yang",lastName="Sopiana",age=29,company="Company.inc"}; Console.WriteLine("Calling from derived class"); me.greet(); person personMe = (person)me; Console.WriteLine("Calling from base class"); personMe.greet(); } }
The code will display:
Calling from derived class
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Calling from base class
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Calling from base class
Employee say: Hello Yang Sopiana. I'm 29
I work for Company.inc
Virtual Vs Abstract
Following table shows you the differences between
virtual
and abstract
:
Virtual Member | Abstract Member | |
---|---|---|
Keyword | virtual |
abstract |
Implementation body | Has an implementation body | No implementation body—semicolon instead |
Overridden in a derived class | Can be overridden | Must be overridden |
Types of members | Methods, Properties, Events, Indexers | Methods, Properties, Events, Indexers |
0 comments:
Post a Comment