A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have zero or more abstract methods. i.e., methods without a body. It can have multiple concrete methods.
Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.
Abstract classes cannot be instantiated.
Important Reasons For Using Interfaces
- Interfaces are used to achieve abstraction.
- Designed to support dynamic method resolution at run time
- It helps you to achieve loose coupling.
- Allows you to separate the definition of a method from the inheritance hierarchy
Important Reasons For Using Abstract Class
- Abstract classes offer default functionality for the subclasses.
- Provides a template for future specific classes
- Helps you to define a common interface for its subclasses
- Abstract class allows code reusability.
What is Interface?
The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods [methods that have code]. All the methods of an interface are abstract methods.
An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables [i.e., constant class variables]
Difference between Interface and Abstract Class in Java
An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.
A Constructor is a special member function used to initialize the newly created object. It is automatically called when an object of a class is created.
Why interfaces can not have the constructor?
- An Interface is a complete abstraction of class. All data members present in the interface are by default public, static, and final. All the static final fields should be assigned values at the time of declaration, otherwise it will throw compilation error saying “variable variable_Name not initialized in default constructor”.
- The methods inside the interface are by default public abstract which means the method implementation cannot be provided by the interface itself, it has to be provided by the implementing class. Therefore, no need of having a constructor inside the interface.
- A constructor is used to initializing non-static data members and as there are no non-static data members in the interface, there is no need of constructor
- Methods present in the interface are only declared not defined, As there is no implementation of methods, there is no need of making objects for calling methods in the interface and thus no point of having constructor in it.
- If we try to create a constructor inside the interface, the compiler will give a compile-time error.
Java
// Java program that demonstrates why
// interface can not have a constructor
// Creating an interface
interface
Subtraction {
// Creating a method, by default
car is created Maruti is running0
car is created Maruti is running2
car is created Maruti is running3
car is created Maruti is running2
car is created Maruti is running5
car is created Maruti is running2
car is created Maruti is running7
car is created Maruti is running8
car is created Maruti is running9
// Java program that demonstrates why
0
// Java program that demonstrates why
1 // Java program that demonstrates why
2// Java program that demonstrates why
3 Subtraction {
// Java program that demonstrates why
6
// Java program that demonstrates why
8
car is created Maruti is running2
car is created Maruti is running3
car is created Maruti is running2
car is created Maruti is running5
car is created Maruti is running2
// interface can not have a constructor
4
// interface can not have a constructor
6
// interface can not have a constructor
7
car is created Maruti is running2
// interface can not have a constructor
9// interface can not have a constructor
7// Creating an interface
1 // Creating an interface
2
car is created Maruti is running8
// Creating an interface
6
// Java program that demonstrates why
8 // Creating an interface
9 interface
0 interface
1
// interface can not have a constructor
6
// interface can not have a constructor
7interface
5
// interface can not have a constructor
7interface
7
// interface can not have a constructor
7interface
9Subtraction {
0 Subtraction {
1
// interface can not have a constructor
7Subtraction {
3Subtraction {
4Subtraction {
5Subtraction {
6Subtraction {
7
car is created Maruti is running8
car is created Maruti is running8
Output
15
In the above program, we have created an interface Subtraction which defines a method subtract[], whose implementation is provided by the class GFG. In order to call a method, we need to create an object, since the methods inside the interface by default public abstract which means the method inside the interface doesn’t have the body. Therefore, there is no need for calling the method in the interface. Since we cannot call the methods in the interface, there is no need for creating the object for interface and there is no need of having a constructor in it.