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.
Below is a list of them.
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
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
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.
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();
}
}
Hello
Welcome
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();
}}
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));
}}
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…