Hackerrank Day 19: Interfaces

Interface
Abstraction is the separation between what something does and how it’s accomplished. An interface is a collection of abstract methods and constants that form a common set of base rules/specifications for those classes that implement it. Much like an abstract class, an interface cannot be instantiated and must be implemented by a class.

By having classes implement the interface (as opposed to having them be completely separate standalone classes), we ensure that all classes (and any future classes that implement that interface) will follow the same basic rules. Knowing the rules for the interface allows us to independently write code that uses some features of the interface, regardless of how it’s implemented.

**If we add another method to the interface, we must add an implementation for that method to each class that implements the interface, in order for the code to compile.

screen-shot-2016-12-26-at-10-00-08-pm

My Solution in Java 8: 

screen-shot-2016-12-26-at-9-59-24-pm

(Ruby does not formally support interfaces like Java or C#, so this challenge was completed in Java 8.   However, interface is a relevant concept to Ruby, which can be read more about here.)

Also, as a side note:   When writing multiple classes in the same file, do not use an access modifier (e.g.: public) in your class declaration (or your code will not compile).  However, you must use the public access modifier before your method declaration for it to be accessible by the other classes in the file.

Advertisements
Hackerrank Day 19: Interfaces

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s