Interfaces Interview Questions
Interfaces Interview Questions
codespaghetti.com/interfaces-interview-questions/
Interface
Java Interface Interview Questions, Programs and Examples.
1/7
How To Describe Interfaces In Interviews?
An interface is a reference type in Java. It is similar to class. It is a collection of
abstract methods.
An interface cannot be instantiate.
An interface does not contain any constructors.
An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
An interface is not extended by a class; it is implemented by a class.
An interface can extend multiple interfaces.
Along with abstract methods, an interface may also contain constants, default methods,
static methods, and nested types. Method bodies exist only for default methods and static
methods.
Writing an interface is similar to writing a class. But a class describes the attributes and
behaviors of an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface
need to be defined in the class.
2/7
Question: What will happen if we define a concrete method in an interface in
Java?
if we declare any concrete method in an interface compile time error will come.
No.We can not create non static variables in an interface. If we try to create non static
variables compile time error comes.
By default members will be treated as public static final variables so it expects some value
to be initialized.
package codespaghetti.com;
interface JavaInterface{
Compile time error will come because by default members will be treated as public static
final variables so it expects some value to be initialized.
package codespaghetti.com;
interface JavaInterface{
int x, y; // compile time error: The blank final field y may not have been
initialized
}
No.
3/7
package codespaghetti.com;
interface JavaInterface{
private int x; // compile time error: Illegal modifier for the interface field
Sample.x; only
public, static & final are permitted
protected int a; // compile time error: Illegal modifier for the interface field
Sample.a; only
public, static & final are permitted
}
A class will implements an interface. A class will extends another class. An interface
extends another interface.
NO. We can not create object for interface. We can create a variable for an interface
package codespaghetti.com;
interface JavaInterface{
void show();
package com.instanceofjava;
interface A implements JavaInterface {
void show(){
// code
}
public static void main(String args[]){
}
}
No. Compile time error will come. Error: Illegal modifier for the interface Sample; only public
& abstract are permitted
4/7
No. Interfaces does not allow constructors.The variables inside interfaces are static final
variables means constants and we can not create object for interface.
So there is no need of constructor in interface that is the reason interface doesn't allow us
to create constructor.
Question: Question : What will happen if we are not implementing all the
methods of an interface in class which implements an interface?
A class which implements an interface should implement all the methods (abstract)
otherwise compiler will throw an error. The type Example must implement the inherited
abstract method JavaInterface.show() If we declare class as abstract no need to implement
methods. No need of overriding default and static methods.
package codespaghetti.com;
interface JavaInterface{
void show();
}
package com.instanceofjava;
interface A implements JavaInterface { // The type Example must implement the
inherited
abstract method JavaInterface.show()
}
}
Yes, implementing the method once is enough in class. A class cannot implement two
interfaces that have methods with same name but different return type.
No. The fields of interfaces are static and final by default. They are just like constants. You
can’t change their value once they got.
5/7
Question: Can we declare an Interface with “abstract” keyword?
Yes, we can declare an interface with “abstract” keyword. But, there is no need to write like
that. All interfaces in java are abstract by default.
Question: For every Interface in java, .class file will be generated after
compilation. True or false?
True, .class file will be generated for every interface after compilation.
No. While overriding any interface methods, we should use public only. Because, all
interface methods are public by default and you should not reduce the visibility while
overriding them.
No. You can’t define interfaces as local members of methods like local inner classes. They
can be a part of top level class or interface.
No, a class can not become super interface to any interface. Super interface must be an
interface. That means, interfaces don’t extend classes but can extend other interfaces.
Question: Like classes, does interfaces also extend Object class by default?
No. Interfaces can’t have static methods. Interfaces can have static methods since Java
1.8.
Advantages
Java interfaces are slower and more limited than other ones.
Interface should be used multiple number of times else there is hardly any use of
having them.
References:
https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
http://stackoverflow.com/questions/18777989/how-should-i-have-explained-the-
difference-between-an-interface-and-an-abstract
http://stackoverflow.com/questions/3355408/explaining-interfaces-to-students
http://www.codespaghetti.com/inheritance-interview-questions
7/7