Interface and abstract class are important topics of discussion when there is a need for abstraction.
What is an abstraction?
Abstraction is the process of providing details only about what must be implemented and hiding the functionality details from the user.
What is an Interface?
- The set of rules that all class that inherits the interface must follow.
- It includes an abstract with only the declaration and not the implementation.
- The derived classes must implement the methods declared.
- Interfaces are public by default and cannot have access modifiers.
- Multiple inheritances can be achieved through an interface.
Syntax
public interface interface_name{
//Method declaration
<access-specifier><data-type> method_name(parameters);
}
class class1 :interface_name{
<access-specifier><data-type> method_name(parameters){
…
}
}
class class2 :interface_name{
<access-specifier><data-type> method_name(parameters){
…
}
}
Example :1
Explanation
In this example, we have the interface ISocialPageInfo which includes a method getCount with only the declaration and not the body of the method.
The PostStatus and PostDetails class that implements the interface must implement the method getCount declared in the interface.
Even though both classes implement the getCount method, the body of the method is different, in PostStatus, we compare the likes and dislikes and print the popular post whereas, in PostDetails class, the number of shares and comments are displayed.
Abstract Class
- Abstract classes indicate improper implementation and cannot instantiate.
- Do not support multiple inheritances.
- Methods can have access modifiers.
- It includes both abstract and non-abstract methods.
- The derived class must implement methods declared in the abstract class.
Syntax:
abstract class class_name{
//Method declaration
<access-specifier> <data-type> method_name(parameters)
{…}
<access-specifier> abstract <data-type> method_name(parameters);
}
class class1 :class_name{
<access-specifier> override <data-type> method_name(parameters)
{…}
}
class class2 :class_name{
<access-specifier> override <data-type> method_name(parameters)
{…}
}
Example 2:
Explanation:
The abstract class Login includes a normal method getCredentials() and an abstract method authenticate().
It is necessary to implement the authenticate method in both the derived classes such as YouTube and FaceBook classes.
In case there is a need to provide sample information about what to implement, declare a virtual method in the abstract class and override the declared method in the derived classes.
Here in this example, the YouTube and FaceBook classes override the authenticate method with a call to the respective authenticator API.
Difference between the interfaces and abstract classes
Interfaces
- An interface cannot provide implementation but allows signature.
- Can inherit more than one interface and hence supports multiple inheritances.
- The interface is public by default and cannot have access modifiers.
- It includes only abstract methods.
Abstract class
- An abstract class can provide the implementation.
- Can inherit only one abstract class and hence do not support multiple inheritances.
- Access modifiers can be provided for methods.
- It includes both abstract and non-abstract methods.