And what condition(s) would cause you to make one or another decision? (to return true or false)
posted 1 month ago
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.
posted 1 month ago
when I try and compile the following in the interface it says interface methods cannot have body. so how else can i write it?
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.
Being persecuted doesn't in any way prove your righteousness or your beliefs. Many people get persecuted because they are repugnant or annoying. Or just because they can be.
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps