The concept of "interface" is similar to that of an abstract class. The main difference between an interface and an abstract class is that, an abstract class will have 1 or 2 methods (not all methods in a class) that are defined as "abstract". Where as, with interface, we can achieve complete abstraction of a class. Interface will have all the methods declared, there would be no method definitions (Method body/logic). Its up to a class that "implements" an interface to implement all the methods present in the interface.
Notice, the keywords used in the below example program. The keyword "interface" is used to define an interface, a class can implement an interface using the keyword "implements". All the methods defined in the class that implements interface, must be defined "public", notice the usage of the access specifier "public" in front of the method definitions.
interface movieInterface { void fillThePlotSummary(String summary); void directorName(String directorName); void movieRunTime(int lengthInMin); } class casinoRoyal implements movieInterface { String plotSummary; String directorName; int movieLength; public void fillThePlotSummary(String summary) { this.plotSummary = summary; } public void directorName(String directorName) { this.directorName = directorName; } public void movieRunTime(int lengthInMin) { this.movieLength = lengthInMin; } void printMovieDetails() { System.out.println(" Movie Name: Casino Royal"); System.out.println(" Plot Summary: " + plotSummary); System.out.println(" Movie Director: " + directorName); System.out.println(" Movie Length: " + movieLength); } } public class interfaceExample { public static void main(String[] args) { casinoRoyal obj = new casinoRoyal(); obj.fillThePlotSummary(" Casino Royal plot summary"); obj.movieRunTime(144); obj.directorName("Martin Campbell"); obj.printMovieDetails(); } }
Compile/Run this program and here is the output:
Movie Name: Casino Royal Plot Summary: Casino Royal plot summary Movie Director: Martin Campbell Movie Length: 144
An interface can also be used to define constants, variables etc. If you are familiar with "C" programming, this is similar to the way "#define" or constant are used. Example usage of interface for defining constants.
File Name: sharedConstants.java ------------------------------- public interface sharedConstants { String FIRST_PLACE = "Gold"; String SECOND_PLACE = "Silver"; String THIRD_PLACE = "Bronze"; }
The above constants defined in the interface are visible to any class that implements the interface "sharedConstants". An interface can be either "public" or "no access specifier". If an interface is defined as public, it should be in it own file, notice the name of the file name in the above example.
Like a class, an interface can also be extended. All the concepts of inheritance that are applicable to a class are also applicable to an interface. Also, a class can extend one or more classes and also implement one or more interfaces at the same time. Let us see that using the following figure.