Interfaces can be defined as blueprints used to
support complete abstraction in Java. Using interfaces you can completely
abstract a class. The syntax of interfaces is similar to that of classes, but
interfaces will not have instance variables and they contain only abstract
methods. An interface can be used by any number of classes. The interfaces
support polymorphism principle of Java. One class can implement any number of
interfaces.
General Form
below shows, How to create Interfaces?
access-specifiers interface NameOfInterface {
data-type variable1=value;
data-type variable2=value;
……..
data-type variable=value;
type MethodName1(parameter-list);
type MethodName2(parameter-list);
………….
Type MethodNameN(parameter-list);
}
In the above general form you can see, the interface keyword after the access-specifier and before the interface-name. The variables are both
declared and initialized. The initializing of interface variables is must
because these variables are implicitly final
and static. The methods defined
inside any interface will be abstract method. The concrete methods are not
allowed.
Rules to keep in mind
when creating an Interface:
1. The interface can have only public or default access specifier.
2. The variables declared inside any
Interface will be implicitly final and
static. These variables must be
initialized after declaration.
3. Interfaces will only contain abstract
methods. They cannot contain concrete methods.
4. All the methods and variables of any
interface are implicitly public.
Implementation of
Interface by a Class:
In the below syntax you can see how a class can
implement interfaces. The implements
keyword is used to include an interface by a class.
class ClassName [extends superclass][implements interface1[,interface2…]] {
//body of class
}
Rules to follow when
implementing an Interface by any class:
1. Any class that implements an Interface must implement all the
methods that are defined by the interface.
2. If a class implements more than one
interfaces than the interfaces are separated with comma. You can refer the above
general syntax.
3. The methods that are implementing
methods defined in an interface must be declared public.
4. The implementing methods must have
the same type signatures as that of the methods defined by the interface.
Note: You cannot create an object of type Interface. But it is
possible to create reference variables for an Interface which can hold the
references of the objects which implements it.
The below
simple example program shows how to create and use an Interface.
package blog.Interface.example;
public interface SampleInterface
{
int a=10;
int b=20;// interface variables
are by default final and static.
int addmethod(int x,int y);
void callMeMethod();
}
package blog.Interface.example;
public class SampleInterfaceClass implements SampleInterface
{
int intfVar1=a;
int intfVar2=b;
@Override
public int addmethod(int x, int y)
{
System.out.println("Am addmethod
defined by Interface");
return x+y;
}
@Override
public void callMeMethod()
{
System.out.println("Am callMeMethod
defined by interface");
System.out.println();
}
void amConcreteMethod()// concrete are legal.
{
System.out.println("Am concrete method
in a class that implements inteface");
System.out.println("value of Interface
variable 'a' "+intfVar1);
System.out.println("value of Interface
variable 'b' "+intfVar2);
}
}
package blog.Interface.example;
public class SampleInterfaceDemo
{
public static void main(String[] args)
{
SampleInterfaceClass sic=new SampleInterfaceClass();
int sum=sic.addmethod(10, 20);
System.out.println("sum(x,y)= "+sum);
System.out.println();
sic.callMeMethod();
sic.amConcreteMethod();//calling concrete method of SampleIntefaceClass.
}
}
Output:
Am addmethod defined by
Interface
sum(x,y)= 30
Am callMeMethod defined
by interface
Am concrete method in a
class that implements inteface
value of Interface
variable 'a' 10
value of Interface
variable 'b' 20
Note: In a situation where a class implements Interface and its
does not overrides all the methods defined by the Interface
then such a class must be declared abstract.
package blog.Interface.example;
public abstract class SamplePartialInterface implements SampleInterface
{
int a, b;// variables of class
@Override
public void callMeMethod()
{
System.out.println("only callMethod is
called and addmethod is not called");
System.out.println("Make this call
abstract");
}
}
In the
above example you can see the class ‘SamplePartialInterface’ implements only one method from
the above interface ‘SampleInterface’. In such case implementing class must be
declared as abstract.
Nested Interfaces:
An interface can
be declared inside a class or within another interface. Such interfaces are
called as nested Interfaces. A nested interface can be modified using access
specifiers, public, private and protected.
In the below example program you can see
the use of nested interface
package blog.Interface.example;
public class X
{
int x; // variable of class X
public interface NestInf
{
void NestInfMethod();
}
}
package blog.Interface.example;
public class Y implements X.NestInf
{
@Override
public void NestInfMethod()
{
System.out.println("Am method from
Nested Interface in class X");
}
public static void main(String[] args)
{
X.NestInf nestInf=new Y();
nestInf.NestInfMethod();
}
}
Output:
Am method from Nested
Interface in class X.
Interfaces can inherit
each other:
One interface can inherit another interface in the
same way as one class inherit the other class by using extends keyword.
Consider
below code fragment shows, the Interface Y inherits interface X.
package blog.Interface.example;
public interface X
{
void my1();
void my2();
}
package blog.Interface.example;
public interface Y extends X
{
void myY1();
void myY2();
}
Note: If a class implements an interface
that has inherited another interface then the class must provide implementations
to all the methods defined within the interface inheritance chain.