What Is an Interface?
In English, an interface is a device or system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks. Similarly, a Java interface is a device that unrelated objects use to interact with one another. Java interfaces are probably most analogous to protocols (an agreed-upon behavior). In fact, other object-oriented languages have the functionality of Java's interfaces, but they call their interfaces protocols.
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.
Definition: An interface is a named collection of method definitions (without implementations). An interface can also include constant declarations.
Interfaces are best understood through examples, so let's look at a concrete example of an interface and two classes that use it to interact. Then we'll talk more about interfaces in the abstract and clear up some common confusion.
There is no multiple inheritance in Java. In other words, a class can only extend one superclass. Now, suppose we needed to have a Car inherit properties from "Vehicle" and "Motorized" at the same time:
Multiple inheritance: a car is both a vehicle and motorized.
public class Car extends Vehicle, Motorized {
// compiler error, only one superclass allowed
}
The Java solution to the multiple inheritance problem is the interface. An interface is kind of an abstract base class that can't have any "real code" (ie. no method implementation), and no variables (only constants). Like a class, an interface is defined in a file with the extension .java, as follows:public interface Motorized {
public abstract void start();
}
Knowing that it's not allowed to define variables or write method implementations in the code of an interface, the question arises: what can you do? The answer is found in its name: defining the interface of a class. Let's look at an example, a modified version of the Car class that implements the Motorized interface:package com.javala;The keyword implements expresses that a class implements an interface. Multiple interfaces can be implemented by the same class, the names of the interfaces are separated by commas. In the above example, the
public class Car extends Vehicle implements Motorized {
public Car(String brand, int yearsModel) {
super(brand, yearsModel);
}
public String giveSoundSample {
return "vrrroooommm";
}
public void start() {
System.out.println("brom-brom-brroooooom");
}
}
start()method of Car simply contains the printing of "brom-brom-brroooooom". Let's look at an example to see how interfaces are useful - we continue the example above and have Moped implement the Motorized interface as well:Car and Moped implement the Motorized interface - Rowboat does not.
Car volvo = new Car("Volvo", 96);
Moped tunturi = new Moped("Tunturi", 82);
Rowboat terhi = new Rowboat("Terhi", 62);
Vehicle something = tunturi; // nyt jokin == tunturi
if ( something instanceof Motorized ) {
// true, because it's a Moped
}
Motorized m = (Motorized) something; // success
m.start();
something = terhi;
Motorized m = (Motorized) something; // failure, because "something" now is a Rowboat
In the above code, the variable "something" is typecast into a reference to a Motorized object. The last typecasting doesn't succeed because a Rowboat is not Motorized.The idea of interfaces may seem a bit complicated and futile at first, and it's quite possible to write small Java programs without using them, but even then the design might be flawed. Bigger (and well designed small) programs tend to have lots of interfaces. Especially frameworks make heavy use of interfaces.
0 comments
Post a Comment