Java Interface (With Examples)
Java Interface (With Examples)
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(https://srv.carbonads.net/ads/click/x/GTND42JUCABIP5QNFT7LYKQNCAS
segment=placement:wwwprogramizcom;) ADS VIA CARBON
(HTTP://CARBONADS.NET/?UTM_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
Java Interface
In this tutorial, we will learn about Java interfaces. We will learn how to implement
interfaces and when to use them in detail with the help of examples.
interface Language {
public void getType();
Here,
Language is an interface.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Implementing
Thank
60% you for printing ouran
Access
Interface
content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
contents.
OFF All
Like abstractCourses
classes, we cannot create objects of interfaces.
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
To use(/) an block&utm_campaign=programiz&utm_medium=referral)
interface, other classes must implement it. We use the implements
www.domain-name.com
keyword to implement an interface.
interface Polygon {
void getArea(int length, int breadth);
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
Output
In the above example, we have created an interface named Polygon . The interface
contains an abstract method getArea() .
Here, the Rectangle class implements Polygon . And, provides the implementation
of the getArea() method.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example
Thank
60% you for 2: Java
printing ourInterface
content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
//PRO
Try (https://programiz.pro/learn/master-java?utm_source=right-floating-
create an interface
for FREE
Search tutorials & examples
interface
(/) block&utm_campaign=programiz&utm_medium=referral)
Language {
void getName(String name);
www.domain-name.com
}
class Main {
public static void main(String[] args) {
ProgrammingLanguage language = new ProgrammingLanguage();
language.getName("Java");
}
}
Output
Here, the ProgrammingLanguage class implements the interface and provides the
implementation for the method.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
interface AAccess
{
contents.
OFF // members All of A
} Courses
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
forinterface
FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
B {
// members of B
www.domain-name.com
}
class C implements A, B {
// abstract members of A
// abstract members of B
}
Extending an Interface
Similar to classes, interfaces can extend other interfaces. The extends keyword is
used for extending interfaces. For example,
interface Line {
// members of Line interface
}
// extending interface
interface Polygon extends Line {
// members of Polygon interface
// members of Line interface
}
Here, the Polygon interface extends the Line interface. Now, if any class
implements Polygon , it should provide implementations for all the abstract
methods of both Line and Polygon .
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
interface AAccess
{
contents.
OFF ... All
} Courses
For Life
interface
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
B {
for FREE
Search tutorials & examples
(/)... block&utm_campaign=programiz&utm_medium=referral)
}
www.domain-name.com
interface C extends A, B {
...
}
Now that we know what interfaces are, let's learn about why interfaces are used in
Java.
Here, we know getArea() calculates the area of polygons but the way area is
calculated is different for different polygons. Hence, the implementation of
getArea() is independent of one another.
Interfaces provide specifications that a class (which implements it) must follow.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
60%Inyou
Thank ourforprevious example,
printing our content we
at have used getArea()
www.domain-name.com. as acheck
Please specification
sale__top-banner-programiz-web__feb__50) back soon inside
for new the
Access
contents.
OFFinterface All Polygon . This is like setting a rule that we should be able to get the
Courses
area of every polygon.
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
Now any class that implements the Polygon interface must provide an
www.domain-name.com
implementation for the getArea() method.
Interfaces are also used to achieve multiple inheritance in Java. For example,
interface Line {
…
}
interface Polygon {
…
}
Here, the class Rectangle is implementing two different interfaces. This is how
we achieve multiple inheritance in Java.
Note: All the methods inside an interface are implicitly public and all fields
are implicitly public static final . For example,
interface Language {
// by default public
void getName();
}
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
default
Thank
60% methods
you for printing
Access
in
our contentJava Interfaces Please check back soon for new
at www.domain-name.com.
sale__top-banner-programiz-web__feb__50)
contents.
OFF All
With the release Courses
of Java 8, we can now add methods with implementation inside an
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
interface.
for FREE
These
Searchmethods
tutorials &are called default methods.
examples
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
To declare default methods inside interfaces, we use the default keyword. For
example,
Let's take a scenario to understand why default methods are introduced in Java.
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.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Example:
Thank
60% Default
you for printing our Method
content in Java Interface Please check back soon for new
at www.domain-name.com.
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Courses
For Life
interface
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
Polygon {
for FREE
Search tutorials & examples
block&utm_campaign=programiz&utm_medium=referral)
void getArea();
(/)
www.domain-name.com
// default method
default void getSides() {
System.out.println("I can get sides of a polygon.");
}
}
Output
Here, we have created two classes Rectangle and Square that implement Polygon
.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
The you
Thank
60% Rectangle class
for printing provides
our content the implementation of
at www.domain-name.com. the check
Please method
back soon
getArea()
sale__top-banner-programiz-web__feb__50) and
for new
Access
contents.
OFF
overrides theAllgetSides() method. However, the Square class only provides the
Courses
implementation of the getArea() method.
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
(/) block&utm_campaign=programiz&utm_medium=referral)
Now, while calling the getSides() method using the Rectangle object, the
www.domain-name.com
overridden method is called. However, in the case of the Square object, the default
method is called.
Similar to a class, we can access static methods of an interface using its references.
For example,
// create an interface
interface Polygon {
staticMethod(){..}
}
Note: With the release of Java 9, private methods are also supported in
interfaces.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Thank
60% you for printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All sqrt function
// To use the
Courses
import java.lang.Math;
For Life
Try PRO (https://programiz.pro/learn/master-java?utm_source=right-floating-
for FREE
Search tutorials & examples
interface
(/) Polygon {
block&utm_campaign=programiz&utm_medium=referral)
void getArea();
www.domain-name.com
// calculate the perimeter of a Polygon
default void getPerimeter(int... sides) {
int perimeter = 0;
for (int side: sides) {
perimeter += side;
}
Output
Area: 2.9047375096555625
Perimeter: 9
Now, all polygons that implement Polygon can use getPerimeter() to calculate
perimeter.
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
However,
Thank
60% you forthe ruleour
printing forcontent
calculating the area is different
at www.domain-name.com. for different
Please check backpolygons.
sale__top-banner-programiz-web__feb__50) Hence,
soon for new
Access
contents.
OFF
getArea() isAll included without implementation.
Courses
For Life
Any class(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO that
Searchimplements Polygon must provide an implementation of getArea() .
tutorials & examples
for FREE
(/) block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Next Tutorial:
(/java-programming/polymorphism)
Java Polymorphism
Previous Tutorial:
(/java-programming/abstract-classes-methods)
Abstract Class & Method
Share on:
(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/intent/tweet?
u=https://www.programiz.com/java- text=Check%20this%20amazing%20
programming/interfaces) programming/interfaces)
Offer
Ends (https://programiz.pro/offer/pro-sales?utm_source=Top-banner-
FLAT programiz-web&utm medium=banner&utm campaign=2-day-
Soon
Soon programiz web&utm_medium banner&utm_campaign 2 day
Related
Thank
60% you forTutorials
printing our content at www.domain-name.com. Please check back soon for new
sale__top-banner-programiz-web__feb__50)
Access
contents.
OFF All
Java Tutorial
Courses
For Life
(https://programiz.pro/learn/master-java?utm_source=right-floating-
Try PRO
for FREE Search tutorials & examples
Java
(/) Anonymous Class
block&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(/java-programming/anonymous-class)
Java Tutorial
(/java-programming/enum-inheritance)
Java Tutorial
(/java-programming/collections)
Java Tutorial
(/java-programming/collection-interface)