Implementing Interface in Java Example Program
An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. Java Interface also represents the IS-A relationship.
Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
- Interfaces specify what a class must do and not how. It is the blueprint of the class.
- An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
- If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
- A Java library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection.
Syntax:
interface { // declare constant fields // declare methods that abstract // by default. }
To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword.
Why do we use an Interface?
- It is used to achieve total abstraction.
- Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
- It is also used to achieve loose coupling.
- Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in the interface are final, public and static.
// A simple interface interface Player { final int id = 10; int move(); }
Difference Between Class and Interface
The major differences between a class and an interface are:
S. No. | Class | Interface |
---|---|---|
1. | In class, you can instantiate variables and create an object. | In an interface, you can't instantiate variables and create an object. |
2. | Class can contain concrete(with implementation) methods | The interface cannot contain concrete(with implementation) methods |
3. | The access specifiers used with classes are private, protected, and public. | In Interface only one specifier is used- Public. |
Implementation: To implement an interface we use the keyword implements
Java
import
java.io.*;
interface
In1 {
final
int
a =
10
;
void
display();
}
class
TestClass
implements
In1 {
public
void
display(){
System.out.println(
"Geek"
);
}
public
static
void
main(String[] args)
{
TestClass t =
new
TestClass();
t.display();
System.out.println(a);
}
}
Real-World Example: Let's consider the example of vehicles like bicycle, car, bike………, they have common functionalities. So we make an interface and put all these common functionalities. And lets Bicycle, Bike, car ….etc implement all these functionalities in their own class in their own way.
Java
import
java.io.*;
interface
Vehicle {
void
changeGear(
int
a);
void
speedUp(
int
a);
void
applyBrakes(
int
a);
}
class
Bicycle
implements
Vehicle{
int
speed;
int
gear;
@Override
public
void
changeGear(
int
newGear){
gear = newGear;
}
@Override
public
void
speedUp(
int
increment){
speed = speed + increment;
}
@Override
public
void
applyBrakes(
int
decrement){
speed = speed - decrement;
}
public
void
printStates() {
System.out.println(
"speed: "
+ speed
+
" gear: "
+ gear);
}
}
class
Bike
implements
Vehicle {
int
speed;
int
gear;
@Override
public
void
changeGear(
int
newGear){
gear = newGear;
}
@Override
public
void
speedUp(
int
increment){
speed = speed + increment;
}
@Override
public
void
applyBrakes(
int
decrement){
speed = speed - decrement;
}
public
void
printStates() {
System.out.println(
"speed: "
+ speed
+
" gear: "
+ gear);
}
}
class
GFG {
public
static
void
main (String[] args) {
Bicycle bicycle =
new
Bicycle();
bicycle.changeGear(
2
);
bicycle.speedUp(
3
);
bicycle.applyBrakes(
1
);
System.out.println(
"Bicycle present state :"
);
bicycle.printStates();
Bike bike =
new
Bike();
bike.changeGear(
1
);
bike.speedUp(
4
);
bike.applyBrakes(
3
);
System.out.println(
"Bike present state :"
);
bike.printStates();
}
}
Output
Bicycle present state : speed: 2 gear: 2 Bike present state : speed: 1 gear: 1
Advantages of Interfaces in Java
The advantages of using interfaces in Java are as follows:
- Without bothering about the implementation part, we can achieve the security of the implementation.
- In Java, multiple inheritance is not allowed, however, you can use an interface to make use of it as you can implement more than one interface.
New Features Added in Interfaces in JDK 8
1. Prior to JDK 8, the interface could not define the implementation. We can now add default implementation for interface methods. This default implementation has a special use and does not affect the intention behind interfaces.
Suppose we need to add a new function in an existing interface. Obviously, the old code will not work as the classes have not implemented those new functions. So with the help of default implementation, we will give a default body for the newly added functions. Then the old codes will still work.
Java
interface
In1
{
final
int
a =
10
;
default
void
display()
{
System.out.println(
"hello"
);
}
}
class
TestClass
implements
In1
{
public
static
void
main (String[] args)
{
TestClass t =
new
TestClass();
t.display();
}
}
2. Another feature that was added in JDK 8 is that we can now define static methods in interfaces that can be called independently without an object. Note: these methods are not inherited.
Java
interface
In1
{
final
int
a =
10
;
static
void
display()
{
System.out.println(
"hello"
);
}
}
class
TestClass
implements
In1
{
public
static
void
main (String[] args)
{
In1.display();
}
}
Important Points About Interface or Summary of the Article:
- We can't create an instance(interface can't be instantiated) of the interface but we can make the reference of it that refers to the Object of its implementing class.
- A class can implement more than one interface.
- An interface can extend to another interface or interface (more than one interface).
- A class that implements the interface must implement all the methods in the interface.
- All the methods are public and abstract. And all the fields are public, static, and final.
- It is used to achieve multiple inheritances.
- It is used to achieve loose coupling.
New Features Added in Interfaces in JDK 9
From Java 9 onwards, interfaces can contain the following also:
- Static methods
- Private methods
- Private Static methods
Related Articles:
- Access Specifier of Methods in Interfaces
- Access Specifiers for Classes or Interfaces in Java
- Abstract Classes in Java
- Comparator Interface in Java
- Java Interface Methods
- Nested Interface in Java
This article is contributed by Mehak Kumar and Nitsdheerendra. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Source: https://www.geeksforgeeks.org/interfaces-in-java/
0 Response to "Implementing Interface in Java Example Program"
Post a Comment