Difference between revisions of "Interfaces"
From Suhrid.net Wiki
Jump to navigationJump to searchLine 20: | Line 20: | ||
* Note, the interface ''itself'' can be public OR package accessible, but members get default public access as described above. | * Note, the interface ''itself'' can be public OR package accessible, but members get default public access as described above. | ||
+ | * If the interface is package protected, then a class outside its package cannot access it. | ||
<syntaxhighlight lang="java5"> | <syntaxhighlight lang="java5"> | ||
Line 34: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | * An interface can '''extend''' NOT implement one or more interfaces. | + | * An interface can '''extend''' (NOT implement) one or more interfaces. |
− | |||
[[Category:OCPJP]] | [[Category:OCPJP]] |
Revision as of 01:15, 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.
- If the interface is package protected, then a class outside its package cannot access it.
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;
}
- An interface can extend (NOT implement) one or more interfaces.