Interfaces In Java Language

Interfaces :

In previous topics we learn the following concepts:

  • Abstract class, which allows you to create methods in a class without writing the code for execution of the method (implementation of the method).
  • Inheritance through the keyword ‘extends’ which tells the machine that an (inherited) class defined is of the type of a base class.
  • Methods in the inherited class must provide implementation. (except when the inherited class is an Abstract class as well.

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

One or more classes can implement

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.

class implements a defined interface

Similarly, you could have other classes inherit from the same interface MusicPlayer. Examples –

interfave example in java

Syntax of Interface To define an interface, use the interface keyword instead of the class keyword.

Syntax
//SYNTAX:
package xxx.xxx;
interface MusicPlayer{
// Cannot have method implementations:
void on();
void off();
void play();
void stop();
}

Points to note above :

  • A semicolon after the method definition
  • No implementation logic in the method above
  • interface keyword instead of class

ACCESS :

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
//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”);
}
}

MULTIPLE INHERITANCE :

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 INHERITANCE in java
CODE/PROGRAM/EXAMPLE
// 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 AND ABSTRACT CLASSES :

Interfaces are similar to abstract classes. The differences are as follows:

  • 1. All methods in an interface are abstract. Which means all methods must be empty; no code implemented.
  • 2. In abstract class, the methods can have code/implementation within it. Atleast one method must be abstract.
  • 3. All properties (data fields) in an interface are static final. Properties in an abstract class need not be static final.
  • 4. Interfaces are implemented(implements keyword); Abstract classes are extended(extends keyword)
  • 5. Class can extend only one abstract class; where as a class can implement multiple interfaces (multiple inheritance)
  • 6. Contractual obligation: When a class specifies that it implements an interface, it must define all methods of that interface. A class can implement many different interfaces. If a class doesn't define all methods of the interfaces it agreed to define (by the implements clause), the compiler gives an error message, which typically says something like "This class must be declared abstract". An abstract class is one that doesn't implement all methods it said it would. The solution to this is almost always to implement the missing methods of the interface. A misspelled method name or incorrect parameter list is the usual cause, not that it should have been abstract!

INHERITANCE WITHIN INTERFACES :

You can add new methods to an existing interface by extending it; and adding new methods.

INHERITANCE WITHIN INTERFACES  in java

In the above example, please note

  • ElectronicDevices is an interface.
  • MusicPlayer and VideoPlayer are interfaces which “extend” ElectronicDevices
  • iPod is a class which implements MusicPlayer and VideoPlayer

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.

CODE/PROGRAM/EXAMPLE
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);
}
}
#interfaces_in_java_language #interfaces_in_java_language #Abstract_class_in_java #multiple_inheriting_in_java #inheriting__within_interfaces__in_java

(New page will open, for Comment)

Not yet commented...