Abstract classes and interfaces
Date posted: 09/11/2013
You haven't filled in compulsory values.
The email is not correct
In this article we are going to talk about abstract classes and interfaces. Both abstract classes and interfaces are a part of .Net's architecture and are not explicit of its web equivalent. They are a way of describing some characteristics a class may have in a way resembling the inheritance model. Yet, though these two may look similar there are a few significant differences and these will consist of the second part of the article.
First there was inheritance
I guess most developers know what inheritance is. Since a base class and an interface have much in common we'll go through the inheritance to reach the interface. In a few words inheritance is the connection between two classes when one of them gets the other one's characteristics. For example we can use the classes Parent and child.
public class Parent
{
public string Name { get; set; }
public void SetName()
{
Name = "Anakin";
}
}
public class Child : Parent
{
}
Child inherits the class Parent. That means that a Child object has all the characteristics a Parent object has.
For example
Parent par = new Parent();
par.SetName();
ResponseLitID.Text = "Parent's name is " + par.Name;
Parent's name is Anakin
A child object has both a Name attribute and a SetName class
Child ch = new Child ();
ch.SetName();
ResponseLitID.Text = "Child's name is " + ch.Name;
Child's name is Anakin
Inheritance also allows us to hide or override the parent class characteristics. This class has its own SetName class
public class HiddenChild : Parent
{
new public void SetName()
{
Name = "Luke";
}
}
HiddenChild hidCh = new HiddenChild ();
hidCh.SetName();
ResponseLitID.Text = "Child's name is " + hidCh.Name;
Child's name is Luke
Abstract classes
So far, we have seen examples of basic inheritance applications. However there may be times you do not want to implement the whole of these characteristics at once. That's when we should mark a class as abstract.
Here's our new abstract class. There's not much difference between that and the previous Parent method. Actually the only difference is the keyword "abstract".
abstract public class AbstractParent
{
public string Name { get; set; }
public void SetName()
{
Name = "Anakin";
}
}
However if we tried to create an AbstractParent object we would get an error stating we can cannot create an instance of the abstract class. That is the way of the compiler to tell us that an abstract class can only be used to create derived classes, not the one itself. So, we should always keep that in mind. An abstract class cannot be used by itself.
Now, what if we tried to create a derived class? That would work great and we would get our method.
public class AbstractChild : AbstractParent
{
}
AbstractChild abCh = new AbstractChild ();
abCh.SetName();
ResponseLitID.Text = "Child's name is " + abCh.Name;
Child's name is Anakin
In addition to creating an abstract class we can also create abstract methods or properties. To mark a method as abstract it must be placed within an abstract class and be declared (marked as override) only inside the derived class. If not, we will get a compile time error.
So, if we added an abstract method to the AbstractParent class, our example would look like:
abstract public class AbstractParent
{
public string Name { get; set; }
public void SetName()
{
Name = "Anakin";
}
//We may not create body for an abstract class in the base class
abstract public void SetNewName();
}
public class AbstractChild : AbstractParent
{
//An abstract method must be declared in the derived class
override public void SetNewName()
{
Name = "Leia";
}
}
Child's name is Leia
Here's what we have here.
First, there is an AbstractParent class marked as abstract. This means that we cannot create an object of that class the way it is, and we have to create a new class (AbstractChild) in order to use it.
Then we created the SetNewName which is an abstract method which we must implement in the AbstractChild class using the override keyword. The SetNewName method is marked as abstract because we want it to exist, yet we do not want to create an implementation in the base class.
So, now we know what an abstract class is. However, if you are not familiar with such concepts, you may still be wondering why we may want to turn a simple class into abstract since we can simply override every method we like.
The answer is, we do not have to create an abstract class, but we may choose to do so. Suppose you got some derived classes and all of them need to save some values to the session. Instead of creating a new method in each one of them, we can create a method SaveToSession in the base class. However, due to our specifications, each class must store different values in the session. For example class A must store type AA values, while class B must store type BB values and so on. So, the question is: What should the method's body in the base class look like? Should it look like class A, B or any other class? Since we find it difficult to answer we'd better mark the base class and its method as abstract. In that way we ensure that all derived class MUST contain this method.
Interfaces
Now, we know what an abstract class is. An interface is quite a similar term. Consider an interface as an abstract class that contains only abstract elements. As a result an interface can't be instantiated and the derived class (we need to create) must contain source code for all elements implementation.
This is an example of an interface
interface IRunner
{
int speed { get; set; }
string GetName();
}
Usually, interfaces are named using the 'I' letter in the beginning, for example IRunner, IDeveloper, IHelper.
The interface IRunner contains the int speed property and the GetName method, and based on its name it is created so it contains characteristics that runners have. Since it is an interface we may not create an IRunner object or put the GetName's body inside the IRunner declaration.
To use it we have to create a derived class, for example:
public class OlympicRunner : IRunner
{
public int speed { get; set; }
public string GetName()
{
return "I'm an olympic runner.";
}
}
Now we can create an object and use it.
OlympicRunner Bolt = new OlympicRunner();
ResponseLitID.Text = Bolt.GetName();
The result will be
I'm an olympic runner.
Keep in mind that all elements within an interface must be declared private. On the contrary, they must be declared public in the derived classes.
That's about all you need to know concerning interfaces. Interfaces seem to be quite similar to abstract classes.
If so, why do we need interfaces or, similarly thinking, why do we need abstract classes?
The major difference is that a class can inherit only one base class but may inherit as many interfaces as it wants. So, if you had two classes, Bipedal and Mammal, and wanted to inherit both of them on the Human class, I'm sorry to tell you but you can't do that. .Net does not allow that.
This brings us to another crucial difference between abstract classes and interfaces. Think of the previous example. Based on basic biology knowledge we have, should the Human class inherit both Bipedal and Mammal class? Actually, no. The correct architecture should be that Human's base class should be Bipedal and Bipedal's base class should be Mammal, as Human contains Bipedal elements and Bipedals contain Mammal elements. However a Bipedal object could also contain elements from the Bird class. Hmmm, that makes it more complicated. Which class should the Bipedal class derive?
The answer is simple. Bipedal should not be a class, but an interface instead. Actually Bipedal describes something that uses two legs to walk, so we could as well use the interface IBipedal. Keep that simple rule in mind.
A base class represents what a derived class is. An interface represents what abilities it has.
In the previous example, Human is a Mammal, so this is the base class. On the contrary, even though Human is a Bipedal, it would have been more accurate to say that Human has the ability of bipedalism, thus showing us that we should create the interface IBipedal.
Let's see how interfaces work in .Net. A well known .Net interface is IEnumerable. Practically, a class that implements the IEnumerable is granted the ability to loop over its elements, in other words, it can use the foreach keyword. IEnumerable, located in the System.Collections library, contains the GetEnumerator method which returns an IEnumerator interface object. IEnumerator contains the MoveNext method which is used to move to the next element.
Since IEnumerable is an interface, the following statement
IEnumerable intCollection = new IEnumerable ();
would return a compiler error, as expected. However, according to what we said, the List class (which implements the IEnumerable interface) can create objects that can use the GetEnumerator method.
List<int> intList = new List List<int>();
foreach (int i in intList)
//do sth
;
Reaching the end of this article, consider this as one more difference between an abstract class and an article, which may help you decide which one you want to use. An abstract class may contain attributes that are not abstract, while this is not an option to an interface. Remember that an interface can contain nothing more but the declaration of its attributes. So if you want to create some default behaviour, then you may choose an abstract class.
Summary
Abstract classes are a special kind of class which cannot be used on their own but rather tell their derived classes what attributes they should contain. Abstract classes may contain more than just a declaration of their attributes. In contrast, interfaces, though they look much like abstract methods, may contain nothing more than declarations. Furthermore a class may implement more than one interface but may use only one base class.
Back to BlogPreviousNext