coderz.py

Keep Coding Keep Cheering!

Interface in Java

java thread class

In Java, an interface is a blueprint for a class. It has abstract methods and static constants.

In Java, the interface is a means of achieving abstraction. The Java interface can only have abstract methods, no method bodies. In Java, it is used to achieve abstraction as well as multiple inheritance.

Java Interface also represents the IS-A relationship.

There are three primary reasons to utilize an interface:

Below is a list of them.

  • It’s a technique for achieving abstraction.
  • We can offer various inheritance functionality via an interface.
  • It’s useful for achieving loose coupling.
Syntax:
interface <interface_name>{  
      
    // declare constant fields  
    // declare methods that abstract   
    // by default.  
}  
Bank Example using interface:
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());  
}}  
Output:
ROI: 9.15

Q) Why is it that multiple inheritance is not supported by classes in Java but is supported by interfaces?

Multiple inheritance is not supported in the case of classes due to ambiguity, as we described in the inheritance part. However, because there is no uncertainty in the case of an interface, it is supported. It’s because the implementation class handles the implementation.

Interface inheritance:

Here, an interface is implemented by a class, but one interface extends another.

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();  
 }  
}  
Output:
Hello
Welcome
Java 8 Default Method in Interface

Since Java 8, we can have a method body in the interface. But we need to make it the default method. Let’s see an example:

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();  
}}  
Java 8 Static Method in Interface

Since Java 8, we can have a static method in the interface. Let’s see an example:

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));  
}}  
Nested Interface in Java

Note: An interface can have another interface which is known as a nested interface. For example:

interface printable{  
 void print();  
 interface MessagePrintable{  
   void msg();  
 }  
}  

Note: also read about the Abstract Class & Method in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement