- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we declare an interface as final in java?
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.
Making an interface final.
If you declare a class final cannot extend it. If you make a method final you cannot override it and, if you make a variable final you cannot modify it. i.e. use final with Java entities you cannot modify them further.
If you make an interface final, you cannot implement its methods which defies the very purpose of the interfaces. Therefore, you cannot make an interface final in Java. Still if you try to do so, a compile time exception is generated saying “illegal combination of modifiers − interface and final”.
Example
In the following example we are defining an interface with name MyInterface and using the final modifier with it.
public final interface MyInterface{ public static final int num = 10; public abstract void demo(); }
Compile time error
On compiling, the above program generates the following error
Output
MyInterface.java:1: error: illegal combination of modifiers: interface and final public final interface MyInterface{ ^ 1 error
- Related Articles
- Can we declare the method of an Interface final in java?
- Can we declare constructor as final in java?
- Can we declare the main () method as final in Java?
- Can we declare an interface with in another interface in java?
- Can we declare an abstract method final or static in java?
- Can we declare final variables without initialization in java?
- Can we declare interface members as private or protected in java8?
- Can we declare a constructor as private in Java?
- Can we declare the variables of a Java interface private and protected?
- Can We declare main() method as Non-Static in java?
- Can we declare a main method as private in Java?\n
- Can we create an object for an interface in java?
- Can we overload methods of an interface in Java?
- Can we define constructor inside an interface in java?
- Can we write an interface without any methods in java?
