Java interface introduction
JAVA
interface is an
important concept in the java programming language.
This java
interface tutorial will dive in depth into this concept and explain it with
many examples.
In a
previous tutorial, we learned what is an abstract class in java.
A java
interface is like an abstract class that has only:
- final attributes (constant)
- no full implemented methods (only abstract
methods)
In fact, an
interface in java has only:
- final attributes
- abstract methods
In
addition, an interface may inherit from many java interfaces unlike
classes that have to inherit from a unique parent class.
In java,
multiple inheritances in the context of classes is not allowed while it is
allowed for interfaces.
If you
don’t know what the concept of inheritance is, please feel free to check our
Also, a
class can implement many interfaces with no problem.
JAVA interface definition (h2)
The syntax
of a java interface is the following:
interface
I {
//final
attributes
final
int a =100;
final
static int b = 300;
//abstract
methods
public
abstract int f(float p);
}
The only
visibilities that can be applied to an interface are public or nothing (package
visibility).
This is the
same rule applied for java classes.
By default,
the methods declared in an interface are public and abstract.
So, we can
omit to mention the keywords public and abstract in the signatures of the
interface ‘s methods.
But, we
recommend mentioning these two keywords in the signature of the methods because
it is a best practice.
JAVA interface implemented by a class (h2)
When
defining a class, we can mention that it implements an interface using this
syntax:
class
A implements I {
//
The class A must implement all the methods of the interface I
}
The class A
must implement all the methods of the interface I.
Otherwise,
we will get a compiler error.
In
conclusion, when a class implements an interface, we are sure that it
implements all the methods present in that interface.
So, the
java interface is like a contract that the class will respect.
In the
following, we will present a class that implements two interfaces. It may
implement more than two interfaces. But in our case we will take the example of
two interfaces.
This is the
second interface:
interface
J {
//abstract
methods
public
abstract float g();
}
This is the
class B that implements the interfaces I and J:
class
B implements I, J {
//
The class A must implement all the methods of the interfaces I and J
}
Here, the
class B implements two interfaces (I and J) separated by comma.
It must
implement all the methods of the interfaces I and J.
If a class
implements an interface, it can access any constant defined by this interface.
If the
constant defined in an interface is static, it can be accessed directly using
the name of the interface.
So, the
constant b defined in the interface I can be accessed like this: I.b
JAVA interface implemented by a class (h2)
If you are
not familiar with polymorphism, you can check our java polymorphism tutorial.
In context
of inheritance, we can declare a variable of a parent type.
Then, we
can assign to it an object of a child type.
We can do
something like that using an interface in java:
I
a;
We can
declare a variable whose type is an interface.
Also, we
can assign any class that implements the interface I to the variable a.
So, we can
assign an object of type A to the variable a:
a
= new A();
In fact, we
can even assign a java
anonymous class to the variable a.
The inheritance between java interfaces (h2)
An
interface can inherit from many interfaces unlike the classes.
So,
multiple inheritance between interfaces is allowed in java.
An
interface that inherits from two other interfaces I and J contains all the
declarations of all the methods of I an J.
Java
interface inheritance is a simple grouping of method declarations.
interface
K extends I, J {
//contains
the declaration of all the methods of both I an J
}
JAVA interfaces and naming conflicts (h2)
Let’s
consider the following example.
This is the
first interface:
interface
P {
public
abstract int l(int a);
public
abstract void q();
}
This is the
second interface:
interface
M {
//abstract
methods
public
abstract int l(float a);
public
abstract void q();
}
Let’s
consider the T class that implements the two interfaces P and M:
class
T implements P, M {
}
In this
case, the class T must implement these methods:
- The method l of the interface P
- The method l of the interface Q
- The method q (because it has the same
signature in the two interfaces)
The method
l is implemented in the class T two times because the two signatures of this
method in the two interfaces respect java method overloading rules.
JAVA interfaces and default method implementation
in JAVA 8 (h2)
JAVA 8
allows to an interface to give a default implementation for a method.
The syntax
is the following:
interface
D {
default
int f(){
//default
implementation of the interface
}
}
Here, a
class that implements the D interface may or may not give an implementation of
the method f(). In the second case, it will use the default implementation
offered by the interface D.
Java interface: conclusion (h2)
This java
interface tutorial arrives at its end.
We have
explained the interface concept with many examples.
If this
java tutorial was helpful for you, please check our core java tutorial.
You can like our how to program facebook page for more tutorials or follow our how to program Google plus page.
Post a Comment