×

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and inheritance in java.

a

Interface in Java

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.

Only abstract methods—not method bodies—are permitted in the Java interface. Multiple inheritance and abstraction are implemented using it in Java.

Use of java interface:

The major three justifications for using an interface. The list of them is below.

  • To achieve abstraction, it is used.
  • We can offer the functionality of multiple inheritance through an interface.
  • Loose couplingor 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 given by; all methods in an interface are provided with an empty body, and all fields are by default public, static, and final. All the methods implemented in an interface that must be implemented by a class which implements the interface.

Syntax for declaring an interface:

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

Example of Java Interface:

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 []) {
Sketchableg=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 []) {
Sketchableh=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

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.