Java Programming - Declarations and Access Control - Discussion


Which two of the following are legal declarations for nonnested classes and interfaces?

  1. final abstract class Test {}
  2. public static interface Test {}
  3. final public class Test {}
  4. protected abstract class Test {}
  5. protected interface Test {}
  6. abstract public class Test {}

[A]. 1 and 4
[B]. 2 and 5
[C]. 3 and 6
[D]. 4 and 6

Answer: Option C


(3), (6). Both are legal class declarations.

(1) is wrong because a class cannot be abstract and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.

Kapil said: (Dec 30, 2010)  
Class(NonNested) can be public,default,final or abstract.

But class can't marked as both fianl and abstract

Amit said: (Feb 12, 2014)  
If interface is marked as static then there would be no meaning of using interface. It is meant to be implemented by many classes or interfaces. If we use "static" then there will be only one copy of it which we don't want in anyway!

Ivan said: (Aug 19, 2014)  
Final can not be used with the combination interface, its gives an error saying "illegal combination of modifiers".

Rishi Pathak said: (Nov 26, 2014)  
This wrong! Interface can't be final. Suppose I Say Final Interface :-).

Then we will not be able to Provide body to the method declared in Interface. This will lead to uselessness of interface.

So Java Compiler will say WTF! This Is An "illegal combination of modifiers".

Aayush said: (Jun 22, 2015)  
Please clear about (4) option. How class can't be protected?

Agata said: (Jan 28, 2018)  
public static interface Test {}

This code will compile. Answer number (2) should be valid, although I have no idea how to use such construction.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.