"Hello, Amigo! Today you're going to make some discoveries. The topic for today—drum roll, please—is interfaces."

"Yep. A day so wonderful that I'm heading home to take a bath."

"An interface is the child of Abstraction and Polymorphism. The interface is very much like an abstract class where all the methods are abstract. It is declared the same way as a class, but with the keyword interface. Here are some examples:"

Code Description and facts
interface Drawable
{
void draw();
}
interface HasValue
{
int getValue();
}
1) Instead of the word class, we write interface.

2) It contains only abstract methods (no need to add the word abstract).

3) In fact, all methods on interfaces are public.

interface Element extends Drawable, HasValue
{
int getX();
int getY();
}
An interface can only inherit other interfaces.

You can have many parent interfaces.

class abstract ChessItem implements Drawable, HasValue
{
private int x, y, value;

public int getValue()
{
return value;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

}
A class can inherit several interfaces (and only one class). To show this inheritance, we use the keyword implements.

The ChessItem  class was declared abstract: it implemented all inherited methods except draw.

In other words, ChessItem contains one abstract method: draw().

"Interesting. But why do we need interfaces? When are they used?"

"Interfaces have two strong advantages over classes:"

1) Separation of "method definitions" from method implementations.

I previously told you that if you want to allow other classes to call the methods of your class, then you need to mark them as public. If you want certain methods to be called only from your own class, then they must be marked private. In other words, we're dividing the class's methods into two categories: «for everybody» and «just for me».

We can use interfaces to strengthen this separation even more. We will make a special "class for everybody" that will inherit a second "just-for-me class". That would look about like this:

Before
class Student
{
 private String name;

 public Student(String name)
 {
  this.name = name;
 }

 public String getName()
 {
  return this.name;
 }

 private void setName(String name)
 {
  this.name = name;
 }
After
interface Student
{
 public String getName();
}

class StudentImpl implements Student
{
 private String name;
 public StudentImpl(String name)
 {
  this.name = name;
 }
 public String getName()
 {
  return this.name;
 }
 private void setName(String name)
 {
  this.name = name;
 }
}
Before
public static void main(String[] args)
{
 Student student =
               new Student("Alibaba");
 System.out.println(student.getName());
}
After
public static void main(String[] args)
{
 Student student =
               new StudentImpl("Ali");
 System.out.println(student.getName());
}

We split our class into two pieces: an interface and a class that implements the interface.

"So what's the advantage?"

"The same interface can be implemented by (inherit) different classes. And each class can have its own behavior. Just like ArrayList and LinkedList are two different implementations of the List interface."

Thus, we're hiding not only the different implementations, but also the classes that contain the implementations (we can just use interfaces everywhere in the code). This lets us very flexibly replace some objects with other objects while the program is running, changing an object’s behavior without the knowledge of any of the classes using it.

In combination with polymorphism, this is a very powerful technique. At the moment, it's far from obvious why we would need to do this. You must first encounter programs consisting of dozens or hundreds of classes to appreciate how interfaces can greatly simplify your life.

2) Multiple inheritance.

In Java, each class can have only one parent class. In other programming languages, classes can often have several parent classes. This is very convenient, but it also creates lots of problems.

Java offers a compromise:  you cannot inherit multiple classes, but you can implement multiple interfaces. An interface can have several parent interfaces. A class can implement multiple interfaces and inherit only one parent class.