Interface in Java
What is an interface?
1.
Interface looks like
class but it is not a class.
2.
An interface can have
methods and variables just like the class but the methods declared in interface
are by default abstract (only method signature, no body).
3.
Also, the variables declared
in an interface are public, static & final by default.
Syntax:
access_specifier
interface interface_name {
data_type
var1 = value1;
data_type
var2 = value2;
data_type
varN = valueN;
return_type
method1(parameter_list);
return_type
method2(parameter_list);
return_type
methodN(parameter_list);
}
What is the use of interfaces?
As mentioned above they are used for abstraction. Since methods
in interfaces do not have body, they have to be implemented by the class before
you can access them.
The class that implements interface must implement all the
methods of that interface. Also, java programming language does not support
multiple inheritance, using interfaces we can achieve this as a class can
implement more than one interfaces, however it cannot extend more than one
classes.
Suppose
you are designing a library system which has library manager class and you have
a research thesis class which contains a group of functions and variables that
describe its characteristics. The research thesis class will have a
relationship to the manager class. The manager class will need determined set
of information from each library item and doesn’t care about its class. Note
that any library could have many items such as research papers, books, magazines
and CDs. In hope to do that the manager class will force all the library items
to implements a specific interface which includes all the needed variables and
functions prototypes.
By
this way, the interface will works as communication protocol or a set of rules
between the library manager class and the library items classes. The manager
class design will be built with the usage of interface properties whatever the
item class type. You even can finish the implementation of the library manager
program without the need to wait for finishing items classes’ implementation.
You can also add any new item class with no needs for manger class changes. In
the other side all the items will implements the interface and each item can
has its unique implementation. Usage of interface in this example avoids the
non-needed classes’ relationships by finding the similarities between unrelated
classes. Interfaces also save the time of rewriting the functions prototypes
creating reusable declarations.
Some
hints about Java Interfaces
Ø
Interface functions
should be public and abstract.
Ø
Interface fields
should be public and final.
Ø
Use the Keyword interface to
define an interface.
Ø
If you define a public interface
with name myInterface the java file should be named as myInterface.java (Similar
to public class definition rules).
Ø
A class implementing
an interface should use the keyword implements.
Ø
No objects can be
created from an interface.
Ø
Interfaces don't have
constructors as they can't be initiated
Ø
An Interface can
extends one or more interfaces.
Ø
You can define a
reference of type interface but you should assign to it an object instance of
class type which implements that interface.
Key points: Here are the key points to remember
about interfaces:
1)
We
can’t instantiate an interface in java.
2)
Interface
provides complete abstraction as none of its methods can have body.
On the other hand, abstract
class provides partial
abstraction as it can have abstract and concrete (methods with body) methods
both.
3)
Implements
keyword is used by classes to implement
an interface.
4)
While
providing implementation in class of any method of an interface, it needs to be
mentioned as public.
5)
Class
implementing any interface must implement all the methods, otherwise the class
should be declared as “abstract”.
6)
Interface
cannot be declared as private, protected or transient.
7)
All
the interface methods are by default abstract
and public.
8)
Variables
declared in interface are public,
static and final by
default.
9)
Interface variables must be initialized at the
time of declaration otherwise compiler will through an error.
10)
Inside any implementation class, you cannot
change the variables declared in interface because by default, they are public,
static and final. Here we are implementing the interface “Try” which has a
variable x. When we tried to set the value for variable x we got compilation
error as the variable x is public static final by default and final variables cannot
be re-initialized.
11)
Any
interface can extend any interface but cannot implement it. Class implements
interface and interface extends interface.
12)
A class can implement any number of interfaces.
13)
If
there are two or more same methods in two interfaces and a class implements
both interfaces, implementation of the method once is enough.
14)
A class cannot implement two interfaces that
have methods with same name but different return type.
15)
Variable names conflicts can be resolved by
interface name.
Benefits of having interfaces:
Following are the advantages of using
interfaces:
1. Without
bothering about the implementation part, we can achieve the security of
implementation
2. In
java, multiple inheritance is
not allowed, However by using interfaces you can achieve the same. A class can
extend only one class but can implement any number of interfaces. It saves you
from Deadly Diamond of Death (DDD) problem.
Difference between an
interface and an abstract class?
Abstract class
|
Interface
|
Abstract class is a
class which contain one or more abstract methods, which has to be implemented
by its sub classes.
|
Interface is a Java
Object containing method declaration but no implementation. The classes which
implement the Interfaces must provide the method definition for all the methods.
|
Abstract class is a
Class prefix with an abstract keyword followed by Class definition.
|
Interface is a pure
abstract class which starts with interface keyword.
|
Abstract class can
also contain concrete methods.
|
Whereas, Interface
contains all abstract methods and final variable declarations.
|
Abstract classes are
useful in a situation that Some general methods should be implemented and
specialization behavior should be implemented by child classes.
|
Interfaces are useful
in a situation that all properties should be implemented.
|
Comments
Post a Comment