interface in JAVA

 Defining an interface




• Using interface, we specify what a class must do, but not how it does this.

• An interface is syntactically similar to a class, but it lacks instance variables and its methods are declared without any body.

• An interface is defined with an interface keyword.

 An interface declaration consists of modifiers, the keyword interface,the  interface name, a comma-separated list of parent interfaces (if any), and the interface body.

For example:

public interface GroupedInterface extends Interface1, Interface2, Interface3 {
// constant declarations double E = 2.718282;
// base of natural logarithms //
//method signatures
void doSomething (int i, double x);
int doSomethingElse(String s);
}


 The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.

 An interface can extend other interfaces, just as a class can extend or subclass another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends


Implementing interface


General format:
access interface name {
type method-name1(parameter-list);
type method-name2(parameter-list);
...
type var-name1 = value1;
type var-nameM = valueM;

...
}


• Two types of access:

1) public – interface may be used anywhere in a program
2) default – interface may be used in the current package only

• Interface methods have no bodies – they end with the  semicolon after the parameter list.

• They are essentially abstract methods.

• An interface may include variables, but they must be final,
static and initialized with a constant value.

• In a public interface, all members are implicitly public.

• A class implements an interface if it provides a complete set
of methods defined by this interface.

1) any number of classes may implement an interface

2) one class may implement any number of interfaces

• Each class is free to determine the details of its implementation.

• Implementation relation is written with the implements keyword.


• General format of a class that includes the implements clause:

 Syntax:
access class name extends super-class implements interface1,
interface2, ..., interfaceN {

...
}

• Access is public or default.


* An implementing class may also declare its own methods:

class Client implements Callback {
public void callback(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that implement “ +
“interfaces may also define ” +
“other members, too.");
}
}

Applying interfaces


 A Java interface declares a set of method signatures i.e., says what behavior exists Does not say how the behavior is implemented
i.e., does not give code for the methods

 Does not describe any state (but may include “final” constants)

 A concrete class that implements an interface Contains “implements InterfaceName” in the class declaration

 Must provide implementations (either directly or inherited from a superclass) of all methods declared in the interface

 An abstract class can also implement an interface

 Can optionally have implementations of some or all interface methods

• Interfaces and Extends both describe an “is- a” relation.

• If B implements interface A, then B inherits the (abstract) method signatures in A

• If B extends class A, then B inherits everything in A.

• which can include method code and instance variables as well as abstract method signatures.

• Inheritance” is sometimes used to talk about the superclass /
subclass “extends” relation only


Variables in interface


• Variables declared in an interface must be constants.

• A technique to import shared constants into multiple classes:

1) declare an interface with variables initialized to the desired
values

2) include that interface in a class through implementation.

• As no methods are included in the interface, the class does
not implement.

• anything except importing the variables as constants.


Example: Interface Variables

class Question implements SharedConstants {
Random rand = new Random();
int ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 30) return NO;
else if (prob < 60) return YES;
else if (prob < 75) return LATER;
else if (prob < 98) return SOON;
else return NEVER;
}
}


Example: Interface Variables 2

• AskMe includes all shared constants in the same way, using them to display the result, depending on the value received:

class AskMe implements SharedConstants {
static void answer(int result) {
switch(result) {
case NO: System.out.println("No"); break;
case YES: System.out.println("Yes"); break;
case MAYBE: System.out.println("Maybe"); break;
case LATER: System.out.println("Later"); break;
case SOON: System.out.println("Soon"); break;
case NEVER: System.out.println("Never"); break;
}
}


Example: Interface Variables 3

 The testing function relies on the fact that both ask and
answer methods.

 defined in different classes, rely on the same constants:

public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}


Extending interfaces

• One interface may inherit another interface.

• The inheritance syntax is the same for classes and interfaces.

interface MyInterface1 {
void myMethod1(...) ;
}
interface MyInterface2 extends MyInterface1 {
void myMethod2(...) ;
}

• When a class implements an interface that inherits another
interface, it must provide implementations for all methods
defined within the interface inheritance chain.


Example: Interface Inheritance 1

• Consider interfaces A and B.

interface A {
void meth1();
void meth2();
}

B extends A:
interface B extends A {
void meth3();
}


Example: Interface Inheritance 2


• MyClass must implement all of A and B methods:

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().");
} }


Example: Interface Inheritance 3


• Create a new MyClass object, then invoke all interface methods on it:


class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}


Post a Comment (0)
Previous Post Next Post