Core Java:Interface interview question

1.What is an Interface?
In its most common form, an interface is a group of related methods with empty bodies.
An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default.

Note:

1) We can’t instantiate an interface in java.
2) Interface provides complete abstraction as none of its methods can have body. On the other hand, abstract class provides partial abstraction as it can have abstract and concrete(methods with body) methods both.
3) Implements keyword is used by classes to implement an interface.
4) While providing implementation in class of any method of an interface, it needs to be mentioned as public.
5) Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”.
6) Interface cannot be declared as private, protected or transient.
7) All the interface methods are by default abstract and public.
8) Variables declared in interface are public, static and final by default.
9) Interface variables must be initialized at the time of declaration otherwise compiler will through an error.
10) Inside any implementation class, you cannot change the variables declared in interface because by default, they are public, static and final. Here we are implementing the interface “Try” which has a variable x. When we tried to set the value for variable x we got compilation error as the variable x is public static final by default and final variables cannot be re-initialized.
12) A class can implements any number of interfaces.
13) If there are having two or more same methods in two interfaces and a class implements both interfaces, implementation of one method is enough.

2. Can we instantiate an interface?
We can’t instantiate an interface directly, but we can instantiate a class that implements an interface.

3. Can we create an object for an interface?

We cannot create an object for interface directly, because method implementation is needed for object creation .
Still there are some ways through which object of interface can be created.

1.By using anonymous inner classes:-
public interface MyInterface {
public void myMethod() ;
}

MyInterface myIntfObj = new MyInterface() {
public void myMethod() {
….
}
};

myIntfObj.myMethod();
2.
it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.

4. Do interfaces have member variables?
Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables.

5. What modifiers are allowed for methods in an Interface?
Only public and abstract modifiers are allowed for methods in interfaces.

6. What is a marker interface?
Marker interfaces are those which do not declare any required methods, marker interface must be empty, but implementing it is meant to imply some special treatment of the implementing class. Thejava.io.Serializable interface and Cloneable are typical marker interfaces. These do not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.
7. Can you declare an interface method static?
No, because methods of an interface are abstract by default, and static and abstract keywords can’t be used together.

8. Can an Interface be final?
No, because its implementation is provided by another class.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.