Java: Constructor in Interface – A tricky question post Java 8 release

In this article, we will discuss whether defining constructor in Java Interface are valid or not i.e.; whether compilation succeeds or not

But to discuss constructor in Java Interface, we need to split it into 2 parts to understand in detail

  • Constructor in Java Interface till Java 7
  • Java Interface post Java 8 release

Here, in this article we will discuss briefly on defining constructors in Interface till Java 7 versions i.e. Java 5 or Java 6 or Java 7 (assuming earlier versions are not used these days)

Later, in the upcoming article we will individually discuss on constructor in Interface for Java 8 version, as to understand why Java 8 added new feature with example

 

Constructor in Java Interface till Java 7 version

Defining constructor inside Java Interface is not allowed; whether it is default constructor or parametrized constructor

 

What happens, if we define constructor inside Java Interface?

Compile-time error is thrown by compiler as shown in the below screen capture

Compile-time error: Interfaces cannot have constructors

1_constructor_in_java_7_interface_compilation_error

 

But why does Java doesn’t allow defining constructor inside Interface?

Before we discuss why Java doesn’t support constructor in Interface, we will look through the key points about Java Interface

Key points about Java Interface,

  • By default, variables defined inside Java Interface are “public static final” –> means constants
  • These variables needs to be initialized and cannot be modified as these are marked with “final” keyword
  • If it’s not initialized, compile-time error will be thrown
  • By default, methods are “public abstract” even if we don’t explicitly mark with abstract keyword
  • By abstract method means, it doesn’t have method body (i.e.; no concrete implementation for methods)
  • These abstract interface methods are implemented by inheriting classes giving definition to them
  • Static methods are not allowed
  • If we try to add static keyword to abstract methods, compiler throws errors
  • Even at Interface declaration, only public and abstract modifiers are allowed

Java Interface are more of contract exposing agreement, by which we are achieving one of the core concepts of OOPS i.e.; abstraction

 

Abstraction: exhibiting only relevant detail to the world and hiding important implementation detail

 

Back again to discuss our primary question, why Java doesn’t allow defining constructor inside Interface?

Generally, we will create new instance of class

  • To hold specific instance values using member variables (i.e.; state of an object)
  • To access member methods using this newly created object

But in Java Interface,

  • All variables are static and thus belongs to class, so there is no point of holding specific instance values
  • In addition to this, objects are used to invoke member methods but methods here are abstract and its implementation is given by implementing/inheriting/extending classes
  • So, there is no need to invoke these abstract methods

As we know, constructor comes in picture only at the time of creating new objects

Since there is no need of creating objects and thus constructor are not required in Java Interface

Note: Interface are used as reference variable type in inheritance

 

Compilation error with respect to Java Interface

 

1. What happens, when we don’t initialize any values to variables in Java Interface?

Compile-time error will be thrown

Compile-time error: The blank final field strParameter may not have been initialized

2_constructor_in_java_7_interface_compilation_error_for_variables

 

2. What happens, when we add static keyword to methods declared in Java Interface?

Compile-time error will be thrown

Compile-time error: Illegal modifier for the interface method display; only public & abstract are permitted

3_constructor_in_java_7_interface_compilation_error_for_static_methods

 

Conclusion: We have seen, constructors are not allowed in Interface till Java 7 version. But with the introduction of Java 8, some new features are added

And one such feature is “default method” and “static method” in Java Interface

In the next article, we will see what is default & static methods in Interface and will clear out the confusion regarding defining constructor inside Interface

 

References:

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

 

Read Also:

Happy Coding !!
Happy Learning !!