Вы находитесь на странице: 1из 7

INTERFACE

 Interface defines what a class must do but not how it will do it.
 In java, an interface defines set of methods that will be implemented by a class.
 interfaces are similar to abstract classes, except that no method can include a
body.
 an interface is a construct that describes functionality without specifying
implementation.
 An interface is declared by use of the interface keyword.
access interface name {
ret-type method-name1(parameter-list);
ret-type method-name2(parameter-list);
|
|
ret-type method-nameN(parameter-list);
}

 access -> either public or not used (because it makes them accessible to the
widest range of code).
 In an interface, methods are declared using only their return type and signature.
They are essentially abstract methods.
 once an interface is defined, any number of classes can implement it. This
makes it possible for 2 or more classes to provide the same functionality even
though they might do so in different ways.
 To implement an interface, follow these 2 steps:

 in a class declaration, include an implements clause that specifies the


interface being implemented.
 inside the class, implement the methods defined by the interface.

 syntax: class classname extends superclass implements interface {


//class-body
}

 extends is optional (it enables to inherit the class)

 NOTE: To implement more than one interface, the interfaces are separated
with a comma.

 NOTE: the methods that implement an interface must be declared public.


Also, the return type and signature of the implementing method must
match exactly the return type and signature specified in the interface
declaration.

 NOTE: only obligation a class has when implementing an interface is to


provide the methods defined by the interface. It is not limited to provide
only those methods. The class can provide whatever additional
functionality is desired.
Example:
//save as series.java
public interface series {
int getseries();
}

//save as demo2.java
class twotable implements series {
int s=0;
public int getseries() {
s += 2; return s;
}
}
class threetable implements series {
int s=0;
public void show() { System.out.println("we are in threetable class"): }
public int getseries() {
s += 3; return s;
}
}
class demo2 {
public static void main(String[] s)
{
twotable ob1 = new twotable();
threetable ob2 = new threetable();
for (int i=1; i<=10;i++)
System.out.println("2 X "+i+" = "+ ob1.getseries());
ob2.show();
for (int i=1; i<=10;i++)
System.out.println("3 X "+i+" = "+ ob2.getseries());

}
}
 one class can implement any number of interfaces. This makes it possible for a
single class to provide a wide range of well- defined functionality.( multiple
interfaces)
Example
interface x {
void show1();
}
interface y
{ void show2(); }

class z implements x,y {


public void show1()
{ system.out.println("interface x"); }
public void show2()
{ system.out.println("interface y"); }
}
class demo {
public static void main(String[] s)
{
z ob1 = new z();
ob1.show1();
ob1.show2();
}
}
 variables declared in interface are always PUBLIC, FINAL and STATIC and
must be initialized. ( act as constants in implementing classes).

Interfaces can be extended

 One interface can inherit another by use of the keyword extends.


 when a class implements an interface that inherits another interface, it must
provide implementations for all methods defined within the interface inheritance
chain.
Example:
//A.java
interface A {
void method1();
void method2();
}
//B.java
interface B extends A {
void method3();
}
//demo.java
class Myclass implements B {
public void method1() { System.out.println(" method in interface A"); }
public void method2() { System.out.println(" method-2 in interface A"); }
public void method3() { System.out.println(" method in interface B"); }
}
class demo
{
public static void main(String[] s)
{
Myclass ob = new Myclass();
ob.method1();
ob.method2();
ob.method3();
}
}

NESTED INTERFACE

 An interface can be declared a member of another interface or of a class. such an


interface is called a member interface or nested interface.
 An interface nested in a class can use any access modifier.
 An interface nested in another interface is implicitly public.
Example:
//A.java
interface A {
public interface B {
boolean isNotNegative (int x);
}
void show();
}
//demo.java
class C implements A.B {
public boolean isNotNegative(int x) {
return x<0 ? false : true;
}
}
class demo {
public static void main(String[] S) {
A.B ob = new C();
if(ob.isNotNegative(10))
System.out.println("10 is not negative");
}
}

differences between interface and abstract class


Abstract class Interface

1) Abstract class can have abstract and non- Interface can have only abstract methods.
abstractmethods.

2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and non- Interface has only static and final variables.
static variables.

4) Abstract class can have static methods, main method Interface can't have static methods, main method or
and constructor. constructor.

5) Abstract class can provide the implementation of Interface can't provide the implementation of
interface. abstract class.

6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

7) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

Вам также может понравиться