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 its Definition, Use, default methods, static method, variables, and How to implement the Java interface.
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.
- 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 the interface keyword as below mentioned.
interface <interface_name> { // declare constant fields // declare abstract methods }
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.
public interface InterfaceExample { // Variables // methods }
Another example with Some methods and variables.
public interface MyInterface { String hello = "Hello"; void sayHello(); }
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
Code:
class Vehicle { void run(){ System.out.println("Running"); } } class TwoWheeler extends Vehicle { void wheel(){ System.out.println("2 Wheeler"); } }
class – interface: implements
Code:
public interface MyInterface { String hello = "Hello"; void sayHello(); }
Must implement all methods of the interface.
class MyClass implements MyInterface { @Override public void sayHello() { } }
interface – interface: extends
Code:
public interface MyInterface { String hello = "Hello"; void sayHello(); }
public interface OtherInterface extends MyInterface { }
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.
interface Vehicle { void maxSpeed(); } public class Bike implements Vehicle { @Override public void maxSpeed() { System.out.println("120 Km/H"); } public static void main(String arg[]) { Bike bike = new Bike(); bike.maxSpeed(); } }
Output: 120 Km/H
Java interface constructor
There is no constructor of the interface, because as we now call a constructor’s need to create an object by a 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 from 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:
interface NewInterface { default void sayHello() { System.out.println("default method"); } } class MyClass implements NewInterface { public static void main(String args[]) { MyClass myClass = new MyClass(); myClass.sayHello(); } @Override public void sayHello() { System.out.println("A Interface default method"); } }
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:
interface NewInterface { static void sayHello() { System.out.println("The Interface static method"); } } class MyClass implements NewInterface { public static void main(String args[]) { NewInterface.sayHello(); } }
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.
interface Vehicle { void maxSpeed(); } interface Brand{ void name(); } public class Bike implements Vehicle,Brand{ @Override public void maxSpeed() { System.out.println("120 Km/H"); } @Override public void name() { System.out.println("Yamaha"); } public static void main(String arg[]) { Bike bike = new Bike(); bike.maxSpeed(); bike.name(); } }
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 defining a nested class.
public interface MyInterface { String hello = "Hello"; void sayHello(); interface MessageInterface { void msg(); } }
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.1Java 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.