An interface is more than an interface. It's a behavior.

"Hello, Amigo! It's me—again. I want to give you another point of view on interfaces. You see, most of the time a class is a model of a particular object. By contrast, interfaces are more like an object's abilities or roles, rather than the object itself."

An interface is more than an interface. It's a behavior. - 1

For example, things such as cars, bicycles, motorcycles, and wheels are best represented as classes and objects. But their abilities, like «I can move», «I can carry people», and «I can park», are better represented as interfaces. Check out this example:

Java code Description
interface Moveable
{
void move(String newAddress);
}
Corresponds to the ability to move.
interface Driveable
{
void drive(Driver driver);
}
— Corresponds to the ability to move.
interface Transport
{
void addStaff(Object staff);
Object removeStaff();
}
— Corresponds to the ability to carry cargo.
class Wheel implements Moveable
{
...
}
— A «wheel» class. Has the ability to move.
class Car implements Moveable, Drivable, Transport
{
...
}
— A «car» class. Has the ability to move, be driven by a person, and carry cargo.
class Skateboard implements Moveable, Driveable
{
...
}
— A «skateboard» class. Has the ability to move and be controlled by a person.
3
Task
Java Core,  level 2lesson 8
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and assimilate some material. What's more, you combat laziness.

Interfaces greatly simplify the life of the programmer. Programs very often have thousands of objects, hundreds of classes, and just a couple dozen interfaces (roles). There are few roles, but they can be combined in many ways (classes).

The whole point is that you don't have to write code defining interaction with every other class. All you have to do is interact with roles (interfaces).

Imagine that you're a robotic builder. You have dozens of subordinate robots and each of them can have multiple skills. Suppose you need to urgently finish building a wall. You just take all the robots that have the ability to "build" and tell them to build the wall. You don't really care which robots do it. Let it be a robotic watering can. If it knows how to build, let it build.

Here's how it would look in code:

Java code Description
static interface WallBuilder
{
void buildWall();
}
— Ability to «build a wall». Understands the command «build a wall» (has the appropriate method).
static class WorkerRobot implements WallBuilder
{
void buildWall()
 {}
}
static class GuardRobot implements WallBuilder
{
void buildWall()
 {}
}
static class WateringCan
{}
— Robots that have this ability/skill.

— A watering can can't build a wall (it doesn't implement the WallBuilder interface).

public static void main(String[] args)
{
 //add all robots to a list
 ArrayList robots = new ArrayList();
 robots.add(new WorkerRobot());
 robots.add(new GuardRobot());
 robots.add(new WateringCan());

 //build a wall if you can
 for (Object robot: robots)
 {
  if (robot instanceof WallBuilder)
  {
   WallBuilder builder = (WallBuilder) robot;
   builder.buildWall();
   }
  }
 }
}
— How do we give the command to build a wall?

"That's amazingly interesting. I never dreamed that interfaces could be such an interesting topic."

"And then some! Together with polymorphism, it's totally mind-blowing."

Comments(12)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Renat MukhametshinLevel 13 , Pervouralsk
30 August 2019, 12:16
hmm, it's not ease as seems for the first glance
Philip T-DLevel 18 , San Diego
24 April 2019, 22:37
In the last code example below the comment //build a wall if you can: Why can you not simply call robot.buildWall() on each object directly in the loop? Won't line 12 guarantee that the robot in question has the .buildWall() method? What's with the type coercion going on there (lines 14-15) ?
RoyLevel 22 , Bangkok
25 April 2019, 22:02
Good question.
for (Object robot: robots)
{
 if (robot instanceof WallBuilder)  //this already should avoid the robots that can't build the wall
 {
  robot.buildWall();          // instead of: WallBuilder builder = (WallBuilder) robot;
                                       //builder.buildWall();
  }
 }
Would be nice if someone could explain why it can't be like that
Ops guyLevel 17 , Round Rock, TX
7 May 2019, 22:44
If I understand this correctly robot's reference variable is type Object and Object doesn't know about buildWall method. So you need to cast Object type to WallBuilder type first in order to use that method. Object type and reference type is not the same. I hope I got this right :)
JulienLevel 15 , Paris
22 May 2019, 17:22
that's what I would have answered. There is one line solution:
if (robot instanceof WallBuilder) {
    ((WallBuilder) robot).buildWall();
}
Ed MaphisLevel 20 , Painesville
31 May 2019, 16:34
The problem is that 'WateringCan' doen't implement 'WallBuilder'.
Muhammad VahhaajLevel 19 , Rawalpindi
5 July 2019, 04:25
One thing that is clear is that, it is a widening reference conversion. So the reference type(Object class) used in foreach loop does not have the method buildWall() defined so it won't be able to run that method. Now what we are doing is that, casting the the object to its child class which hs the method defined and storing it in that reference type variable to explicitly tell that which method to run.
Darko JakimovskiLevel 18 , Kriva Palanka
24 April 2019, 14:14
Can anyone explain why would an interface be static? Couldn't find anything on the subject
Oleg TokarenkoLevel 19 , L'viv
15 May 2019, 18:51
I assume, that's because an interface is a general behaviour that classes implement. Therefore, if the field is static it means that all instantiated objects can use the same general field. You can apply this analogy to interfaces: all of the classes can implement the same general interface. That's how I understand it.
Darko JakimovskiLevel 18 , Kriva Palanka
15 May 2019, 19:25
I still haven't wrapped my head around the concept, but I'm sure I'll have the Eureka moment as with everything Java related. Thanks for the effort tho Oleg, cheers
IasuLevel 17
1 June 2019, 21:50
Same here! I got "modifier static is redundant for inner interface" in IntelliJ
Vidhya Level 13 , Sunnyvale
17 December 2019, 23:36
when a Interface has a inner interface embedded (Nested interfaces)ideally they define it as static (same is the case with class identifier).please find a detailed explanation https://stackoverflow.com/questions/8374646/what-is-a-static-interface-in-java