I am writing an interface called Launchable. The Launchable interface specifies three methods: launch(), which takes no argument and returns no value, isAbleToFly(), which takes no argument and returns true or false, and land() which takes no argument and returns no value.
Alright, since you write/have an interface which has few methods defined in it, one of which is isAbleToFly(), the implementing class should return true or false based on some of its properties or simply knowledge of the thing it represents.
One note, Launchable interface name is poor for what methods it contains. Look down below how that reads (example 1). Other examples demonstrate possible implementations.
Think of it that you don't implement methods inside interfaces. Interfaces provide, well, an interface, while the actual implementation reside in the implementing classes, similarly as I showed you in the examples. Did you analyse them carefully?
Think of the interface as a set of rules you are defining for membership. "Anyone who wants to be in my Launchable club must be able to do these things. I don't care HOW you do them, but you must at minimum be able to return true or false when someone asks you if you are able to fly".
So now, you go to write an Airplane class. your Airplane class wants to be in the club, so you say it implements Launchable. In the Airplane class you write the method:
Now java comes along and says "ok...so you claim you are in the club..do you really have a method called isAbleToFly that takes no parameters and returns a boolean? yes? GOOD!!". life is wonderful
now you write an Automobile class. It does not want to be in the club. so it doesn't say it implements your interface Life is still good. You could write the isAbleToFly method in this class, but you don't HAVE to.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Think of the interface as a set of rules you are defining for membership.
That's an amusing analogy!
An Interface is a contract. If a class is defined with "implements XYZ", then each and every method defined in interface XYZ must be implemented in that class or one of its superclasses. You might be able to persuade your good friend Joe to ease membership rules for you, but the Interface contract allows no exceptions. Leave even one Interface method undefined or mis-defined and the compiler will refuse to compile the class.
Blitzlügen - Lies or information broadcast, but when called out the broadcaster does little or nothing is done to correct them, thus allowing those who wish to believe to accept them as truth.
Lügensturm - A barrage of Blitzlügen fired in such quick succession that it is essentially impossible to correct them all.
Just the other day, I was thinking ... about this tiny ad: