Monday, October 22, 2012

Java Interfaces Elucidated


Interfaces can be used to implement the Inheritance relationship between the nonrelated
classes that do not belongs to the same hierarchy, i.e. any Class and anywhere in
hierarchy. Using Interface, you can specify what a class must do but not how it does.

  • A class can implement more than one Interface.
  • An Interface can extend one or more interfaces, by using the keyword extends.
  • All the data members in the interface are public, static and Final by default.
  • An Interface method can have only Public, default and Abstract modifiers.
  • An Interface is loaded in memory only when it is needed for the first time.
  • A Class, which implements an Interface, needs to provide the implementation of all the methods in that Interface.
  • If the Implementation for all the methods declared in the Interface are not provided , the class itself has to declare abstract, otherwise the Class will not compile.
  • If a class Implements two interface and both the Intfs have identical method declaration, it is totally valid.
  • If a class implements two interfaces both have identical method name and argument list, but different return types, the code will not compile.
  • An Interface can’t be instantiated. Interfaces are designed to support dynamic method resolution at run time.
  • An interface cannot be native, static, synchronize, final, protected or private.
  • The Interface fields can’t be private or protected.
  • A Transient variables and Volatile variables cannot be members of Interface.
  • The extends keyword should not be used after the Implements keyword, the Extends must always come before the Implements keyword.
  • A top level Interface cannot be declared as static or final.
  • If an Interface species an exception list for a method, then the class implementing the interface need not declare the method with the exception list.
  • If an Interface can’t specify an exception list for a method, the class can’t throw an exception.
  • If an Interface does not specify the exception list for a method, the class cannot throw any exception list.


The general form of Interface is
Access interface name {
return-type method-name1(parameter-list);
type final-varname1=value;
}

Marker Interfaces : Serializable, Clonable, Remote, EventListener,
Java.lang is the Package of all classes and is automatically imported into all Java Program
Interfaces: Clonable , Comparable, Runnable

No comments:

Post a Comment