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.
In the above program, we have created an interface Substraction which defines a method substract(), 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.
Why abstract classes have a constructor?
- The main purpose of the constructor is to initialize the newly created object. In abstract class, we have an instance variable, abstract methods, and non-abstract methods. We need to initialize the non-abstract methods and instance variables, therefore abstract classes have a constructor.
- Also, even if we don’t provide any constructor the compiler will add default constructor in an abstract class.
- An abstract class can be inherited by any number of sub-classes, thus functionality of constructor present in abstract class can be used by them.
- The constructor inside the abstract class can only be called during constructor chaining i.e. when we create an instance of sub-classes. This is also one of the reasons abstract class can have a constructor.
car is created Maruti is running
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Two interfaces with same methods having same signature but different return types
- Why can't static methods be abstract in Java?
- Why non-static variable cannot be referenced from a static method in Java
- Access specifiers for classes or interfaces in Java
- Abstract Classes in Java
- Messages, aggregation and abstract classes in OOPS
- Interfaces in Java
- Functional Interfaces In Java
- Interfaces and Inheritance in Java
- Interfaces in Java
- Callback using Interfaces in Java
- Private Methods in Java 9 Interfaces
- Difference between Abstract Class and Interface in Java
- Java | Abstract Class and Interface | Question 1
- Java | Abstract Class and Interface | Question 2
- Java | Abstract Class and Interface | Question 3
- abstract keyword in java
- Difference between Abstract Class and Concrete Class in Java
- Difference between Final and Abstract in Java
- Abstract Methods in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.