Interfaces in Ruby & Java

Katerina
3 min readNov 29, 2017

--

When I first started programming in Java after having programmed for a year in Ruby, I found many aspects of Java confusing. There was one particular aspect of Java which took me a while to understand; the Java Interface.

Don’t worry, I know what you’re thinking. What is an Interface?

An Interface is a contract. On the contract you detail the behaviour of that interface in the form of different methods the interface has on it. Then any classes which should share the behaviour from that interface need to implement that interface. In essence when the class implements the interface, it “signs” the interface contract which confirms the class will provide all the methods from the interface.

Let’s create an example, we have an interface called Car. On that interface are lots of methods which is essential behaviour that every car will need to know like starting the engine, turning on the lights etc.

public interface Car {
public String startTheEngine();
}

This is important behaviour that we need a car to know but in the real world there isn’t just one type of car. We have numerous different brands Toyota, Nissan, BMW etc. In our program, each of these brands would be represented by a different class and though the brands are different they’ll still need the essential behaviour from our Car interface. Therefore, all of the classes will implement Car.

After each car brand class has these basic methods, it can have its own methods which are not on the interface and which make that car unique. Perhaps BMW has a special method that makes it go at the speed of light or Nissan has a method that plays Abba’s greatest hits as the driver is accelerating. Either way, we don’t care. What we do care about is that classes implementing the car interface, honour that contract.

public class BMW implements Car {  public String startTheEngine() {
return pushRedButton();
}
public class Nissan implements Car {  public String startTheEngine() {
return pushBlueButton();
}

So, interfaces in Java work like this and once you understand why they are so handy, interfaces seem pretty cool.

Is there a Ruby equivalent of the Java interface? Strictly speaking, not really.

In Ruby, instead of a literal interface object the interface is invisible but you have a class that provides the mechanism for polymorphism. From this class you call the classes that share those methods.

You might have a Game class with the method get_next_move that gets a move from a HumanPlayer and ComputerPlayer. For this to work, the player_type passed in to the get_next_move method is used to ensure the right class is called for each player’s respective turn.

def get_next_move(player_type) 
player_move = player_type.make_move
mark_move_on_board(player_move)
end

How the move is actually played is dependent on player type. Although each player object has the same make_move method, the code inside each player’s method may be different. The only requirement (that the invisible interface oversees) is that the value returned from each method is a string as this is the data type required by the mark_move_on_board(player_move) method.

In Ruby, to create the same behaviour as an interface in Java, two elements are at play; the idea of an invisible “interface” where each Human and Computer class has the get_next_move method and that it returns a string and polymorphism.

** Trade offs **

  • I know the program well so I know the data types of the arguments needed but someone reading my Ruby code might not. This is one of the huge benefits of Java, its data types are explicitly stated so its easy to see what a method will return. Likewise, the interface is like a category that the classes fit into which makes it easier to see the overall intent of the classes and how they should behave.
  • Ruby is very kind. It does a lot of work behind the scenes but maybe it helps to walk through the paces and do the work that Java insists on, perhaps this makes your code clearer.

--

--