×

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

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Applet Program in Java

Applet Program in Java An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side....

3 minutes read.