Java interface

16
JAVA Interface Prepared by Miss. Arati A. Gadgil

Transcript of Java interface

Page 1: Java interface

JAVA Interface

Prepared by

Miss. Arati A. Gadgil

Page 2: Java interface

Multiple inheritance means creating a new class that inherits behavior directly from more than one superclass.

Java does not support multiple inheritance reason is ambiguity around Diamond problem, consider a class A has show() method and then B and C derived from A and has there own show() implementation and now class D derive from B and C using multiple inheritance and if we refer just show() compiler will not be able to decide which show() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond.

Page 3: Java interface

3

AShow()

BShow()

CShow()

DShow()

Page 4: Java interface

4

Interface is Like a class but only contains abstract method and final variables

example:

interface Operation{ void Add(int a,intnt b); int Sub(int a,int b);}abstract interface Operation{ public abstract sub(); public abstract int sub();}

Both are correct!the abstract and publickeywords are implied,so the shorthand is recommended.

Page 5: Java interface

5

An interface is very much like a class-with one important difference. None of the methods declared in aninterface are implemented in the interface itself. Instead, these methods must be implemented in any class that uses the interface.

In short, interfaces describe behaviors but do not detail how those behaviors will be carried out.

We save the interface's source code in a file with the .java extension. Then use the Java compiler, javac, to compile the source code into byte-code form. Just like a normal class, the byte-code file will have the .class extension.

Page 6: Java interface

6

Difference between class and interface

we cannot instantiate an interface.

An interface does not contain any constructors.

All of the methods in an interface are abstract.

An interface is not extended by a class; it is implemented by a class.

An interface can extend multiple interfaces.

Page 7: Java interface

7

interface student { public void add(); public void display(); }

For defining interface use interface keyword followed by interface name as shown above.

When any class implements the interface then, class must be write the definition of all methods in the interface.

Page 8: Java interface

8

class A implements student{

String nm;

public void add(){ Scanner sc=new Scanner(System.in);

nm=sc.nextLine();}

public void display(){

System.out.print("Student name="+nm);}

}

Page 9: Java interface

9

Extending interface

An interface can extend another interface, similarly to the way that a class can extend another class.

The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

Page 10: Java interface

10

interface abc{ static int a=10; void displayA();}interface student extends abc{ void add();

void display();}

Page 11: Java interface

11

Using an Interface as a Type

When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name.

If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

As an example,method for finding the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable

Page 12: Java interface

12

public Object findLargest(Object object1, Object object2) {

Relatable obj1 = (Relatable)object1; Relatable obj2 = (Relatable)object2; if ((obj1).isLargerThan(obj2) > 0)

return object1; else

return object2; }

By casting object1 to a Relatable type, it can invoke the isLargerThan method

Page 13: Java interface

13

Evolving Interfaces

Consider an interface that we have developed called operation:public interface opertion {

void Add(int i, int j); void Mul(int a,int b);

}Suppose later we want to add a new method so now interface will bepublic interface opertion {

void Add(int i, int j); void Mul(int a, int b);void Sub(int c, int d);

}

Page 14: Java interface

14

If we make this change, then all classes that implement the old operation interface will break because they no longer implement the old interface.

 If you want to add additional methods to an interface, you have several options. Create oprationplus interface that extends operation:

public interface operationplus extends operation{

void sub(int c, int d);}

Page 15: Java interface

15

Default methods

Alternatively, we can define your new methods as default methods. The following example defines a default method named sub:

public interface opertion {

void Add(int i, int j); void Mul(int a, int b);default void Sub(int c, int d){//method body}

}

Note that we must provide an implementation for default methods

Page 16: Java interface

Thank You

16