Java Interface
Program to a capability.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Interface.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
An interface describes a contract that different classes can implement. The caller can depend on that contract rather than a concrete implementation. A class may implement more than one interface, which makes capabilities easier to combine.
Explore the details
Interfaces
Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class"
that is used to group related methods with empty bodies:
To access the interface methods, the interface must be "implemented"
(kinda like inherited) by another class with the implements
keyword (instead of extends). The body of the
interface method is provided by the "implement" class:
Notes on Interfaces:
Like abstract classes, interfaces cannot be used to create objects (in the example above,
it is not possible to create an "Animal" object in the MyMainClass)
Interface methods do not have a body – the
body is provided by the "implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and
public
Interface attributes are by default public,
static and final
An interface cannot contain a constructor (as it cannot be used to create objects)
Why And When To Use Interfaces?
1) To achieve security – hide certain details and only show the important
details of an object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved
with interfaces, because the class can implement multiple interfaces.
Note: To implement multiple interfaces, separate them with a comma (see example below).
Multiple Interfaces
To implement multiple interfaces, separate them with a comma:
interface Drawable {
void draw(); // abstract by default
default String type() { return "2D"; } // default method
}
interface Resizable {
void resize(int factor);
}
class Square implements Drawable, Resizable {
int side;
Square(int s) { this.side = s; }
public void draw() { System.out.println("Square " + side + "x" + side); }
public void resize(int f) { side *= f; }
}
Square sq = new Square(4);
sq.draw(); // Square 4x4
sq.resize(3);
sq.draw(); // Square 12x12
System.out.println(sq.type()); // 2DA complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
Printable report = new Report();
report.print();
}
}
interface Printable { void print(); }
class Report implements Printable { public void print() { System.out.println("Report is ready"); } }Predict first. Then reveal the expected output / வெளியீடு
Report is ready
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Create a Receipt class that implements Printable and prints a different message.
Reveal the answer and reasoning / விடை
The caller can use Printable receipt = new Receipt(); and invoke the same print method.
One mistake to avoid: A class implementing this interface must provide a public print method; it cannot reduce the access of the interface method.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.