- 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 interface members as private or protected in java8?
Interface in Java is similar to a 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 in it.
Private members of an interface
If the members of the interface are private you cannot provide implementation to the methods or, cannot access the fields of it in the implementing class.
Therefore, the members of an interface cannot be private. If you try to declare the members of an interface private, a compile-time error is generated saying “modifier private not allowed here”.
Example
In the following Java example, we are trying to declare the field and method of an interface private.
public interface MyInterface { private static final int num = 10; private abstract void demo(); }
Compile time error
On compiling, the above program generates the following error.
Output
MyInterface.java:2: error: modifier private not allowed here private static final int num = 10; ^ MyInterface.java:3: error: modifier private not allowed here private abstract void demo(); ^ 2 errors
Protected members of an interface
In general, the protected members can be accessed in the same class or, the class inheriting it. But, we do not inherit an interface we will implement it.
Therefore, the members of an interface cannot be protected. If you try to declare the members of an interface protected, a compile-time error is generated saying “modifier protected not allowed here”.
Example
In the following Java example, we are trying to declare the field and method of an interface protected.
public interface MyInterface{ protected static final int num = 10; protected abstract void demo(); }
Compile-time error
On compiling, the above program generates the following error.
Output
MyInterface.java:2: error: modifier protected not allowed here protected static final int num = 10; ^ MyInterface.java:3: error: modifier protected not allowed here protected abstract void demo(); ^ 2 errors
- Related Articles
- Can we declare the variables of a Java interface private and protected?
- Can we declare a top level class as protected or private in Java?\n
- Can we declare main() method as private or protected or with no access modifier in java?
- Can we declare an abstract method, private, protected, public or default in java?
- Private and Protected Members in C++
- Can we declare a constructor as private in Java?
- Can we declare an interface as final in java?
- Can we declare a main method as private in Java?\n
- Can we declare an interface with in another interface in java?
- Can we have a private method or private static method in an interface in Java 9?\n
- Can we use private methods in an interface in Java 9?
- 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 main() method as Non-Static in java?
