Java Interface, As discussed in the previous chapter interface is used to get full abstraction , to solve the problem of multiple Inheritance, and by default interface is an abstraction. Java interface also follows IS-A relationship. The main advantage of the interface is to get the loose coupling. Following some points needs to be remember.
Static and final keywords cannot be used inside an interface.
Local variables inside interface is not possible because local variables are local to methods and method inside interface is not defined because local variables are declared in the method definition.
The interface cannot be inherited from class.
A class can inherit from Java Interface by using “implements” keyword.
Conceptual
figure
A class can implement the interface.
Example
[java]
interface printable{
void print();
}
class splesson implements printable{
public void print(){System.out.println("Hello Splesson.");}
public static void main(String args[]){
splesson obj = new splesson();
obj.print();
}
}
[/java]
Where printable is an interface and it has only one method that is print().
[java]interface printable{
void print();}[/java]
Output:
When compile the code the result will be as follows.
[c]
Hello Splesson.
[/c]
Example
[java]
package com.spl.absI;
public class AbstractInterface {
public static void main(String[] args) {
System.out.println("Program starts");
DerivedClass dref=new DerivedClass();
dref.test1();
dref.test2();
System.out.println("Program ends");
}
}
interface Sample1
{
void test1();
void test2();
}
abstract class A implements Sample1
{
public void test1()
{
System.out.println("test1() method");
}
}
class DerivedClass extends A
{
@Override
public void test2() {
// TODO Auto-generated method stub
System.out.println("test2() method");
}
}
[/java]
If a class implements a Java Interface, a contract exist between class and interface.
The contract is the class which has to define all the interface methods otherwise the class is abstract.
Output:
When compile the program following is the output will be displayed.
[c]
Program starts
test1() method
test2() method
Program ends
[/c]
Interface-extends
Description
An Java Interface can inherit from another interface using "extends" keyword. Following is the syntax.
[java]interface Sample1{
void test1(int a);
}
interface Sample2 extends Sample1{
void test2();
}[/java]
Interface and inheritance
Description
A class can implement more than one interface at a time. In this case, the class has to provide definition for all the methods of each interface. A class can inherit from another class and implements more than one interface.
For more detailed overview on inheritance click here .
Syntax
Class DerivedClass extends Baseclass implements Interface1,Interface2
{
}
Example
[java]
package com.spl.concrete;
public class Interface3 {
public static void main(String[] args) {
System.out.println("Program starts");
B dref=new B();
dref.test1();
System.out.println("Program ends");
}
}
class BaseClass
{
public void test1()
{
System.out.println("Running test1() mehod");
}
}
interface Sample1
{
void test1();
}
class B extends BaseClass implements Sample1
{
//inheriting from both Sample1 and BaseClass
}
[/java]
In the above example, the class B is becoming concrete because the test 1() method inherited from base class is full filling the contract of Sample 6 interface.
Output:
[c]
Program starts
Running test1() method
Program ends[/c]
Example
[java]
package com.spl.int4;
public class Interface4 {
public static void main(String[] args) {
System.out.println("Program starts");
E dref=new E();
dref.test1();
System.out.println("Program ends");
}
}
interface Sample7
{
void test1();
}
interface Sample8
{
void test1();
}
class E implements Sample7,Sample8
{
@Override
public void test1() {
// TODO Auto-generated method stub
System.out.println("Running test1() method");
}
}
[/java]
In the above example, the class is becoming concrete by overriding only test1() method.The overrided method fulfills the contract of both Sample 7 & Sample 8 interface,because the method signature are same.
Output:
When compile the code output will be as follows.
[c]
Program starts
Running test1() method
Program ends
[/c]
Multiple Inheritance By Using Interface
The following is an example to achieve multiple inheritance by using interface.
[java]package com.splessons;
interface Printable{
void print();
}
interface Showable{
void show();
}
class Splesson implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome To Splessons");}
public static void main(String args[]){
Splesson obj = new Splesson();
obj.print();
obj.show();
}
} [/java]
In the above example, Printable and Showable are the two interfaces that have their own methods and where Splesson is the class name that implements two interfaces(there is no ambiguity).
Output:
[java]Hello
Welcome To Splessons[/java]
Advantages of interfaces
Points
The following are the some advantages of interfaces.
Interface is used to set the standardization.
It is used to set the generalization.
Java interfaces are used to define standards for subclass which implements it.
If any subclass failed to meet the standards, then that subclass cannot exist.
The interface is used to achieve the generalization of the behaviors across the sub classes while developing an application. If one comes across any features, which needs to be implemented in a different way then go for interfaces.
Developing the extensibility of application features is easy and maintenance is also easy by interfaces.
Using interfaces one can achieve 100% abstraction which also helps us to add new features without breaking the existing features.
The interfaces are very useful in developing API’s so that customization and enhances becomes easy.
Summary
Key Points
Multiple inheritance problem will be done by interface.
100% of abstraction will be achieved through an interface.