Visibility and Accessibility Modifiers
Abstract Modifier
The abstract modifier is used in inheritance scenarios. When applied to a class, it forces that class to be inherited, you can't instantiate an abstract class. When applied to a type member, such as a method, it enforces that anything that inherits from the class has to implement the abstract member.
using System;
// The message class must be inherited.
public abstract class Message
{
public void Send()
{
string address = GetAddress();
Console.WriteLine(address);
}
// This method must be implemented by something that inherits from Message
public abstract string GetAddress();
}
public class EmailMessage: Message
{
public override string GetAddress()
{
return "bill@microsoft.com";
}
}
public class Program
{
public static void Main(string[] args)
{
Message message = new EmailMessage();
message.Send();
}
}
In the above example, we've created a class called 'Message'. The Message class cannot be instantiated using the new keyword because it needs to be inherited:
public class EmailMessage : Message
{
public override string GetAddress()
{
return "me@somewhere.com";
}
}
The GetAddress method is also marked as abstract, and you'll notice that we haven't actually provided an implemention, we simply end the declaration with the ; semi-colon line terminator.
When we implement an abstract method in your inherited class, you must specify the override modifier as part of the declaration:
public override string GetAddress()
{
}