Interface in Java
Interface in Java
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
There can be only abstract methods in the Java interface, not method body. It is used to
achieve abstraction and multiple inheritance in Java
In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body.
There are mainly three reasons to use interface. They are given below.
It provides total abstraction; means all the methods in an interface are declared with the
empty body, and all the fields are public, static and final by default.
A class that implements an interface must implement all the methods declared in the
interface.
Syntax:
interface <interface_name>{
1.
2. // declare constant fields
3. // declare methods that abstract
4. // by default.
5. }
OOP-UNIT 2- INTERFACE IN JAVA
In other words, Interface fields are public, static and final by default, and the methods are
public and abstract.
OOP-UNIT 2- INTERFACE IN JAVA
1. interface Drawable{
2. void draw();
3. }
4. class Rectangle implements Drawable{
5. public void draw(){System.out.println("drawing rectangle");}
6. }
7. class Circle implements Drawable{
8. public void draw(){System.out.println("drawing circle");}
9. }
10. class TestInterface1{
11. public static void main(String args[]){
12. Drawable d=new Circle();
13. d.draw();
14. }}
OOP-UNIT 2- INTERFACE IN JAVA
1. interface Bank{
2. float rateOfInterest(); }
3. class SBI implements Bank{
4. public float rateOfInterest(){return 9.15f;}
5. }
6. class PNB implements Bank{
7. public float rateOfInterest(){return 9.7f;}
8. }
9. class TestInterface2{
10. public static void main(String[] args){
11. Bank b=new SBI();
12. System.out.println("ROI: "+b.rateOfInterest());
13. }}
OOP-UNIT 2- INTERFACE IN JAVA
1. interface Printable{
2. void print(); }
3. interface Showable{
4. void show();
5. }
6. class A7 implements Printable,Showable{
7. public void print(){System.out.println("Hello");}
8. public void show(){System.out.println("Welcome");}
9. public static void main(String args[]){
10. A7 obj = new A7();
11. obj.print();
12. obj.show();
13. } }
OOP-UNIT 2- INTERFACE IN JAVA
1. interface Printable{
2. void print(); }
3. interface Showable{
4. void print();
5. }
6. class TestInterface3 implements Printable, Showable{
7. public void print(){System.out.println("Hello");}
8. public static void main(String args[]){
9. TestInterface3 obj = new TestInterface3();
10. obj.print();
11. } }
OOP-UNIT 2- INTERFACE IN JAVA
Hello
Interface inheritance
1. interface Printable{
2. void print(); }
3. interface Showable extends Printable{
4. void show();
5. }
6. class TestInterface4 implements Showable{
7. public void print(){System.out.println("Hello");}
8. public void show(){System.out.println("Welcome");}
9. public static void main(String args[]){
10. TestInterface4 obj = new TestInterface4();
11. obj.print();
12. obj.show();
13. } }
OOP-UNIT 2- INTERFACE IN JAVA
Since Java 8, we can have method body in interface. But we need to make it default
method. Let's see an example:
1. interface Drawable{
2. void draw();
3. default void msg(){System.out.println("default method");} }
4. class Rectangle implements Drawable{
5. public void draw(){System.out.println("drawing rectangle");} }
6. class TestInterfaceDefault{
7. public static void main(String args[]){
8. Drawable d=new Rectangle();
9. d.draw();
10. d.msg();
11. }}
OOP-UNIT 2- INTERFACE IN JAVA
Since Java 8, we can have static method in interface. Let's see an example:
1. interface Drawable{
2. void draw();
3. static int cube(int x){return x*x*x;}
4. }
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8. class TestInterfaceStatic{
9. public static void main(String args[]){
10. Drawable d=new Rectangle();
11. d.draw();
12. System.out.println(Drawable.cube(3));
13. }}
OOP-UNIT 2- INTERFACE IN JAVA
1. interface printable{
2. void print();
3. interface MessagePrintable{
4. void msg();
5. }
6. }