Abstract Class Vs Interface

An abstract class is a special kind of class that has no implementation. It cannot be instantiated. Its implementation logic is provided by the classes that derive from it. It can have both abstract as well as non-abstract methods.
It is not compulsory to have only abstract methods in an abstract class. We can also have an abstract class with only non-abstract methods.

Why do we need an Abstract Class?

With an Abstract Class, we can provide some kind of default functionality for all derived classes to extend from. This is useful to avoid code duplication in many cases. (It helps to provide default functionality in all the derived classes and also avoids code duplication.)

Abstract classes are also useful in the case of modifications to the project. If you plan on updating the base class in your project, it is better to make the class abstract. Because you can define a functionality in an abstract base class and automatically all the inheriting classes will have the same functionality without disturbing the hierarchy.

Note: We can have a Base class for default implementation of some logic. But the difference b/w abstrct and base class is
1. we can’t create object to abstract class. If you never want to be able to instantiate the base class then make it abstract. But we can create a reference of it.

//We can’t do this
//absClass cls = new absClass();

//We can do this
absClass cls;

2.Any class inheriting the abstract class must implement it’s methods. An inheritance between abstract to abstract classes is possible. We don’t need to implement abstract methods of the base abstract class into a derived abstract class. We can implement it later in concrete classes.

3. An abstract class can never be sealed or static. If we compile the below code, it throws an error.

What is an Interface?

In the real world, an interface means a medium to interact with something. Similarly, in the programming world, an interface means a contract to interact with multiple code modules. If a class wants to communicate with an interface, it must implement it and define its members.

An interface is declared using the keyword “interface”. Interface members are implicitly public and abstract, so we cannot prefix any access modifiers to it. An interface cannot contain fields, constant members, constructors, destructors and static members.

An interface has no implementation and cannot be instantiated.

Some rules:
1. we cannot declare these methods as static.
interface ISmartPhone
static void OS();

2. An important point that should be noted here is that whenever we implement interface members in derived classes, the access modifier must always be public otherwise it throws an error. If we write a protected modifier instead of public to the OS method, the compiler throws an error.
Advantage of using an interface over an abstract class; that is “Multiple Inheritance Support”.

Interface Ex:
Suppose we need to define a class for a Smart Phone. The class can have members like OS, AppStore and Call. The Smartphone can be either Android based or iOS based and cannot be both. There is no common functionality between an Android and iOS Smartphone, so we don’t need to provide any kind of default functionality. One approach is to make the Smartphone class abstract and also all its members abstract. This approach works fine and several concrete classes like Samsung, Apple, HTC can inherit from it.

Now, after a few days Apple wants to add a Touch ID feature to its Smartphone. We can add TouchID as an abstract method in our abstract base class SmartPhone. But what if HTC doesn’t want that feature and neither does Samsung? So, the TouchID method cannot be placed inside the abstract class SmartPhone. An alternative is to define another abstract class Features and add the TouchID method to it. This is also a bad idea since C# doesn’t support inheritance of multiple classes (abstract or concrete) into a derived class.

In this situation, an interface is useful and plays a vital role in solving the problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s