Interface in java is core part of its programming language despite that many programmers thinks Java Interface as an advanced concept and refrain using interfaces from early in programming career. At very basic level interface in java is a keyword but same time it is an object oriented term to define contractsand abstraction , This contract is followed by any implementation of Interface in Java. Since multiple inheritance is not allowed in Java, interfaceis only way to implement multiple inheritance at Type level. In this Java tutorial we will see What is an interface in Java, How and where to use interface in Java and some important points related to interface in Java.
1. Interface in java is declared using keyword interface and it represent a Type like any Class in Java. a reference variable of type interface can point to any implementation of that interface in Java. Its also a good design principle to "program for interfaces than implementation" because when you use interface to declare reference variable, method return type or method argument you are flexible enough to accept any future implementation of that interface which could be much better and high performance alternative of current implementation. similarly calling any method on interface doesn't tie you with any particular implementation and you can leverage benefit of better or improved implementation over time.
2) All variables declared inside interface is implicitly public final variable or constants. which brings a useful case of using Interface for declaring Constants. We have used both Class and interface for storing application wide constants and advantage of using Interface was that you can implement interface and can directly access constants without referring them with class name which was the case earlier when Class is used for storing Constants. Though after introduction of static imports in Java 5 this approach doesn't offer any benefit over Class approach.
3) All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword. you can not define any concrete method in interface. That's why interface is used to define contracts in terms of variables and methods and you can rely on its implementation for performing job.
4) In Java its legal for an interface to extend multiple interfaces. for example following code will run without any compilation error:
interface Session extends Serializable, Clonnable{ }
here Session interface in Java is also a Serializable and Clonnable. This is not true for Class and one Class can only extend at most another Class. In Java one Class can implement multiple interfaces. They are required to provide implementation of all methods declared inside interface or they can declare themselves as abstract class.
Java standard library itself has many inbuilt interfaces like Serializable, Clonnable, Runnable or Callable interface in Java. Declaring interface is easy but making it correct in first attempt is hard but if you are in business of designing API then you need to get it right in first attempt because its not possible to modify interface once it released without breaking all its implementation. here is an example of declaring interface in Java :
interface SessionIDCreator extendsSerializable, Cloneable{
String TYPE = "AUTOMATIC";
int createSessionId();
}
class SerialSessionIDCreator implements SessionIDCreator{
private int lastSessionId;
@Override
publicint createSessionId() {
return lastSessionId++;
}
}
In above example of interface in Java, SessionIDCreator is an interface while SerialSessionIDCreator is a implementation of interface. @Override annotation can be used on interface method from Java 6 onwards, so always try to use it. Its one of those coding practice which should be in your code review checklist.
Interface is best choice for Type declaration or defining contract between multiple parties. If multiple programmer are working in different module of project they still use each others API by defining interface and not waiting for actual implementation to be ready. This brings us lot of flexibility and speed in terms of coding and development. Use of Interface also ensures best practices like "programming for interfaces than implementation" and results in more flexible and maintainable code. Though interface in Java is not the only one who provides higher level abstraction, you can also use abstract class but choosing between Interface in Java and abstract class is a skill. Difference between Interface in Java and abstract class in java is also a very popular java interview question.
That's it for now on Interfacein Java, specifics of Java interface and How and when to use Interface in Java. Interface is key to write flexible and maintainable code. If you are not yet using interface in your code than start thinking in terms of interfaces and use it as much possible. You will learn more about interfaces when you start using design patterns. many design patterns like decorator pattern, Factory method pattern or Observer design pattern makes very good use of Java interfaces.
Key Points about Interface in Java
1. Interface in java is declared using keyword interface and it represent a Type like any Class in Java. a reference variable of type interface can point to any implementation of that interface in Java. Its also a good design principle to "program for interfaces than implementation" because when you use interface to declare reference variable, method return type or method argument you are flexible enough to accept any future implementation of that interface which could be much better and high performance alternative of current implementation. similarly calling any method on interface doesn't tie you with any particular implementation and you can leverage benefit of better or improved implementation over time.
2) All variables declared inside interface is implicitly public final variable or constants. which brings a useful case of using Interface for declaring Constants. We have used both Class and interface for storing application wide constants and advantage of using Interface was that you can implement interface and can directly access constants without referring them with class name which was the case earlier when Class is used for storing Constants. Though after introduction of static imports in Java 5 this approach doesn't offer any benefit over Class approach.
3) All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword. you can not define any concrete method in interface. That's why interface is used to define contracts in terms of variables and methods and you can rely on its implementation for performing job.
4) In Java its legal for an interface to extend multiple interfaces. for example following code will run without any compilation error:
interface Session extends Serializable, Clonnable{ }
here Session interface in Java is also a Serializable and Clonnable. This is not true for Class and one Class can only extend at most another Class. In Java one Class can implement multiple interfaces. They are required to provide implementation of all methods declared inside interface or they can declare themselves as abstract class.
Example of interface in Java
Java standard library itself has many inbuilt interfaces like Serializable, Clonnable, Runnable or Callable interface in Java. Declaring interface is easy but making it correct in first attempt is hard but if you are in business of designing API then you need to get it right in first attempt because its not possible to modify interface once it released without breaking all its implementation. here is an example of declaring interface in Java :
interface SessionIDCreator extendsSerializable, Cloneable{
String TYPE = "AUTOMATIC";
int createSessionId();
}
class SerialSessionIDCreator implements SessionIDCreator{
private int lastSessionId;
@Override
publicint createSessionId() {
return lastSessionId++;
}
}
In above example of interface in Java, SessionIDCreator is an interface while SerialSessionIDCreator is a implementation of interface. @Override annotation can be used on interface method from Java 6 onwards, so always try to use it. Its one of those coding practice which should be in your code review checklist.
When to use interface in Java
Interface is best choice for Type declaration or defining contract between multiple parties. If multiple programmer are working in different module of project they still use each others API by defining interface and not waiting for actual implementation to be ready. This brings us lot of flexibility and speed in terms of coding and development. Use of Interface also ensures best practices like "programming for interfaces than implementation" and results in more flexible and maintainable code. Though interface in Java is not the only one who provides higher level abstraction, you can also use abstract class but choosing between Interface in Java and abstract class is a skill. Difference between Interface in Java and abstract class in java is also a very popular java interview question.
That's it for now on Interfacein Java, specifics of Java interface and How and when to use Interface in Java. Interface is key to write flexible and maintainable code. If you are not yet using interface in your code than start thinking in terms of interfaces and use it as much possible. You will learn more about interfaces when you start using design patterns. many design patterns like decorator pattern, Factory method pattern or Observer design pattern makes very good use of Java interfaces.
No comments:
Post a Comment