In previous topics we learn the following concepts:
Interface takes the above concepts even further. It provides a mechanism to define a class with absolutely no implementation (code for execution of a method or logic).
In this chapter you will learn more about interfaces, its syntax and use, the difference between interfaces and abstract class and when to use which.
MORE ABOUT ‘INTERFACE’ :
One or more classes can implement a defined interface
When a class implements a defined interface, it has to implement (write the code, execution logic) for all the methods defined by the interface. The class is free to define more methods if necessary.
Similarly, you could have other classes inherit from the same interface MusicPlayer. Examples –
Syntax of Interface To define an interface, use the interface keyword instead of the class keyword.
//SYNTAX: package xxx.xxx; interface MusicPlayer{ // Cannot have method implementations: void on(); void off(); void play(); void stop(); }
Points to note above :
is public, private or protected. A private interface makes no sense. If not defined the above interface is visible in the package where the interface belongs. You can define an interface public – which means the interface is visible outside the package as well.
Methods inside the interface are public by default. So in the above example, the methods are public and visible outside of the package as well.
The class which inherits the methods must explicitly define the methods to be public.
//SYNTAX: class MP3Player implements MusicPlayer{ public void on(){ System.out.println(“the MP3 Player is ON”); } public void off(){ System.out.println(“the MP3 Player is OFF”); } public void play(){ System.out.println(“the MP3 Player is playing”); } public void stop(){ System.out.println(“the MP3 Player is off”); } }
In Java, there is nothing which prevents from inheriting from multiple interfaces. Since there are no implementations in the methods (code in the methods), there is no danger or overwriting any implementations between multiple interfaces.
// Multiple interfaces. interface MusicPlayer { void on(); void off(); void play(); void stop(); } } interface VideoPlayer{ void on(); void off(); void play(); void stop(); void changeContrast(int x); void changeBrightness(int x); } } class iPod implements MusicPlayer, VideoPlayer{ public void on(){ System.out.println(“the MP3 Player is ON”); } public void off(){ System.out.println(“the MP3 Player is OFF”); } public void play(){ System.out.println(“the MP3 Player is playing”); } public void stop(){ System.out.println(“the MP3 Player is off”); } public void changeContrast(int x){ System.out.println(“Constrast Changed by” + x); } public void changeBrightness(int x){ System.out.println(“Brightnesss Changed by” + x); } }
Interfaces are similar to abstract classes. The differences are as follows:
You can add new methods to an existing interface by extending it; and adding new methods.
In the above example, please note
So, if ElectronicDevices interface had one property – which is “powerSource”; it would be inherited by all classes which implement MusicPlayer or VideoPlayer
Example for practice :
Write a class that implements the CharSequence interface found in the java.lang package. Your implementation should return the string backwards. Select one of the sentences from this book to use as the data. Write a small main method to test your class; make sure to call all four methods.
Answer 1 : // CharSequenceDemo presents a String value -- backwards.
public class CharSequenceDemo implements CharSequence { private String s; public CharSequenceDemo(String s) { //It would be much more efficient to just reverse the string //in the constructor. this.s = s; } private int fromEnd(int i) { return s.length() - 1 - i; } public char charAt(int i) { if ((i < 0) || (i >= s.length())) { throw new StringIndexOutOfBoundsException(i); } return s.charAt(fromEnd(i)); } public int length() { return s.length(); } public CharSequence subSequence(int start, int end) { if (start < 0) { throw new StringIndexOutOfBoundsException(start); } if (end > s.length()) { throw new StringIndexOutOfBoundsException(end); } if (start > end) { throw new StringIndexOutOfBoundsException(start - end); } StringBuilder sub = new StringBuilder(s.subSequence (from End(end),from End (start))); return sub.reverse(); } public String toString() { StringBuilder s = new StringBuilder(this.s); return s.reverse().toString(); } //Random int from 0 to max. private static int random(int max) { return (int) Math.round(Math.random() * max + 0.5); } public static void main(String[] args) { CharSequenceDemo s = new CharSequenceDemo("Write a class that implements the CharSequence interface found in the java.lang package."); //exercise charAt() and length() for (int i = 0; i < s.length(); i++) { System.out.println(s.charAt(i)); } //exercise subSequence() and length(); int start = random(s.length() - 1); int end = random(s.length() - 1 - start) + start; System.out.println(s.subSequence(start, end)); //exercise toString(); System.out.println(s); } }
All Rights Reserved. © 2024 BookOfNetwork