Gadgets

Thursday, 18 February 2016

Interface interview questions

Generally meaning of interface is mediator between two objects to communicate each other.
Interface: Any service requirement specification (or) any contract between client and service provider (or) 100% pure abstract class is known as interface.

1) What modifiers are applicable for methods and variables inside interface?
Every interface methods are declared with public abstract modifiers and variables are declared with public static final modifiers.
            public static final int a=10; //varaible
            public abstract void display(); //method

2) Can java class implements any number of interfaces?
A java class can implements any number of interfaces.
public class Test implements Inter1,Interf2
{
} 
3) If two interfaces contain same method with same return type and signature, what will happen?
 If two interfaces contain same method with same return type and signature then one implementation is enough in implementation class.

4) What is abstract class?
Abstract class contains abstract methods and concrete, abstract class is partial implementation.  
abstract class Test 
{
    public abstract void add();
     void display(){
      System.out.println("implement");
      }
}
 
5) What is the difference between interface and abstract?
interface
abstract
Interface contain only abstract methods
Abstract class contain abstract method and concrete methods
Every method is public abstract
 No restriction in abstract class
Every variable public static final
No restriction in abstract class
Inside interface every variable should be initialize
No need to initialize
We can’t declare static and instance block inside interface
We can declare static and instance block inside abstract class
We can’t declare constructor
We can declare constructor

6) What the difference between extends and implements?
extends keyword is used to extend the class, implements is used to implements the interface.
A class extends only one class at a time.
class Test extends Test1
{
} 
A class can implement any number of interfaces simultaneously.
class Test implements Inter1,Interf2,Interf3
{
} 
A class can extend one class and implement any number of interfaces simultaneously.
class Test extends Test1 implements Inter1,Interf2
{
} 
An Interface can extend any number of interfaces simultaneously.
interface Interf extends Inter1,Inter2,Inter3
{
} 
7) What is the marker interface?
Marker interface doesn’t contain any methods and by implementing marker interface our object will get ability to perform action.
Example: Serializable, Cloneable, RandomAccess etc. 

8) What is the adapter class?
Adapter class is a simple java class that implements an interface with empty implementation. By using it reduce the code.
abstract class AdapterTest implements InterTest
{
method1(){ }
method2(){ }
method3(){ }
} 

No comments:

Post a Comment