Its simply to Hide the actual implementation from the client who consumes it.
- It Provides the concerete implementation for the certain functionality and this cannot be directly instantiated
- This will only be accessed from the classes those are implementing it.
- So the clients consuming the derived class, will never know the implementation of the functionality because its abstratced.
Now you would ask why do we need this, since the interfaces serve the same mechanismm.. Go through the simple logger example
interface ILogger
{
string PrepareLog(System.Exception ex);
void InitializeLogger(string Type);
int WriteLog(string msg);
}
Any logging client implements this interface should implement all this functionality
class EventLogger : ILogger
{
public override void InitializeLogger(string Type)
{
//Event Logger Initialize
}
public override int WriteLog(string msg)
{
//Write to event log
return 1;
}
public override string PrepareLog(System.Exception ex)
{
return ex.StackTrace ;
}
}
class FileLogger : ILogger
{
public override void InitializeLogger(string Type)
{
}
public override int WriteLog(string msg)
{
//Write to File
return 1;
}
public override string PrepareLog(System.Exception ex)
{
return ex.StackTrace ;
}
}
class MailLogger : ILogger
{
public override void InitializeLogger(string Type)
{
}
public override int WriteLog(string msg)
{
//Write to mail
return 1;
}
public override string PrepareLog(System.Exception ex)
{
//prepare HTML Formatted msg
return ex.StackTrace ;
}
}
And the classes EventLogger ,FileLogger and maillogger implements the iLogger and gives the Context Specific implementation. Now we wanted to hide the actual implementation of PrepareLog and this would do the common operation of preparing the log message from the exception object.
In our current implementation we dont have the option to make the single method concrete and others to be just contracts.
so lets change the implementation little bit with abstract classes
abstract class AbstractLogger:ILogger
{
#region ILogger Members
public virtual string PrepareLog(System.Exception ex)
{
return ex.StackTrace;
}
public abstract void InitializeLogger(string Type);
public abstract int WriteLog(string msg);
#endregion
}
class EventLogger : AbstractLogger
{
public override void InitializeLogger(string Type)
{
//Event Logger Initialize
}
public override int WriteLog(string msg)
{
//Write to event log
return 1;
}
}
class FileLogger : AbstractLogger
{
public override void InitializeLogger(string Type)
{
}
public override int WriteLog(string msg)
{
//Write to File
return 1;
}
}
class DBLogger : AbstractLogger
{
public override void InitializeLogger(string Type)
{
}
public override int WriteLog(string msg)
{
//Write to DB
return 1;
}
}
class MailLogger : AbstractLogger
{
public override void InitializeLogger(string Type)
{
}
public override int WriteLog(string msg)
{
//Write to mail
return 1;
}
public override string PrepareLog(System.Exception ex)
{
//prepare HTML Formatted msg
return ex.StackTrace ;
}
}
Now i have created the AbstractLogger class which inherits from the iLogger and implemented the PrepareLog method alone, remainig methods left abstract. so the consumers will write the context specific code in their implementation.
So now the PrepareLog method completely hidden (meaning the log preparation) from the cosumers initialized any of the Loggers.
then why PrepareLog is Virtual ed??
There are scenarios the consumer may want to override the preparelog method, Ex: MailLogger will override the PrepareLog and format HTML formatted output to supply to mail messages.
Cheers
Ramesh Vel