In the earlier post Interfaces, we learned about the interfaces and their implementations. And we also learned about, how we can implement an interface partially by an abstract class and then inheriting the abstract class by another class to complete the implementation process: Partial Implementation of an interface.
We can also define an interface inside a class. The interface defined in the class is called as a nested / inner interface and is public and static implicitly. The inner interface can be implemented in its outer-class itself or can also be implemented by other classes outside its enclosing class.
Suppose, there is a class Keyboard which provides the functionality of all the available keys on the keyboard. The class Keyboard has an interface named as KeyAction which provides two methods for all kind of the keys on the keyboard - hold() and press(). Each pair of keys has their own functionalities when they are press or hold, but they call same methods hold() and press().
An inner interface KeyAction in the class Keyboard is declared in the following statements -
interface KeyAction {
void hold();
void press();
}
There are three implementations of the interface KeyAction - AlphaKeys, FuncKeys and NumKeys:
AlphaKeys - It's a non-static class in the class Keyboard and implements the interface KeyAction. The class represents the alphabetic keys on the keyboard. Learn more about Non-Static Inner Classes.
// Non-static Class AlphaKeys implementing the interface KeyAction
class AlphaKeys implements KeyAction {
AlphaKeys() {
System.out.println("");
System.out.println("Alphabet Keys");
}
public void hold() {
System.out.println("Holding the alphabet keys");
}
public void press() {
System.out.println("Pressing the alphabet keys");
}
}
FuncKeys - It's a static class in the class Keyboard and implements the interface KeyAction. The class represents the function keys on the keyboard. Learn more about Static Inner Classes.
// Static Class FuncKeys implementing the interface KeyAction
static class FuncKeys implements KeyAction {
FuncKeys() {
System.out.println("");
System.out.println("Function Keys");
}
public void hold() {
System.out.println("Holding the function keys");
}
public void press() {
System.out.println("Pressing the function keys");
}
}
NumKeys - It's a class outside from the class Keyboard and implements the interface KeyAction. The class represents the numeric keys on the keyboard.
// Class NumKeys implementing the interface KeyAction
public class NumKeys implements Keyboard.KeyAction {
NumKeys() {
System.out.println("");
System.out.println("Numeric Keys");
}
public void hold() {
System.out.println("Holding the Numeric keys");
}
public void press() {
System.out.println("Pressing the Numeric keys");
}
}
Class KeyboardDemo has main method and creates objects for all three classes implementing the inner interface KeyAction. Each class is implementing the same interface, but the object is created in different ways according to their accessibility.
AlphaKeys - The class AlphaKeys is a non-static class declared in the class Keyboard.
// Create an object of non-static inner class AlphaKeys
Keyboard.AlphaKeys ak = new Keyboard().new AlphaKeys();
// Above statement is same as -
/*
Keyboard k = new Keyboard();
AlphaKeys ak = k.new AlphaKeys();
*/
ak.hold();
ak.press();
To access this class, an object of the outer class Keyboard is created,
Keyboard k = new Keyboard();
and then the inner class AlphaKeys constructor is accessed by using the dot (.) operator.
AlphaKeys ak = k.new AlphaKeys();
Both above statements can be written in one statement too.
Keyboard.AlphaKeys ak = new Keyboard().new AlphaKeys();
FuncKeys - The class FuncKeys is a static class declared in the class Keyboard.
// Create an object of static inner class FuncKeys
Keyboard.FuncKeys fk = new Keyboard.FuncKeys();
fk.hold();
fk.press();
As this class is static, to access this class, we don't need to create an object of the outer class Keyboard, the inner class FuncKeys constructor is directly called by the class Keyboard itself by using dot operator.
Keyboard.FuncKeys fk = new Keyboard.FuncKeys();
NumKeys - The class NumKeys is a class declared outside from the class Keyboard.
// Create an object of class NumKeys, which is not inner class
NumKeys nk = new NumKeys();
nk.hold();
nk.press();
As this class is outer class, to access this class, we don't need to access the class Keyboard, we can just create an object of the outer class NumKeys itself.
NumKeys nk = new NumKeys();
Here is the full code of the three classes - Keyboard.java, NumKeys.java and KeyboardDemo.java
// Class Keyboard which has interface KeyAction declared in the class itself
public class Keyboard {
// Declared the interface KeyAction
interface KeyAction {
void hold();
void press();
}
// Non-static Class AlphaKeys implementing the interface KeyAction
class AlphaKeys implements KeyAction {
AlphaKeys() {
System.out.println("");
System.out.println("Alphabet Keys");
}
public void hold() {
System.out.println("Holding the alphabet keys");
}
public void press() {
System.out.println("Pressing the alphabet keys");
}
}
// Static Class FuncKeys implementing the interface KeyAction
static class FuncKeys implements KeyAction {
FuncKeys() {
System.out.println("");
System.out.println("Function Keys");
}
public void hold() {
System.out.println("Holding the function keys");
}
public void press() {
System.out.println("Pressing the function keys");
}
}
}
// Class NumKeys implementing the interface KeyAction
public class NumKeys implements Keyboard.KeyAction {
NumKeys() {
System.out.println("");
System.out.println("Numeric Keys");
}
public void hold() {
System.out.println("Holding the Numeric keys");
}
public void press() {
System.out.println("Pressing the Numeric keys");
}
}// Class KeyboardDemo for demo of all three classes
public class KeyboardDemo {
public static void main(String args[]) {
// Create an object of non-static inner class AlphaKeys
Keyboard.AlphaKeys ak = new Keyboard().new AlphaKeys();
// Above statement is same as -
/*
Keyboard k = new Keyboard();
AlphaKeys ak = k.new AlphaKeys();
*/
ak.hold();
ak.press();
// Create an object of static inner class FuncKeys
Keyboard.FuncKeys fk = new Keyboard.FuncKeys();
fk.hold();
fk.press();
// Create an object of class NumKeys, which is not inner class
NumKeys nk = new NumKeys();
nk.hold();
nk.press();
}
}
Here is the output of above code -
Alphabet Keys
Holding the alphabet keys
Pressing the alphabet keys
Function Keys
Holding the function keys
Pressing the function keys
Numeric Keys
Holding the Numeric keys
Pressing the Numeric keys
The class Keyboard can be used to add more advanced keys on the keyboard like symbol keys, volume keys, arrow keys and other language keys, using the same interface KeyAction.
You can find code at Github link.