As we discussed in the part 1 there were many concepts of Object Oriented Programming, but usually the abstract class and interface are stubborn and they do not allow us to understand them easily. So let’s discuss in detail about these two concepts and try to get answer for some of the frequently asked questions.

Abstract Class:

They are known as half defined parent class. Their full implementation is done by child classes. It means that the abstract class has a missing or incomplete implementation. It can only be declared as the base class of the other classes. In part 1 we have taken the sample code where abstract class was “Transport”, so this means that Transport is the base class and from this base class many other classes can be derived and inherited. Here we only know that Transport is a class, but something is missing in its implementation that is what transport? Which transport? And then child classes like “Roadways”, “Railways” complete the implementation of the Transport class. These classes are derived from transport class and they also inherit the properties of transport class.

Interface:

It is defined as a contract between the two interfaces such as User interface and database or any other kind of interface where they follow some guidelines which cannot be violated.

To simply understand this we can take our daily life example where we see there are many types of contracts made between two people or between many people. Now as the contract is made between two people there are some guidelines which both of them have to follow. If they are trying to change anything they are violating the contract, and according to the contract the changes are not allowed.

But then if they want to make the change they can create another contract within the first contract. For example when we purchase or rent a house, we create a contract with the seller or owner. Now suppose for renting the contract has some guidelines such as no parking for the tenant, 10k will be the rent, etc. Now if the owner one day says that he wants 15k rent then according to the contract he is violating the guidelines. But then they can make another contract under the main contract which will be only about the rent and it will not violate any other aspects of the main contract like about the parking.

In the same way the Interface works in Object Oriented Programming. Here is a sample code for the interface.

interfaceIDatabase
{
void Save();
}
Public class SQLServer: IDatabase
{
      void Save();
}

Now here we have an interface “IDatabase” and its method or we can call as a signature in contract is “Save”. It means that whichever class is inheriting with this IDatabase it has to implement the same method which is there in this class that is “Save”. Now here we have a public class SQLServer which is a part of the interface class IDatabase, so this public class will have to implement the Save method. If it tries to implement any other method other than Save, like Add, Delete, etc. the code will show an error and it will not run because the contract is being violated.

So in this way Interface works in the object oriented programming like a regular concept of contract in the real world. Now if we want to implement other method without disturbing the initial contract or initial interface class, we can then create a new contract under the existing one.

Now many a times a question is asked whether abstract class and interface have any similarities or what is the difference between them?

Difference:

In Abstract Classsome methods can have implementation.

Here we can also declare variables.

We cannot create object of abstract class.

And abstract classes are inherited.

In Interface all the methods, functions, properties are empty. It means that interface is a class but has no implementation as every method, functions, properties are empty.

Here we cannot declare variables.

We cannot create object of interface.

And interfaces are implemented not inherited.

Abstract classes are used when a common functionality has to be shared in a parent-child relationship.

Interfaces are used when we want to define some contract, bring some standardization, for decoupling and also for dynamic polymorphism.Use of abstract class & Interface:

Abstract classes are used when a common functionality has to be shared in a parent-child relationship.

Interfaces are used when we want to define some contract, bring some standardization, for decoupling and also for dynamic polymorphism.

Important note:

If all the methods, functions, properties are defined as abstract in Abstract class then it will inhibit same behavior as interface i.e. Abstract class will behave as interface in such condition.

Interfaces once fixed cannot be changed. If we ever want to change anything or add new functions we will have to create a new interface separately or we can create new interface within the existing interface so that we do not break the compatibility with the old clients or the existing ones.

 

Advertisements