Course Content
Introduction to Java
Interfaces are a way to define a contract in Java. A contract is a set of rules that all classes implementing the interface must follow. Usually, interfaces contain only method signatures (no bodies) and can't contain any instance variables. Think of interfaces as a blueprint for classes. Classes that implement an interface must provide an implementation for all of its methods. For example, you could have an interface called Drawable with a method draw that takes no arguments and returns nothing. Then, you could have a class Circle that implements the Drawable interface and provides its implementation for the draw method.
How to declare an interface?
To declare an interface, you use the interface keyword followed by the interface's name. Here's an example:
Main.java
How to implement an interface?
To implement an interface in a class, you use the implements keyword followed by the interface's name. Here's an example:
Main.java
Interfaces are useful when you want to define a set of methods a class must implement but don't want to provide any implementation. By using interfaces, you can ensure that all classes implementing the interface provide a common set of methods.
Real-world use of an interface
Here's an example to illustrate the use of interfaces in the real world:
Consider a scenario where you have to create a simulation of different types of musical instruments. You can define an interface Playit with methods like music, playNote
, stopPlaying
, and changeInstrument
. Then, you can create classes for instruments such as Violin, Cello, and Trumpet. Each of these classes can implement the Playit interface and provide its specific implementation for the methods.
In this example, you can see that the interface Playit provides a common interface for all types of musical instruments. Cello and Violin's classes implement the Playit interface and provide their specific implementation. This is an example of how interfaces can be used to define a set of methods a class must implement and provide a common interface for all classes.
Here is what that code will look like.
Main.java
There are a few things to know about interfaces. Those are:
- A class can implement multiple interfaces. Then that class should provide an implementation for all the methods of interfaces that it implements;
- In Java 8, interfaces were enhanced to allow default methods. This means you can provide a default implementation for a method in an interface. If a class implements the interface, it can choose to use the default implementation or provide its implementation for the method;
- Interfaces can also contain static methods. A static method in an interface is called the same way it is called in a class;
- A marker interface is an interface that has no methods. It is used to mark a class as having a certain property. For example, Serializable is a marker interface that marks a class as serializable, meaning it can be written to and read from a file.
Now you know how to use interfaces. Write more codes and practice better.
What is the purpose of a marker interface in Java?
Select the correct answer
Can a class implement multiple interfaces in Java?
Select the correct answer
Section 3.
Chapter 4