它用于实现抽象。
通过接口,我们可以支持多重继承的功能。
可用于实现松散耦合。
interface <
interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.}
interface printable{
void print();
}
class A6 implements printable{
public void print(){
System.out.println("Hello");
}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Hello
//Interface declaration: by first userinterface Drawable{
void draw();
}
//Implementation: by second userclass Rectangle implements Drawable{
public void draw(){
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable{
public void draw(){
System.out.println("drawing circle");
}
}
//Using interface: by third userclass TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();
//In real scenario, object is provided by method e.g. getDrawable()d.draw();
}
}
drawing circle
interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){
return 9.15f;
}
}
class PNB implements Bank{
public float rateOfInterest(){
return 9.7f;
}
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}
}
ROI: 9.15
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){
System.out.println("Hello");
}
public void show(){
System.out.println("Welcome");
}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:Hello
Welcome
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestInterface3 implements Printable, Showable{
public void print(){
System.out.println("Hello");
}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
obj.print();
}
}
Hello
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){
System.out.println("Hello");
}
public void show(){
System.out.println("Welcome");
}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
Hello
Welcome
interface Drawable{
void draw();
default void msg(){
System.out.println("default method");
}
}
class Rectangle implements Drawable{
public void draw(){
System.out.println("drawing rectangle");
}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}
}
drawing rectangle
default method
interface Drawable{
void draw();
static int cube(int x){
return x*x*x;
}
}
class Rectangle implements Drawable{
public void draw(){
System.out.println("drawing rectangle");
}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}
}
drawing rectangle
27
//How Serializable interface is written?public interface Serializable{
}
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}