Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Can interfaces have constructors in Java?
No, interfaces can?t have constructors for the following reasons −
- All the members of an interface are abstract, and since a constructor cannot be abstract.
Still, if you try to write a constructor within an interface it will generate a compile time error.
Example
public interface InterfaceTest {
InterfaceTest(){
}
public abstract void display();
public abstract void show();
}
Error
C:\Sample>javac InterfaceTest.java
InterfaceTest.java:2: error: <dentifier> expected
public InterfaceTest(){
^
1 error
Advertisements
