An interface defines what a class must do without saying how. It lets Java achieve multiple inheritance safely.
Defining Interfaces
An interface is a reference type that contains abstract methods (method signatures without bodies) and constants. It is declared with the keyword interface: interface Bank { double rateOfInterest(); }. All interface methods are implicitly public and abstract, and all fields are public, static and final. Since Java 8, interfaces may also contain default methods (with a body) and static methods. An interface cannot be instantiated.
Definition: Interface. A reference type that declares abstract methods and constants which implementing classes must provide, used in Java to achieve abstraction and multiple inheritance.
Implementing Interfaces
A class implements an interface and must give bodies for all its abstract methods, or else be declared abstract: class SBI implements Bank { public double rateOfInterest() { return 7.0; } }. A class can extend one class and implement several interfaces at the same time: class C extends A implements I1, I2. An interface can extend other interfaces.
Multiple Inheritance through Interfaces
Because a class may implement any number of interfaces, it can inherit the types and contracts of many parents. This gives the benefits of multiple inheritance without its ambiguity, since interfaces carry no conflicting state. For example, a class SmartPhone can implement Camera, Phone and MusicPlayer.
Polymorphism with Interfaces
A variable of an interface type can refer to an object of any class that implements it: Bank b = new SBI(); and later b = new PNB();. Calling b.rateOfInterest() runs the version of the actual object. This is run-time polymorphism, and it lets code be written to the interface rather than to a particular class, so new implementations can be added without changing the code that uses them. Java's own libraries rely heavily on this, as with the List and Comparable interfaces.
Abstract Classes and Interfaces
An abstract class may have fields, constructors and both abstract and concrete methods, and a class can extend only one. An interface has no instance fields or constructors, and a class can implement many. Use an abstract class for closely related classes sharing code, and an interface for a capability that unrelated classes share.
Table 3.6: Abstract class and interface
| Feature | Abstract class | Interface |
|---|---|---|
| Methods | Abstract and concrete | Abstract (plus default, static) |
| Fields | Any | public static final only |
| Constructors | Yes | No |
| Inheritance | extends one | implements many |
Example 3.6: Bank interface
Problem: Write an interface Bank with rate_of_interest(), and classes SBI and PNB that implement it with rates 6.8 and 7.2 percent, each with a branch field and a constructor. Use a Bank reference to call both.
Solution:
- interface Bank declares double rateOfInterest().
- SBI and PNB implement it, with constructors setting the branch name.
- A Bank reference is pointed first at an SBI object and then at a PNB object.
Result: The same call prints the rate of each bank: run-time polymorphism.
public class BankDemo {
public static void main(String[] args) {
Bank[] banks = { new SBI("Gurugram"), new PNB("Delhi") };
for (Bank b : banks)
System.out.println(b.info() + ": " + b.rateOfInterest() +
"%");
}
}
interface Bank {
double rateOfInterest();
String info();
}
class SBI implements Bank {
private String branch;
SBI(String branch) { this.branch = branch; }
public double rateOfInterest() { return 6.8; }
public String info() { return "SBI " + branch; }
}
class PNB implements Bank {
private String branch;
PNB(String branch) { this.branch = branch; }
public double rateOfInterest() { return 7.2; }
public String info() { return "PNB " + branch; }
}
Output
SBI Gurugram: 6.8%
PNB Delhi: 7.2%
Common mistakes
- Forgetting to make implementing methods public, which is a compile error.
- Trying to create an object of an interface with new.
- Confusing extends (for classes) with implements (for interfaces).
Key takeaways
- An interface declares methods that implementing classes must define.
- A class can implement many interfaces: multiple inheritance of type.
- An interface reference can point to any implementing object.
- Use abstract classes for shared code and interfaces for shared capabilities.
Practice questions
- What is an interface?
- How does Java achieve multiple inheritance?
- Are interface fields variables?
Answers to the practice questions
- A reference type that declares abstract methods and constants for classes to implement.
- A class can implement several interfaces.
- No; they are implicitly public, static and final constants.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit III
