JAVA
Interface
Prepared by
Miss. Arati A. Gadgil
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.
3
A
Show()
B
Show()
C
Show()
D
Show()
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 public
keywords are implied,
so the shorthand is
recommended.
5
An interface is very much like a class-with one important
difference. None of the methods declared in an
interface 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.
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.
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.
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);
}
}
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.
10
interface abc
{
static int a=10;
void displayA();
}
interface student extends abc
{
void add();
void display();
}
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
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
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 be
public interface opertion
{
void Add(int i, int j);
void Mul(int a, int b);
void Sub(int c, int d);
}
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);
}
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
Thank You
16

Java interface

  • 1.
  • 2.
    Multiple inheritance meanscreating 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.
  • 3.
  • 4.
    4 Interface is Likea 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 public keywords are implied, so the shorthand is recommended.
  • 5.
    5 An interface isvery much like a class-with one important difference. None of the methods declared in an interface 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.
  • 6.
    6 Difference between classand 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.
  • 7.
    7 interface student { public voidadd(); 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.
  • 8.
    8 class A implementsstudent { String nm; public void add() { Scanner sc=new Scanner(System.in); nm=sc.nextLine(); } public void display() { System.out.print("Student name="+nm); } }
  • 9.
    9 Extending interface An interfacecan 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.
  • 10.
    10 interface abc { static inta=10; void displayA(); } interface student extends abc { void add(); void display(); }
  • 11.
    11 Using an Interfaceas 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
  • 12.
    12 public Object findLargest(Objectobject1, 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
  • 13.
    13 Evolving Interfaces Consider aninterface 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 be public interface opertion { void Add(int i, int j); void Mul(int a, int b); void Sub(int c, int d); }
  • 14.
    14 If we makethis 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); }
  • 15.
    15 Default methods Alternatively, wecan 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
  • 16.