Introduction to Interfaces
Sometimes we want a class to inherit from more than one classes. This is called multiple inheritance. In Java, subclass must be inherited from one super class, but a subclass can implement multiple interfaces. So multiple inheritance is accomplished indirectly using an “interface.”
A Java interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behavior.
An interface is a collection of abstract methods that's why it is also called pure abstract class.
A class implements an interface, inheriting all the abstract methods declared in the interface. Therefore, a class that implements an interface must either override the interface methods or the class must be declared abstract.
An interface has the following properties:
- An interface is defined in a .java file. If the interface is public, the name of the file must match the name of the class. If the interface has the default access, it is only accessible from within its package.
- All the methods in an interface are abstract, whether or not the abstract keyword is explicitly denoted.
- All the methods in an interface are public, whether or not they are explicitly declared public.
- The fields of an interface are public, static, and final.
- An interface cannot declare static methods.
An interface has some similarities to a class, but an interface is not a class. For example, an interface cannot be instantiated, and it cannot contain any instance fields.
To create an interface, use the interface keyword instead of the class keyword. To make a class that conforms to a particular interface (or group of interfaces) use the implements keyword. You’re saying “The interface is what it looks like but now I’m going to say how it works.”
A remote control is an interface between you and a television set. We know by the remote control interface that what actions the TV can perform. The remote control is also used to operate TV. Similarly, a Java interface is a device that unrelated objects use to interact with one another.
Syntax
interface MyInterface {
statement(s);
}
Example
1. /**
2. * @(#)InterfaceDemo.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/20
7. */
8.
9. interface RemoteControl {
10.
11. public void on();
12. public void off();
13. public void tune();
14. public void increaseVolume();
15. public void decreaseVolume();
16. public void nextChannel();
17. public void previousChannel();
18. }
19.
20. class TV implements RemoteControl {
21.
22. public void on() {
23.
24. System.out.println("TV is turned on.");
25.
26. }
27.
28. public void off() {
29.
30. System.out.println("TV is turned off.");
31.
32. }
33.
34.
35. public void tune() {
36.
37. System.out.println("Tune TV channels.");
38.
39. }
40.
41. public void increaseVolume() {
42.
43. System.out.println("Volume high.");
44.
45. }
46.
47. public void decreaseVolume() {
48.
49. System.out.println("Volume low.");
50.
51. }
52.
53. public void nextChannel() {
54.
55. System.out.println("Switch on next channel.");
56.
57. }
58. public void previousChannel() {
59.
60. System.out.println("Switch on previous channel.");
61.
62. }
63.
64. }
65.
66. public class InterfaceDemo {
67.
68. /**
69. * Creates a new instance of InterfaceDemo.
70. */
71. public InterfaceDemo() {
72. }
73.
74. /**
75. * @param args the command line arguments
76. */
77. public static void main(String[] args) {
78. // TODO code application logic here
79.
80.
81. RemoteControl remoteControl = new TV();
82.
83. remoteControl.on();
84. remoteControl.tune();
85. remoteControl.nextChannel();
86. remoteControl.previousChannel();
87. remoteControl.increaseVolume();
88. remoteControl.decreaseVolume();
89. remoteControl.off();
90.
91. }
92. }
Output
TV is turned on.
Tune TV channels.
Switch on next channel.
Switch on previous channel.
Volume high.
Volume low.
TV is turned off.
