Sie sind auf Seite 1von 25

 An interface is a collection of abstract methods.

 A class implements an interface, thereby inheriting


the abstract methods of the interface.
 Writing an interface is similar to writing a class.

A class describes the attributes and behaviours of an object.


An interface contains behaviours that a class implements.

 Unless
the class that implements the interface is abstract, all
the methods of the interface need to be defined in the class.
 An interface can contain any number of methods.
 An interface is written in a file with a .java extension,
with the name of the interface matching the name of
the file.
 The bytecode of an interface appears in a .class file.
 Interfaces appear in packages, and their corresponding
bytecode file must be in a directory structure that
matches the package name.
 Cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only
fields that can appear in an interface must be
declared both static and final.
 An interface is not extended by a class; it is
implemented by a class.
 An interface can extend multiple interfaces.
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
Interfaces have the following properties:
 An interface is implicitly abstract. You do not need to
use the abstract keyword when declaring an
interface.
 Each method in an interface is also implicitly
abstract, so the abstract keyword is not needed.
 Methods in an interface are implicitly public if
interface itself is declared as public.
interface Callback {
void callback(int param);
}
The general form of a class that includes the implements
clause:

access class classname [extends superclass]


[implements interface [,interface...]] {
// class-body
}
 The methods that implement an interface must be
declared public.
 Also, the type signature of the implementing
method must match exactly the type signature
specified in the interface definition.

class Client implements Callback {


// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
}
 Classesthat implement interfaces can also define
additional members of their own.

class Client implements Callback {


// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.print(“Classes that implement interfaces”);
System.out.println(“may also define other members, too”);
}
}
class TestIface {
public static void main(String args[]) {
Callback c = new Client();
c.callback(42);
}
}
class AnotherClient implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("Another version of callback");
System.out.println("p squared is " + (p*p));
}
}

class TestIface2 {
public static void main(String args[]) {
Callback c = new Client();
AnotherClient ob = new AnotherClient();
c.callback(42);
c = ob; // c now refers to AnotherClient object
c.callback(42);
}
}
 Ifa class includes an interface but does not fully implement
the methods defined by that interface, then that class must
be declared as abstract
 Example:
abstract class Incomplete implements Callback {
int a, b;
void show() {
System.out.println(a + " " + b);
}
// ...
}
 An interface can be declared a member of a class or another
interface. Such an interface is called a member interface or
a nested interface.
 A nested interface can be declared as public, private, or
protected.
// A nested interface example.
// This class contains a member interface. class NestedIFDemo {
class A { public static void main(String args[]) {
// this is a nested interface // use a nested interface reference
public interface NestedIF { A.NestedIF nif = new B();
boolean isNotNegative(int x); if(nif.isNotNegative(10))
} System.out.println("10 is not negative");
} if(nif.isNotNegative(-12))
// B implements the nested interface. System.out.println("this won't be displayed");
class B implements A.NestedIF { }
public boolean isNotNegative(int x) { }
return x < 0 ? false : true;
}
}
 You can use interfaces to import shared constants into
multiple classes by simply declaring an interface that
contains variables that are initialized to the desired values.
 When you include that interface in a class, all of those
variable names will be in scope as constants.
 It is as if that class were importing the constant fields into
the class name space as final variables.
import java.util.Random; class AskMe implements SharedConstants {
interface SharedConstants { static void answer(int result) {
int NO = 0; switch(result) {
int YES = 1; case NO:
int MAYBE = 2; System.out.println("No");
int LATER = 3; break;
int SOON = 4; case YES:
int NEVER = 5;} System.out.println("Yes");
class Question implements SharedConstants { break;
Random rand = new Random(); case MAYBE:
int ask() { System.out.println("Maybe");
int prob = (int) (100 * rand.nextDouble()); break;
System.out.println("Random: "+prob); case LATER:
if (prob < 30) System.out.println("Later");
return NO; // 30% break;
else if (prob < 60) case SOON:
return YES; // 30% System.out.println("Soon");
else if (prob < 75) break;
return LATER; // 15% case NEVER:
else if (prob < 98) System.out.println("Never");
return SOON; // 13% break;
else }
return NEVER; // 2% } } }
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}
 One interface can inherit another by use of the keyword
extends
 The syntax is the same as for inheriting classes.
 When a class implements an interface that inherits another
interface, it must provide implementations for all methods
defined within the interface inheritance chain.
// One interface can extend another.
interface A {
void meth1();
void meth2(); class IFExtend {
} public static void main(String arg[]) {
// B now includes meth1() and meth2() -- it adds meth3(). MyClass ob = new MyClass();
interface B extends A { ob.meth1();
void meth3(); ob.meth2();
} ob.meth3();
// This class must implement all of A and B }
class MyClass implements B { }
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
 Design an interface named ‘stack’ with following methods:
 Push and pop elements from stack.
 Check whether the stack is empty or not.
Implement the stack with the help of arrays and if the size of the
array becomes too small to hold the elements, create new one. Test
this interface by inheriting it in its subclass StackTest.java
interface FamousLine {
void showline(); public class UseInterface {
} public static void main(String args[])
class Novel1 implements FamousLine { {
Novel1 hamlet = new Novel1();
public void showline() {
Novel2 juliet = new Novel2();
System.out.println(“to be or not to hamlet.showline();
be”); juliet.AuthorName();
} }
} }
class Novel2 implements FamousLine {
public void AuthorName() {
System.out.println(“Shakespeare”);
}
}
interface NewShape {
class ExtendInterface extends NewCircle
void draw(); {
} public static void main(String args[]) {
Circle nc = new NewCircle();
interface Circle extends NewShape {
nc.getRadius();
void getRadius(); }
int radius = 10; }
}
class NewCircle implements Circle {
public void getRadius() {
System.out.println(radius);
}
}
interface NewShape { class CastInterface {
void draw(); public static void main(String a[])
{
}
NewShape nc1 = new NewCircle1();
class NewCircle1 implements NewShape { NewShape nc2 = new NewCircle2();
public void draw() { nc1.draw();
System.out.println(“New Cirle 1 is drawn”); nc2.draw();
}
} }
}
class NewCircle2 {
public void draw() {
System.out.println(“New Circle 2 is drawn”);
}
}
interface NewShape {
void draw();
int radius = 10;
}

class NewCircle1 implements NewShape {


public void draw() {
radius = 12;
System.out.println(“Radius is : ” + radius);
}
}
class InterfaceVar {
public static void main(string args[]){
NewShape nc1 = new NewCircle1();
nc1.draw();
}
}

Das könnte Ihnen auch gefallen