Tuesday, 27 December 2016

Abstract Class & Interface - When to use?

Abstract Class

The "abstract" keyword indicates that the thing has a missing or incomplete implementation and must be completed by others. 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. 

  



As shown in figure we are defining an iPhone base class for Apple and then inheriting it to iPhone6 and iPhone6s sub classes. The iPhone class should be an abstract class that contains some predefined functions like Call() and SMS() for all iPhone models to share . We can also add abstract methods like Model() and Color() into the iPhone class that must be implemented by all the subclasses inheriting iPhone. The main advantage of this approach is, whenever we inherit the iPhone class into a derived class, say iPhone6s, we need not define the Call() and SMS() methods again. We just need to implement the abstract methods and we are good to go. It helps to provide default functionality in all the derived classes and also avoids code duplication. 

Key Points

1. We cannot create an object of Abstract Class but we can create a reference of it.
2. 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. 

4. An abstract class can have abstract as well as non-abstract methods. 

5. The abstract keyword can be used with class, methods, properties, indexers and events.

6. Abstract members can only be declared inside an abstract class. 

7. An abstract member cannot be static or private. 

8. An abstract method cannot be marked virtual. 


9. A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible. 

Interface

An Interface means a medium to interact with something. In terms of programming, 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. We can call it as pure abstract class. It can contain only signature but no implementation. 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.

Why do we need an Interface?





As shown in figure, if we 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. For this we make the Smartphone class abstract and also all its members abstract. This approach works fine and classes like Samsung, Apple, HTC can inherit from it.
Now, suppose Apple wants to add a new feature to its Smartphone. We can add this feature as an abstract method in our abstract base class SmartPhone. But what if HTC doesn't want that feature. So the method for new feature cannot be placed inside the abstract class SmartPhone. An alternative is to define another abstract class for new features and add methods 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. An interface provides only the method definitions, just like an abstract class, but can be useful in multiple inheritances. We can make the new Features class as an interface and add methods to it. It provides only the method signature and whichever class inherits it can implement it in its own way. It is also completely valid for a class to inherit more than one interface in C#. Also, we can make the SmartPhone class an interface instead of an abstract class. It is better instead of making a pure abstract class, we can use interfaces.

Key Points

1. A class may inherit several interfaces.
2. An interface cannot provide any code, just the signature.
3. An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed      as public
4. If various implementations only share method signatures then it is better to use Interfaces.

5. Cannot contain fields, constant members, constructors, destructors and static members.



No comments:

Post a Comment