×

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

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes 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.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.