In this article, we will cover some of the interview questions with their justification on Java interface
These are most frequently asked interview question from OOPS concepts
Note: All justification done w.r.t both version i.e.;
- Till Java 7 version
- Post Java 8 release
Q) What is Interface in Java? OR Explain Interface in Java ?
Till Java 7 version,
- An interface in java is a pure abstract class which means all methods are abstract and variables are constants
- By default, all methods inside interface are public & abstract and variables are public, static & final
- Interface is a mean to achieve full abstraction in Java
Post Java 8 release,
- Interface can contain default and static methods, in addition to abstract methods
- Though it looks to similar to abstract classes but they are actually different in many ways
- Read more about Java 8 default and static methods in Interface in detail
Q) What type of variables can be defined in an interface ?
Be default, any variables declared inside interface is,
- public
- static
- final
Note: even when not declared explicitly inside interface
Which means these implicit modifier make all variables inside interface is CONSTANT
Q) Can we instantiate an interface ?
- No, we cannot instantiate interface
- Since, interface doesn’t have constructor and contains only abstract methods and constants therefore we don’t need to instantiate
- Instead implementing classes provide concrete implementation for these abstract methods and constants can be accessed using <interfaceName>.<variableName>
Post Java 8 release,
- In addition to abstract methods and constants, Java 8 introduced default and static methods
- default methods can be accessed using implementing class’s reference object
- static methods can be accessed using interface name i.e.; <interfaceName>.<staticMethodName>
- Read more about Java 8 default and static methods in Interface in detail
Q) Can we create an object for an interface ?
- No, we cannot create an object of an interface
Q) What happens, if we define concrete method inside Interface ?
Till Java 7 version,
- Compilation error will be thrown stating below reason
- Compile-time error: Abstract methods do not specify a body
Post Java 8 release,
- Concrete method (method with body) are allowed with default or static keyword prefixed, as shown below
- Otherwise, even in Java 8 compilation error will be thrown as seen below screen capture
Q) Can a method inside an interface be declared as final ?
- By default, methods declared inside interface are public & abstract even if we don’t declare it explicitly compiler adds these modifier during compilation time
- Interface allows only public & abstract modifiers in method declaration
- If final keyword added in method declaration then compiler will throw error as seen in the below screen capture
- Compile-time error: Illegal modifier for the interface method display; only public & abstract are permitted
Post Java 8 release,
- Still, final modifier is not allowed in any of the methods in interface i.e.; abstract, default & static methods
- Compile-time error: Illegal modifier for the interface method display; only public, abstract, default, static and strictfp are permitted
Q) What happens, if we don’t initialize variables inside Interface ?
- Compiler throws error stating final variable needs to be initialized
- As variables defined inside interface are by default public, static & final. So, final variable always needs to be initialized where it is declared
- Compile-time error: The blank final field <fieldname> may not have been initialized
- No change even post Java 8 release
Q) Can we declare members as private or protected modifier inside Interface ?
- Variables (members) defined inside interface are by default public, static & final
- Therefore, no other access-modifier allowed except public
- During compilation process, compiler inserts/adds public, static & final keyword for variables
- These are interface variables and are accessed using interface name
- For example, <interfaceName>.<memberName> from any other class
- No change even post Java 8 release too
Q) How can we access variables defined inside Interface ?
- Members defined inside interface can be accessed using interface name from any other class
- For example, <interfaceName>.<memberName> from any other class
- No change even post Java 8 release too
Q) Can we modify variables defined inside Interface ?
- Since, variables defined inside interface are final therefore we cannot change the value of these variables anywhere (simple OOPS concept)
- If we try to change the value, then compiler throws error
- Compile-time error: The final field <interfaceName>.<fieldName> cannot be assigned
- No change even post Java 8 release too
Q) Can we re-assign a value to a field of interface ?
- Re-assigning fields throws compile-time error as these are final by default
- Compile-time error: The final field <interfaceName>.<fieldName> cannot be assigned
Q) What modifiers are allowed for methods in an interface ?
- Till Java 7 version, only public & abstract are permitted
- Post Java 8 release, only public, abstract, default, static and strictfp are permitted
Q) Is it ok to add “abstract” keyword to interface definition ?
- Yes, we can actually add abstract keyword to interface definition (somewhat similar to abstract classes)
Q) Whether class compiles successfully, if we don’t implement any of the abstract methods from Interface ?
- No, compilation error will be thrown
- If a class implements any interface then it must provide definition or concrete implementation for every abstract method
Post Java 8 release,
- Still, implementing class must provide definition or concrete implementation for every abstract method in interface
- Exceptional being default and static methods; it is okay if we don’t override default method
- Read more about Java 8 default and static methods in Interface in detail
Q) What is the best possible solution, if we don’t want to implement few of the abstract methods from Interface ?
- The best solution is to declare the implementing class as abstract; compilation will succeed
- But next inheriting class (i.e.; extending this class) must provide concrete method implementation or declare again as abstract
Q) Can we reduce the visibility of the methods while implementing interface ?
- By default abstract methods declared inside interface are public
- As per overriding rule, access visibility of the methods can be widened further
- So, it is must to declare overriding methods as public; as no other access visibility is more wider than public
- Read more about Java overriding rules here
Q) Can we declare constructor inside interface ?
- Compilation error will be thrown stating “Interfaces cannot have constructors”
- Compile-time error: Interfaces cannot have constructors
Q) Can interface be final ?
- No, interface cannot be final and compilation error will be thrown
- Compile-time error: Illegal modifier for the interface <interfaceName>; only public and abstract are permitted
Q) Can interface extend any class ?
- No, interface cannot extend any class
- Interface can only extend one or more other interfaces
Q) Can an interface implement any other interface ?
- No, interface cannot implement other interface
- Interface can only extend one or more other interfaces
Q) Can an interface extend another interface ?
- Yes, an interface can extend one or more interfaces
Q) What is marker interface or tagged Interface ?
- An interface with no fields or methods is known as marker interface
- Marker interface are used to indicate or provide essential information to JVM or compiler
- Alternatively, it is referred as tagged interface
- java.io.Serializable or java.lang.Cloneable are example of marker or tagged interface
- Marker interface improves readability in comparison with any other alternatives
Q) Can an interface contain another interface as member ?
- Yes, an interface can contain another interface
- This is referred as Nested interface
Q) What is Nested Interface in Java ?
- An interface declaration contained inside another class or interface is known as Nested interface
- During compilation, compiler inserts/adds static keyword to nested interface
DemoInterfaceA.java
package in.bench.resources.itf.example;
public interface DemoInterfaceA {
String NAME = "BenchResources.Net";
// Nested interface inside another interface
interface NextedItfExample {
}
}
Q) What if the difference between abstract class and interface ?
- Below table shows some of the differences between Abstract Class & Interface
- Read this article for more information with detailed explanation and exmaples
Sr. No. | Abstract Classes | Interface |
1 | Contains members variables | All variables are actually constants |
2 | It can have constructors | Interface cannot have constructors |
3 | Can hold state of an object using instance member variables | Since, all variables are static and final therefore no concept of holding state of an object |
4 | Forces to implement abstract methods or else declare class as abstract | default methods can be overridden, if required but never forces |
Related Articles:
- Java Constructor with example
- Default constructor
- Parametrized constructor
- Default constructor v/s Parametrized constructor
- Constructor overloading
- Constructor chaining
- Constructor v/s Methods
- Private Constructor
- Static constructor in Java – A big interview question ?
References:
- http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html
- http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
- http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
- http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html#static
- https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
Happy Coding !!
Happy Learning !!