Abstract Class and Interface

Abstract Class
———————

A Abstract class nothing but a class which is declare using a abstract keyword. It also allows us to create method with abstract keyword (abstract methodName()), and forces its subclass to implement all the declare methods. Abstract class must have one abstract method in it.

For example, let’s suppose you want to built a different kind of Bikes, which will be in different color, model etc. Now, as process will be same to manufacture a bike, but color, model can be different based on bike types.

Interface
——————-

Interface is a blueprint of a system. A Interface is a concept which allows us to interact between two different unrelated entities. In simple words , remote control is an interface between you and Television.

For example, if you want to built a house, you need to have a blueprint about how your house will look like, how may bedrooms will be there, how many windows will be there etc.

Let’s clear these two concepts with simple design and example.

Relation Between Abstract Class and Interface :

In Abstract Class we can define the characteristics of an object, specifying “What an Object is?” while Interface define a capability “What the Object can do?

Bit confused, lets make it simple, lets consider above exmple “Remote Control is an Interface Between you and television”.

Here,

Abstarct Class (What is an object?): Remote (Is an Object)
Interface (What the Object can do?) : Remote turns on the Television (Object can turns on the television)

When to use Abstract Class and When to use Interface

  • Abstract Class
    • If you want to add more method in in future, then abstract class is a better choice to implement.
    • Abstract class you can use when most of the behaviour is same for the system but there are few different implementation should be done based on some scenario. Example, Create reports in different format where data will be the same in all format.
    • Abstract class you can use if you require non-public function in your class.
    • If you want to share you code among several closely related class.
    • If you are designing the large functional units, then you must use an abstract class.
    • Use Abstract class when you can make the statements “A is B
  • Interface
    • Interface you can use when you know your code is not going to change for a long time.
    • Interface is good for design a loosly coupled system.
    • Interface you can use if your functionality can be use across the application or wide range of application.
    • If you want to have something similar to the multiple inheritances, then you can implement various interfaces.
    • If we are going to design the small, concise bits of functionality, then you must use interfaces.
    • Always use Interface when you can make the statement “A is capable of doing this?“.

Class Diagram of Abstract Class

Abstract Class – generateReports.ts (Language: TypeScript)

Class Diagram of Interface

Interface : IAction.ts (language: TypeScript)

GitHub link : Abstract Class and Interface 🙂

Leave a comment