web analytics

Interface vs Abstract Class in C#

Options

codeling 1595 - 6639
@2015-12-18 08:47:05

In practice, an interface is a good way to encapsulate a common idea for use by a number of possibly unrelated classes, while an abstract class is a good way to encapsulate a common idea for use by a number of related classes.

Interface

An interface defines a contract. In other words, you can say an interface is an abstraction of an abstraction, while a class encapsulates a data structure and its operations, an interface encapsulates a type of data structure and its operations.

For example, the .NET ICloneable interface defines a type of class that can be cloned, or copied, from an existing class instance to a new one. This concept applies to the Array, Brush, Font, String, and a number of other classes throughout the .NET Framework. Languages such as C++ provide multiple inheritance for this type of support. In C++, ICloneable could be an abstract class and inherited where needed. In C# and Java, only single inheritance is supported, so this is not possible. Instead, both languages provide interfaces as a way to encapsulate common functionality that can be used by a wide range of classes.

An interface declaration may declare zero or more members. The members of an interface must be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can an interface contain static members of any kind.

The interface itself does not provide implementations for the members that it defines. The interface merely specifies the members that must be supplied by classes or interfaces that implement the interface.

For example, the following IStringList interface declares an interface that contains one each of the possible kinds of members: A method, a property, an event, and an indexer.

public delegate void StringListEvent(IStringList sender);
public interface IStringList
{
   void Add(string s); //Method
   int Count { get; } //Property
   event StringListEvent Changed; //Event
   string this[int index] { get; set; } //Indexer
}

All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, it is a compile-time error for an interface member to include any of the following modifiers: abstract, public, protected, internal, private, virtual, override, or static.

An interface can inherit from zero or more interfaces, which are called the explicit base interfaces of the interface.The explicit base interfaces of an interface must be at least as accessible as the interface itself. For example, it is a compile-time error to specify a private or internal interface in the interface-base of a public interface.

Abstract classes

The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class. An abstract class differs from a non-abstract class is the following ways:

  • An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types.
  • An abstract class is permitted (but not required) to contain abstract members.
  •  An abstract class cannot be sealed.

When a non-abstract class is derived from an abstract class, the non-abstract class must include actual implementations of all inherited abstract members. Such implementations are provided by overriding the abstract members. For example:

abstract class A
{
 public abstract void F();
}
abstract class B: A
{
 public void G() {}
}
class C: B
{
   public override void F() {
    // actual implementation of F
   }
}

the abstract class A introduces an abstract method F. Class B introduces an additional method G, but since it doesn’t provide an implementation of F, B must also be declared abstract. Class C overrides F and provides an actual implementation. Since there are no abstract members in C, C is permitted (but not required) to be non-abstract.

Like a non-abstract class, an abstract class must provide implementations of all members of the interfaces that are listed in the base class list of the class. However, an abstract class is permitted to map interface methods onto abstract methods. For example:

interface IMethods
{
 void F();
 void G();
}
abstract class C: IMethods
{
 public abstract void F();
 public abstract void G();
}

Here, the implementation of IMethods maps F and G onto abstract methods, which must be overridden in non-abstract classes that derive from C.

Summary

The following table summarizes the similarities and differences between an interface and an abstract class.

Feature

Interface

Abstract class

Multiple inheritance

A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation

An interface just defines a contract, it cannot provide any implementation.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modfiers An interface cannot have access modifiers for the methods, events, properties etc, everything is assumed as public An abstract class can contain access modifiers for the methods, properties etc

Core VS Peripheral

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Adding functionality (Versioning)

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Fields and Constants No fields can be defined in interfaces

An abstract class can have fields and constrants defined.

An abstract class can have static methods and static members.

@2016-01-04 08:54:07

To understand what's the difference between abstract class and interface, you must think about abstraction and encapsulation, the two aspects that object oriented programming heavily relies on in modeling reality.

An interface is a good way to encapsulate a common idea for use by a number of possibly unrelated classes, while an abstract class is a good way to encapsulate a common idea for use by a number of related classes.

Interface represents CAN-DO relationship, such Bird CAN fly, Airplane CAN fly too, while the abstract class represent IS-A relationship, such as Student IS A Person, Employee IS A Person.

Interface can only contain events, indexers, methods or properties and of these defined in interface are by default public and abstract; whileas abstract class can contain abstract methods, abstract property as well as other members with implementation.

An Interface can support multiple inheritance,while abstract class cannot support multiple inheritance.

An Interface can be inherited from by structures,when abstract class cannot be inherited from by structures.

Interfaces contain no implementation for any members, a class that implements an interface need to contain all the implementation, if you add a new method to an interface then you have to track down all the implementations of the interface and define implementation for the new method; whereas abstract classes allow you to partially implement your class, if you add a new method to an abstract class then you have the option of providing default implementation and therefore all the existing code might work properly.

@2016-01-04 11:15:03

When Do You Need to Declare a Class as Abstract in C#?

Abstract class is a class that should never be instantiated; only its subclasses should be instantiated. Abstract classes are defined so that other classes can inherit from them.

When a non-abstract class is derived from an abstract class, the non-abstract class must include actual implementations of all inherited abstract members.

In the following two occasions, you have to declare a class as abstract:

  1. When the class itself is inherited from an abstract class, but not all base abstract methods have been overridden.
  2. When at least one of the methods in the class is abstract. 
@2020-07-10 09:25:38

It is recommended to use interfaces to decouple your application’s code from specific implementations of it, or to restrict access to members of a certain type.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com