Interfaces in Java

Interface Declarations in Java:
An interface describes fields and methods but does not implement them. An interface-
declaration may contain field descriptions, method descriptions, class declarations, and interface declarations, in any order.

interface-modifiers interface I extends-clause {
field-descriptions
method-descriptions
class-declarations
interface-declarations
}

An interface may be declared at top level or inside a class or interface but not inside a method or
constructor or initializer. At top level, the interface-modifiers may be public or absent. A public
interface is accessible also outside its package. Inside a class or interface, the interface-modifiers may be static (always implicitly understood) and at most one of public, protected, or private.

The extends-clause may be absent or have the form

extends I1, I2, ...

where I1, I2, ... is a nonempty list of interface names. If the extends-clause is present, then interface I describes all those members described by I1, I2,.., and interface I is a sub interface (and hence subtype) of I1, I2,.... Interface I can describe additional fields and methods but cannot override inherited members.

A field-description in an interface declares a named constant and must have the form
field-desc-modifiers type f = initializer;

where field-desc-modifiers is a list o f static, final, and public, none of which needs to be given
explicitly, as all are implicitly understood. The field initializer must be an expression involving only literals and operators, and static members of classes and interfaces.

A method-description for method m must have the form
method-desc-modifiers return-type m (formal-list) throws-clause;

where method-desc-modifiers is a list of abstract and public, both of which are understood and
need not be given explicitly.A class-declaration inside an interface is always implicitly static and public.

Classes Implementing Interfaces:
A class C may be declared to implement one or more interfaces by an implements-clause:

class C implements I1, I2, ...
class-body

In this case, C is a subtype I1, I2, and so on, and C must declare all the methods described by I1, I2,... with exactly the prescribed signatures and return types. A class may implement any number of interfaces. Fields, classes, and interfaces declared in I1, I2,... can be used in class C.

Three Interface Declarations:
The Colored interface describes method getColor, interface Drawable describes method draw, and Colored-Drawable describes both. The methods are implicitly public.

import java.awt.*;

interface Colored1 { Color getColor(); }

interface Drawable1 { void draw(Graphics g); }

interface ColoredDrawable extends Colored, Drawable {}


Classes Implementing Interfaces:
The methods getColor and draw must be public as in the interface declarations .

Ex1: class ColoredPaint extends Point implements Colored {
Color c;
ColoredPaint(int x, int y, Color c) {
super(x, y); this.c = c;
}

public Color getColor() {
return c;
}
}


Ex2: class ColoredDrawablePaint extends ColoredPaint implements ColoredDrawable {
Color c;

ColoredDrawablePaint(int x, int y, Color c) {
super(x, y, c);
}

public void draw(Graphics g) {
g.fillRect(x, y, 1, 1);
}
}


Ex3: class ColoredRectangle implements ColoredDrawable {

int x1, x2, y1, y2; // (x1, y1) upper left, (x2, y2) lower right corner
Color c;

ColoredRectangle(int x1, int y1, int x2, int y2, Color c){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.c = c; }

public Color getColor() {
return c;
}

public void draw(Graphics g) {
g.drawRect(x1, y1, x2-x1, y2-y1;
}
}

RELATED POST:

STATEMENTS IN JAVA

No comments:

Post a Comment