Java Interface | Definition, Use, Methods | What is ? How to implement Example

Java interface definition (What is an interface)

Java interface provides 100% abstraction. The interface also has methods and variables same as a class but methods are by default abstract. Mends no Body or sentence in the method the only declaration. And Variables in Interface are constant, which means fields are public, static, and final by default.

The interface is a set of instructions or blueprints of a class.

This Java interface tutorial is a complete overview and detail about everything in Java Interface. Like it’s Definition, Use,  default methods, static method, variables, and How to implement Java interface.

Java Interface Definition, Use, Methods What is ? How to implement Example

Note: Must read new features of Java interface as mentioned bottom of this chapter.

Use of interface in Java

Here are some important use or needs of Interface in programming.

What is Use of Java interface
  • First and the main reason to use interface is to achieve total abstraction.
  • Java does not support Multiple Inheritance through class, so with Interface, Multiple Inheritance is possible.
  • It’s Provide a Loose coupling.

Syntax

Here Java Interface syntax, how to declare an interface. To declare an interface, use interface keyword as below mentioned.

Example of Java interface

Same as we define a class in Java used “class keyword”. Here you need an “interface” keyword to create an interface.

Here is a Java interface example, how to create its program? See below code a very easy to do it.

Another example with Some methods and variables.

How to Java interface implements

Before coming into the Implements part let’s look at a “Relationship between classes and interfaces”. One by one we will look at all examples of Implementing an Interface with class and interface.

class – class: extends

java class inheritance class to class

Code:

class – interface: implements

Java interface inheritance class to interface

Code: 

Must implement all methods of the interface.

interface – interface: extends

Java interface inheritance interface to interface

Code:

The Upper diagrams show relation and explanations, now let’s do some practical example “Implementing an Interface”.

When class need to inherit an interface property then it needs to use implements keyword.

Output: 120 Km/H

Java interface constructor

There is no constructor of the interface, because as we now to call a constructor’s need to create an object by new keyword. But Interface has used by inheritance.

So There is no constructor till now in the interface.

Interface methods

Java interface default methods are abstract methods and some new updates since Java 8.

Default Method in Interface

A new update form Java 8, method no longer only declaration. Mena’s methods can have copy and statements but methods should be a default access modifier

Let’s see an example:

Output: A Interface default method

Static Method in Interface

Same as Java 8 as now allow to use static methods in the interface. Let’s see an example:

Output: The Interface static method

Interface variables

Java interface variables are a constant before Java 8, for now, can contain both variables and constants. All variables in an interface should be public access modifier, even if you leave it a blank keyword in the variable declaration.

Tiring add Interface variables as a Private or protected or default will show error – Modifier ‘default or other’ not allowed here.

interface inheritance

Java interface inheritance is done by using the implements keyword. It also gives an option in java to do Multiple inheritances.

Output: 120 Km/H
Yamaha

Nested Interface in Java

A Nested Interface is which is inside another interface. Below is a programming code example of Nested Interface. It same as to define nested class.

Here is Java interface Attributes and Some Rules:

  • An interface has only abstract methods.
  • The class can implement any number of interfaces.
  • All methods public and fields are public, static, and final by default. (Java Modifiers)
  • A Class must implement all declared methods of an interface.

JDK 8 – New features added in interfaces

Here are new features or changes of Interface in Java 8.

  • Java 8 allows the interfaces to have default and static methods.

JDK 9 – New features added in interfaces

From Java 9 onwards has some changes, interfaces can contain the following.

  • Static methods
  • Private methods
  • Private Static methods

Q: Which Java Types Can Implement Interfaces?

Answer: A Java Interface can implement with following types as below list:

  • Java Class
  • Abstract Class
  • Java Enum
  • Dynamic Proxy
  • Nested Class

Do comment below if any doubt or suggestion with examples and details.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All  Interface in Java with example program PDF are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.