Understanding abstract classes
Join the DZone community and get the full member experience.
Join For FreeA misconception I sometimes encounter among beginner developers is the fact that some people do think that abstract classes and interfaces are the same thing. However, this is a wrong understanding. Although abstract classes and interfaces have similarities, their purpose as well as structure is different.
Abstract classes can have method/function implementations
The interface is a structure, that allows the developer to outline a set of methods, properties or and/or indexers that should be implemented in a class that implements the interface. Therefore, if an interface defines the Add and Remove methods, the inheriting class must have those methods implemented. One important aspect of interfaces is that no implementation is provided for the declared methods and functions – it all has to be done in the class that inherits the interface.
Abstract classes, on the other side, can (but not necessarily) have method/function implementations, providing default functionality for them. Therefore, once a class inherits an abstract class (in that case referenced as the base class), the developer can either use the default functionality or override the existing methods/functions.
A class can inherit multiple interfaces, but only one abstract class
C# is a language that does not support multiple inheritance. And although a single class can inherit multiple interfaces, it cannot inherit multiple abstract classes. It can be considered a limitation, but in fact it isn’t. An interface gives the freedom to develop anything that is built around a well-defined structure, while an abstract class sets a hierarchy that should be followed.
Let’s take a look at an example. Here I have an abstract class SystemDetails:
public abstract class SystemDetails
{
public string GetDetails()
{
string version = Environment.OSVersion.VersionString;
string machineName = Environment.MachineName;
return string.Format("{0} is using {1}", version, machineName);
}
public void CheckTime()
{
Debug.Print(DateTime.Now.ToString());
}
public abstract void GetUserInfo();
}
This class explicitly defines the default functionality for GetDetails and CheckTime. At the same time, it defines an abstract method – GetUserInfo, that doesn’t carry any implementation (just like in an interface).
A class that will inherit SystemDetails can use the default functionality of GetDetails and CheckTime, but must also provide an implementation for GetUserInfo. Here is an example:
public class MySystem : SystemDetailsAs you see, I am not providing an implementation for GetDetails and CheckTime inside the MySystem class. However, when I will instantiate the class, I can call both GetDetails and CheckTime, since those are already implemented in the base class.
{
public override void GetUserInfo()
{
Debug.Print(Environment.UserName);
}
}
As GetUserInfo is an abstract method, I had to provide an implementation. The difference (compared to interfaces) is that I have to override the existing method declaration, even if no implementation is provided in the base class.
Using the same override keyword, I can override the existing methods, properties and functions, providing new functionality.
Access modifiers
Compared to an interface, abstract classes can have members that have different access modifiers. In an interface, all elements are set as public and that cannot be changed. In an abstract class, the developer can use different access modifiers (like private, protected or internalprotected). This would mean that a lot of class members can be used internally, without those being exposed.
The question – when to use interfaces and when – abstract classes?
This is one of the questions that comes up in many interviews, but also when a developer tries to decide what’s better for a specific case. When you want to set a base structure that will apply to classes of the same type, then use abstract classes. An interface is applied to classes with diverse types and no well-known structure, that can have different functionality (for example, the same method declared in an interface can be implemented differently in two classes that implement the same interface). And although in an abstract class the methods can be overridden, if you intend on providing default functionality for a multitude of classes, then abstract classes is the way to go.
Opinions expressed by DZone contributors are their own.
Comments