interface in Java :
An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not method body. It is used to achieve
abstraction and multiple inheritances in Java.
Why use Java interface?
There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.
Interface inheritance
A class implements an interface, but one interface extends another interface.
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){[Link]("Hello");}
public void show(){[Link]("Welcome");}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
[Link]();
[Link]();
}
}
Output:
Hello
Example 1:
Interface A
{
Void Adisplay();
}
Class AB implements A
{
Public void Adisplay();
{
[Link](A Display”);
}}
Class A Main
{
PSVM(String args[])
{
AB obj= new AB();
[Link]();
}}
Example 2:
Interface A
{
Void Adisplay();
}
Interface B
{
Void Bdisplay();
}
Class AB implements A,B
{
Public void Adisplay();
{
[Link](A Display”);
}
Public void Bdisplay();
{
[Link](B Display”);
}}
Class A Main
{
PSVM(String args[])
{
AB obj= new AB();
[Link]();
[Link]();
}}
Example 4:
Interface A
{
Void Adisplay();
Void Ashow();
}
Interface B
{
Void Bdisplay();
Void Bshow();
}
Class AB implements A,B
{
Void Adisplay();
{
[Link](A DISPLAY”);
}
Void Ashow();
{
[Link](A SHOW”);
}
Void Bdisplay();
{
[Link](B DISPLAY”);
}
Void Bshow();
{
[Link](B SHOW”);
}}
Class interface AB
{
Public static void main(String args[])
{
AB obj=new AB();
[Link]();
[Link]();
[Link]();
[Link]();
}}
Example 3:
Interface Features
{
Void dialing();
Void messaging();
}
Interface Addons
{
Void Vcalling();
Void mms();
}
Class FeaturedPhone implements Features
{
Public void dialing();
{
[Link](“Featured Phone is Dialing…”);
}
Public void messaging();
{
[Link](“Featured Phone is Mess…”);
}}
Class SmartPhone implements Features,Addons
{
Public void dialing();
{
[Link](“Smart Phone is Dialing…”);
}
Public void messaging();
{
[Link](“smart Phone is Mess…”);
}
Public void Vcalling();
{
[Link](“smart Phone is Vediocall…”);
}
Public void mms();
{
[Link](“smart Phone is Sending MMS…”);
}}
Class Mobile
{
PSVM(String args[])
{
FeaturedPhone fp= new FeaturedPhone();
SmartPhone sp= new SmartPhone();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}}
Hybrid inheritance in java
A hybrid inheritance is a combination of more than one types of inheritance.
A
↑
|
---------------
↑ ↑
| |
B C
↑
|
D
Hybrid inheritance:
Class A
{
int a;
}
Class B extends A
{
int b;
}
Class C extends A
{
int c;
C()
{
a=10;
c=20;
}
Void sum()
{
[Link](“sum is “+ (a+c));
}}
Class D extends B
{
int d;
{
D()
{
a=10;
b=20;
d=30;
}
Void mul()
{
[Link](“Multiplication is “+ (a*b*d));
}}
Class Test
{
PSVM(String args[])
{
C obj1=new C();
[Link]();
D obj2=new D();
[Link]();
}}