The word abstract means – existing in thought or as an idea but not having a physical or concrete existence. Both abstract classes and interfaces can be used to define contracts to be extended or implemented in Object Oriented Programming.
The following are some of the key features of an abstract class and an interface, and the differences between them.
What is an Abstract Class?
An abstract class can contain both abstract and concrete methods. It can contain constructors, fields and methods like a concrete class. It cannot be instantiated and can only be extended by other classes.
An abstract class is similar to a concrete class, where you cannot extend more than one abstract class. Any class that extends an abstract class has to provide an implementation for all of the abstract methods defined in the base abstract class.
You can use an abstract class as a base class that can provide shared functionalities which can be used across all its child classes.
What is an Interface?
An interface is a complete abstract entity that creates a template for classes that implement it. It cannot contain any concrete methods. It can only contain method declarations with signature and return type. It can also contain constants and properties.
Any class that implements this interface must provide a definition to all the methods and properties declared in the interface.
You cannot instantiate an interface directly, you can only create the instance of a derived class that implements this interface.
A derived class can implement more than one interfaces. This allows a class to create multiple behaviors based on the different interfaces it implements. In Java or C#, you can only extend one abstract class and implement multiple interfaces.
An Interface is used to define common behavior for unrelated classes. Using this, multiple classes can comply with a single contract even if they serve different purposes.
The following is an example for an Abstract class and Interface implementation in C# –
using System;
public abstract class Figure
{
public abstract void Draw();
public void SomeMessage()
{
Console.WriteLine("Some Message printed from the Shape");
}
}
public interface IScalable
{
void Scale(int scaleFactor);
}
public class Rectangle : Figure, IScalable
{
private int width;
private int height;
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public override void Draw()
{
Console.WriteLine("Drawing a rectangle with");
Console.WriteLine($"width: {width} and height: {height}");
}
public void Scale(int scaleFactor)
{
this.width *= scaleFactor;
this.height *= scaleFactor;
}
}
public class Circle : Figure, IScalable
{
private int radius;
public Circle(int radius)
{
this.radius = radius;
}
public override void Draw()
{
Console.WriteLine("Drawing a Circle with");
Console.WriteLine($"Radius: {radius}");
}
public void Scale(int scaleFactor)
{
this.radius *= scaleFactor;
}
}
public class Program
{
public static void Main()
{
Rectangle rectangle = new Rectangle(5, 3);
rectangle.Draw();
rectangle.Scale(2);
Circle circle = new Circle(4);
circle.Draw();
circle.Scale(6);
}
}
Summary – differences between abstract class and interface class
Abstract Class | Interface | |
---|---|---|
Type Definition | Can contain both concrete and abstract methods, fields and constructors | Can contain only method declarations and constants |
Instantiation | Cannot create an object of the Abstract Class directly. Can only create an object of the derived class that extends the abstract class | Cannot create an object of the Interface directly. Can only create an object of the derived class that implements the interface |
Inheritance | A derived class can extend only one Abstract class similar to a concrete class. The derived class extending the abstract class must provide definitions for all the abstract methods. | A derived class can implement more than one interface classes. The derived class must implement all the methods and properties declared in the interface |
Usage | You can use abstract classes to provide base functionality which can be shared by all the derived classes | You can use interfaces to provide common behavior for unrelated classes even if they serve different purposes |