JavaInterfaces
JavaInterfaces
Similarities
An interface is similar to a class in the following ways −
Differences
However, an interface is different from a class in several ways, including −
1
The interface keyword is used to declare an interface. Here is a simple example
to declare an interface −
An interface is implicitly abstract. You do not need to use the abstract keyword
while declaring an interface.
Each method in an interface is also implicitly abstract, so the abstract keyword is
not needed.
Methods in an interface are implicitly public.
Example
2
A class uses the implements keyword to implement an interface. The
implements keyword appears in the class declaration following the extends
portion of the declaration.
Output
Mammal eats
Mammal travels
3
When overriding methods defined in interfaces, there are several rules to be
followed −
// Filename: Sports.java
public interface Sports {
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
// Filename: Football.java
public interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
4
// Filename: Hockey.java
public interface Hockey extends Sports {
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}
The Hockey interface has four methods, but it inherits two from Sports; thus, a
class that implements Hockey needs to implement all six methods. Similarly, a
class that implements Football needs to define the three methods from Football
and the two methods from Sports.
Example
Open Compiler
interface Sports {
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
5
public void setVisitingTeam(String name) {}
Output
The extends keyword is used once, and the parent interfaces are declared in a
comma-separated list.
For example, if the Hockey interface extended both Sports and Event, it would be
declared as −
6
interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
interface Event {
public void organize();
}
public class HockeyDemo implements Hockey, Event {
7
public void organize() {
System.out.println("Match organized. ");
}
}
Output
To declare default methods inside interfaces, we use the default keyword. For
example,
We can add the method in our interface easily without implementation. However,
that's not the end of the story. All our classes that implement that interface must
provide an implementation for the method.
If a large number of classes were implementing this interface, we need to track all
these classes and make changes to them. This is not only tedious but error-prone
as well.
To resolve this, Java introduced default methods. Default methods are inherited
like ordinary methods.
8
Let's take an example to have a better understanding of default methods.
// default method
default void getSides() {
System.out.println("I can get sides of a polygon.");
}
}
class Main {
public static void main(String[] args) {
9
// create an object of Rectangle
Rectangle r1 = new Rectangle();
r1.getArea();
r1.getSides();
Output
Here, we have created two classes, Rectangle and Square , that implement Polygon .
The Rectangle class provides the implementation of the getArea() method and
overrides the getSides() method. However, the Square class only provides the
implementation of the getArea() method.
Now, while calling the getSides() method using the Rectangle object, the
overridden method is called. However, in the case of the Square object, the default
method is called.
10