Pages

Friday, 20 September 2013

Core Java - Interfaces (FAQ's)




1.    What are interfaces ?

Interface is nothing but Service requirement specification.

If you are client ==> Interface for you is the set of services you are expecting from service provider.
If you are Service provider ==> Interface for you is the set of services you are providing to the client.

Interfaces provided by sun people are:
JAI API (Java Advance Imaging)
Set of interfaces which supports high level programming model allowing to manipulate images.

JMF API (Java Media Framework)
An API that enables audio, video and other time-based media to be added to Java applications and applets.

JDBC API      (Java Database Connectivity)
An API that acts as an requirement specification to develop database driver. This API is provided by sun but implementing these specification is a responsibility of database vendor.
Example: providing Oracle driver for JDBC connection is responsibility of Oracle. Similarly for DB2 driver, is responsibility of IBM, etc.

Servlet API
An API that acts as an requirement specification to develop Webserver.
Sun people provided the Servlet API but implementing this API is responsibility of Webserver vendor.
Example: Websphere server, Tomcat Server, Weblogic Server, etc.

Conclusion: Interfaces are service specification requirements and acts as contract between Service provider & client in a different way.




2.    Why interfaces are called Service requirement specifications only ?

All methods in Interfaces are defined as abstract, it provides only service requirement specification but not implementation. hence it is also called as complete or 100% pure abstract class.




3.    Explain how interfaces are declared and used with example ?

Interfaces I
{
      public abstract void m1();
      public abstract void m2();
      abstract void m3();
      public void m4();
      void m5();
}

In above interface declaration, we have to make a note of few rules:
1.   In any interface all methods should be public & abstract
      public - to make the declared method accessible to every implementation class.
      abstract - implementation of method is responsibility of implementation class.
2.   Class can implement any number of interfaces
      ImplementationClass implements I, J, K                 <== Here I, J, K are interfaces
              {
                   m1() {  -----  }                               <== Implementation of all methods in implementation class
                   m2() {  -----  }
                   m3() {  -----  }
                   m4() {  -----  }
                   m5() {  -----  }
                   -----
                   -----
              }
3.   Class can extend only one class & can implement any number of interfaces.
      ImplementationClass extends childClass implements I, J, K                 <== Here I, J, K are interfaces
              {
                   m1() {  -----  }                               <== Implementation of all methods in implementation class
                   m2() {  -----  }
                   m3() {  -----  }
                   m4() {  -----  }
                   m5() {  -----  }
              }

4.    Interface can extend any no. of interfaces at a time.
        I1 extends I, J, K                 <== Here I1, I, J, K are interfaces
   
Conclusion:
Interface can extend any number of interfaces at a time.
Class can extend only class & can implement any number of interfaces at a time.




4.    Can we declare variables inside interface ?

Yes, variables inside interface are used to specify requirement level constants.
variable declaration should be -
interface I
{
    public static final int v1=23;
    ------
    ------
}

Here v1 is interface variable.
If we declare v1 without public static final, still we will not get any compilation error. But v1 declaration will be assumed as public static final only.
public - To make this variable available to every implementation class.
static  - To make this variable accessible to implementation class even without creating an object.
final    - Not to allow implementation classes to modify the value of interface variables.




5.    How we can achieve multiple inheritance using interfaces ?


Interface I
  {
     public abstract void getBalance();
  }


Interface J
  {
     public abstract void getBalance();
  }


In above 2 interfaces I & J, we have a similar method called getBalance().

Lets implement these 2 interfaces in class test.

Class test implements I, J
  {
       public void getBalance()                  <== As both interfaces contain same method with same return type                                                                      & same signature, one method implementation is enough.
           {
                -----
                -----
            }
  }

Note:
If same method has different return type and same signature in 2 interfaces then it is impossible to implement both interfaces in any java class with that method implementation in implementation class.
If same method has different signature in 2 interfaces then in implementation class we have to implement both methods with declared signatures & they will act as overloaded methods.




6.    What is Marker Interface ?

An interface which has no methods defined but still our object gets some ability by implementing this interface are called Marker Interfaces.

Example:
1.  Cloneable -    By implementing this marker interface our object can produce exactly similar object.
2.  Serializable -  By implementing this marker interface our object can travel across the network & can be saved in file.
etc.

These interfaces are called marker interfaces because all these interfaces are marked for some ability.


Marker interfaces don't have any method defined then how they get ability ????





7.    What is adapter class ?

Adapter class is more or less like normal java class that implements an interface with only empty implementations i.e. all interface methods are provided in a class without providing implementation.

Problem in implementing interface directly:

  • If we implement interface directly, we have to provide implementation for all methods available in interface irrespective of requirement or not.
  • Due to this programmer complexity will increase, code length will increase and readability of code will decrease.

To overcome above problem, we can use adapter class which is extending interface with all methods without implementing them. Then instead of implementing interface we can extend adapter class, only with implementation of methods required for our functionality.

Example:
Generic servlet acts as adpater class for servlet interface.




8.    Can we declare variables inside interface ?

Yes, variable inside interface are used for service level constants only.

Example:
int v = 100;                 <--    valid declaration 
public int v = 100;          <--    valid declaration 
public static int v = 100;   <--    valid declaration 
protected v;                 <--  Invalid declaration
private v;                   <--  Invalid declaration
volatile v;                  <--  Invalid declaration
transient v;                 <--  Invalid declaration


Note:

  • Variables inside interface are always considered as public static only whether you provide or not.
  • All combinations which are invalid with public & static modifiers are not allowed.
  • Interface variables should be initialized while declaration, otherwise compile time error will occur.
  • Any class which is implementing interface cannot change the value of interface variable (declared as final).





9.    What are the differences between Abstract class & Interface ?









































More Java Interview questions:

Java.lang Package      |       OOPS concepts      |       Exception handling      |       Access Modifiers 






2 comments:

Tomáš Zilvar said...

Is it just me, or are the descriptions in the image in section 9 the other way around? The green column should be labeled Interface and the purple one Abstract class, right?

Hadoop Guru said...

Thank you ZiziTheFirst. I made the changes !!