Difference between revisions of "Interfaces"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 1: Line 1:
 +
== Intro ==
 +
 +
* An interface defines a contract for what a class can do without specifying how.
 +
 +
== Declaration ==
 +
 
* All interface methods are implicitly '''public and abstract'''
 
* All interface methods are implicitly '''public and abstract'''
 
* Even if some modifiers are left out, the above modifiers will be applied.
 
* Even if some modifiers are left out, the above modifiers will be applied.

Revision as of 01:03, 22 August 2011

Intro

  • An interface defines a contract for what a class can do without specifying how.

Declaration

  • All interface methods are implicitly public and abstract
  • Even if some modifiers are left out, the above modifiers will be applied.
  • Since they have to be overriden, methods cannot be static.
  • Similarly they cannot be final, strictfp, or native.
  • All variables are implicitly public, static and final
  • Even if some modifiers are left out, the above modifiers will be applied.
  • Since variables are final, they must be initialized
  • If no access modifier is specified, the member is public, not package !
  • Similarly, specifying protected/private is not allowed for methods and variables
  • Watch out when classes implement interfaces and don't specify the method as public - this wont compile.
  • Similarly, watch out when classes try to reassign the value of an interface.
  • Note, the interface itself can be public OR package accessible, but members get default public access as described above.
public interface Mashable {
   public int COUNT; //illegal not initialized !
   int MINCOUNT = 3; //this is public, static and final implcitly.
   public static final int MAXCOUNT=10; //full correct declaration

   void foo(); //Implicity: public abstract void foo();
   static void foo(); //Illegal;
}