×

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare or implement abstraction.

Multiple inheritance and abstraction are implemented using it in Java.

Use of java interface:

Below are the major justifications for using an interface.

  • To achieve abstraction, it is used.
  • Loose coupling or retriving is done by java interface, it can be employed.

Declaring an Interface

The interface is a keyword used to implement an interface using implementations. Total abstraction is achieved by all interface methods having an empty body and all fields being public, static, and final by default. Every one of the methods that must be implemented by a class that implements an interface.

Syntax for declaring an interface:

interface <interface_name> {     
    // initializing methods that abstract and declaring constants field    
}  

Examples of Java Interface

Example 1: Here the executable or printed interface has only a single method, and its implementation is provided in the S9 class that is given below.

interface executable {  
void print ();
}  
class S9 implements executable {  
public void print ()
 {
System.out.println("Raju bai");
}  


public static void main (String s []) {  
S9 bj = new S9();
bj.print ();
 }  
}  

Output of the program is given below:

Raju bai

Example 2: Sketchable

The Sketchable interface in this illustration has just one method. Rectangle and Circle classes are responsible for its implementation. In a real-world scenario, someone else defines the interface, but multiple implementation providers implement it. Furthermore, someone else uses it. The user of the interface is unaware of the implementation portion.

//Interface declaration: by first user  
interface Sketchable {  
void sketch ();
} 
//Implementation: by second user  
class Triangle implements {Sketchable
public void sketch ()
{
System.out.println("drawing triangle");}  
}  
class Square implements Sketchable {  
public void sketch () {System.out.println("drawing square");}  
}  
//Using interface: by third user  
class Test_ Interface 1{  
public static void main (String s []) {  
Sketchable g=new Square () ;//In real scenario, object is provided by method e.g. getSketchable ()  
g. sketch ();
}}  

Output of the program is:

drawing square 

Example 3: Bank

Let's look at yet another Java interface sample that implements the Bank interface

interface Bank {  
float Interestrate ();
}  
class uni implements Bank {  
public float Interestrate () 
{
return 9.15f;}  
}  
class inp implements Bank {  
public float Interestrate ()
{
return 9.7f;}  
}  
class Test_Interface_2{  
public static void main (String [] s) {  
Bank p=new uni ();
System.out.println("ROI: "+p. Interestrate ());
}}  

Output of the program is:

ROI: 9.15

Multiple Inheritance in Java

Multiple inheritance is the process or method of a class implementing more than one interface or an interface extending more than one interface.

Program:

interface executable {  
void print ();
}  
interface Showable {  
void show ();
}  
class D9 implements executable, Showable {  
public void print () {System.out.println("Hiiii");}  
public void show () {System.out.println("Raju");}  


public static void main (String s []) {  
 D9 obj = new D9();
obj.print ();
obj. show ();
 }  
}  

Output of the program is:

Hiiii
Raju

Interface Inheritance

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

interface Executable {  
void Exe ();
}  
interface Showable extends Executable {  
void show ();
}  
class Test_Interface_4 implements Showable {  
public void print () {System.out.println("Hiii");}  
public void show () {System.out.println("Raju bai");}  


public static void main (String s []) {  
Test_Interface_4 bj = new Test_Interface_4().  
bj. Exe ();
bj. show ();
 }  
}  

Output of the program is:

Hiii
Raju bai

Default Method in Interface Example

interface Sketchable {  
void sketch ();
default void msg () {System.out.println(" method is default");}  
}  
class Square implements Sketchable {  
public void sketch () {System.out.println("drawing square");}  
}  
class Test_Interface_Default {  
public static void main (String s []) {  
Sketchable h=new Square ();
h. Sketch ();
h.msg ();
}}  

Output of the program is:

drawing Square
 method is default

Nested Interface in Java

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

interface executable {  
 void print ();
 interface Messageexecutable {  
   void msg ();
 }  
}  

Related Topics

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

ArrayList VS Linked List

ArrayList VS Linked List Both the ArrayList and LinkedList implements the List interface, and they have some differences as well as some similarities between them. The internal working and performance of both vary significantly. Let’s...

7 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

How to Calculate Time Difference Between Two Dates in Java?

Date is being used extensively in Java to calculate date differences. While constructing an application, the date of joining an organisation, admission date, appointment date, and others might be included....

4 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.