I have seen many answers on stackoverflow, but I didn't find an answer that is matching mine. Apart from all those difference, Does it make sense if we say an abstract class abstracts the implementation of behaviour while an interface abstracts the type which implements the behaviour.
An abstract class can (and normally does) provide some implementation.
And interface cannot provide any implementation.
Not really no, because an abstract class doesn't need to implement any behaviour. It probably should, because otherwise you may argue the usefulness of it, but it doesn't have to.
The main differences from design point of view are that:
- you can declare a contract on constructor of the implementing classes, by creating a protected constructor in the base abstract class.
- you can provide implementations of methods usable by base classes
- you can make a wrapper around the contract (e.g. validate method arguments)
- you can provide a "calling scheme" when you create non-abstract methods that call abstract methods of the type, implemented by derived classes. This can be useful for implementing abstraction of an algorithm in derived classes, while the base class implements all the handling logic - prepares and validates data, and lets the actual processing algorithm to be implemented by derived classes.
So I would say you are correct in the statement that "an abstract class abstracts the implementation of behaviour while an interface abstracts the type which implements the behaviour"
Abstract class: provides requirement to implement some methods (you override methods of the abstract class)
Interface: defines only a contract. Indicates that a class that implements the interface has methods of the interface (you implement an interface)
For example:
by implementing an interface on an existing class, you just declare adding the interface methods to the contract of the class. The class may already implement all the methods of the interface and you do not need to change anything in the existing class.
by changing the base type to an abstract class, you are required to override all the methods, even if methods with the same names as abstract methods of the base class already exist on the type.
Commonly, an abstract class implements some behavior, but leaves some specialized behavior unimplemented.
For example, you might write a class that implements a network application server, but does not implement the process function, instead leaving that to the inherited class to implement.
class MyServer(Networkserver):
// process() is called whenever a client has sent a request
function process(data):
...
By making the class abstract and therefore unable to be instantiated, there does not have to be some proper "default" behavior for the specialization functions.
Interface = pure abstract class (abstract class with no implementation)
Purely from a design point of view, and being language agnostic, an interface is a contract between your class and the client, promising what it does, not how it does it. This is the usage implied in the "program to an interface" mantra.
Since languages such as C++ don't have interfaces, an abstract class is the only way to represent it. For languages in which interface is a first class construct, either way is acceptable and there are trade-offs in the choice. There are, of course, other technical differences in implementation between languages, but I don't believe you asked about those.
There is an interview with Erich Gamma, in which he discusses some of the differences.
To answer your question, I think it makes sense from a theoretical point of view. From a practical point of view, it probably depends which language you are programming in :)
Both have specific uses as per the language design- abstract class are designed to be a base class and cannot be instantiated. wheras when u need to define just a contract (NO implementation) which each implementing class must follow in thrie own way, then u must use interfaces.Also -
Can be a base class for Inheritance
abstract class - yes Interface - no
Can have impelementation
abstract class - Yes Interface -No