11-08-2012, 04:35 PM
Interfaces
Interfaces.ppt (Size: 190 KB / Downloads: 23)
Introduction
Enhance Java’s single inheritance model
Single inheritance model a better model
class : fundamental unit of programming in Java.
fundamental unit of OOD is type
while classes define type , very useful and powerful to be able to define a type without defining a class
With interface separate class interface with its implementation
An interface is a contract.
When you create an interface, you're defining a contract for what a class can do,without saying anything about how the class will do it.
Think of an interface as a 100-percent abstract class.
But while an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods.
Rules
All interface methods are implicitly public and abstract.
need not actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract.
All variables defined in an interface must be public, static, and final— interfaces can declare only constants, not instance variables.
Example
Following five method declarations, if declared within their own interfaces, are legal and identical.
void bounce();
public void bounce();
abstract void bounce();
public abstract void bounce();
abstract public void bounce();
Declaring Interface Constants
put constants in an interface.
Any class implementing the interface will have access to the same constant.
Key rule : They must always be public static final.
Because interface constants are defined in an interface, they don't have to be declared as public, static, or final.
They must be public, static, and final.