Interfaces

Available

"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.

Comments (38)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Thomas
Level 13 , Scottsdale, United States
12 August, 20:32
See @Berkson below
Thành Black
Level 49 , Hanoi
22 September 2021, 02:05
Can a Interface maybe inhert class ?
Korlat
Level 25 , Gasteiz, Basque Country, Spain
5 May 2022, 10:19
"An interface can only inherit other interfaces." "A class can inherit several interfaces (and only one class). "
P.B.Kalyan Krishna
Level 22 , Guntur, India
16 July 2023, 14:53
A class can implement several interfaces but can inherit only one class.
TheLordJackMC a person at war with my demons
15 July 2021, 14:48
why is he taking a bath o_o
Sansho
Level 19 , Bordeaux, France
7 May 2021, 13:51
So at this moment, I think interfaces are empty forms, that you have to fulfill. Imagine you have to enlist somewhere. You could write your informations on a white paper and give it to the etablishment (for, I dunno, maybe travel to space). If you have some lucks, maybe all the data you put are rights, but there's a big chance that somes datas are missing or wrong written. That's why the Space Travel Company gives you a form. With interfaces, it looks like the same.... At least that's how I see it actually :)
Gellert Varga
Level 23 , Szekesfehervar, Hungary
8 January 2021, 20:40
I think this is wrong:
class abstract ChessItem implements Drawable, HasValue
and it would be correct this way:
class abstract ChessItem implements Element
Opinions?
Gellert Varga
Level 23 , Szekesfehervar, Hungary
2 February 2021, 00:17
Element interface extends (it inherits) Drawable and HasValue. I think class ChessItem can implement the Element interface, and i think in this case the ChessItem abstract class will automatically inherit the Drawable and HasValue interfaces, too. The lesson says: "The ChessItem class was declared abstract: it implemented all inherited methods except draw." And in the referred code in the lesson you can see the implementations of the alleged inherited public int getX() and the alleged inherited public int getY() methods. But how did ChessItem inherit these methods? These methods are within the Element, and the declaration of ChessItem class doesn't talk about "it implements Element".
Gaelle Gilles
Level 15 , New York City, United States
2 February 2021, 04:07
Oh, you're right, I read it wrong. Sorry.
Jonaskinny Java Developer at Sandmedia
25 February 2022, 01:06
If you implement an interface and that interface extends other interfaces you must implement all those interface methods in your class. cleaner than CG
Gellert Varga
Level 23 , Szekesfehervar, Hungary
26 February 2022, 01:02
Yes, it's a clear example:)
Chang You
Level 47 , Santa Rosa, United States
24 December 2020, 17:37
"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."
Michael Amann Full Stack Developer
23 May 2022, 17:28
You've obviously never worked on a very large real world program.
Artur
Level 22 , Moorends, United Kingdom
3 November 2020, 04:59
I know how you feel it just don't make much sense yet but hey.. hopefully will clear it out after few exercises :D
Edddieg
Level 15 , Providence, United States
1 August 2020, 18:57
I don't understand anything I just read :(
Marek Pasierbek Working at Nexus Polska
4 August 2020, 07:42
why? It is s easy and clever. You need practice and some examples
Edddieg
Level 15 , Providence, United States
4 August 2020, 20:33
I know-how to implement an interface, but everything I read just wasn't clear enough
Jaques Strydom PHP/Python Developer
1 August 2020, 01:56
Am I imagining it or is Amigo becoming really sarcastic in the last few lessons?
Sela
Level 20 , Poland
14 July 2020, 05:32
since Java 1.8 methods in interfaces can have implementations if marked with default keyword