Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How do I write a method isAbleToFly(), which takes no argument and returns true or false,

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the above is what I have so far. Stuck on the rest. Many thanks

would the rest just be:

return true;
{
else
}
return false;
 
 
Sheriff
Posts: 7421
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

would the rest just be:

return true;
{
else
}
return false;


And what condition(s) would cause you to make one or another decision? (to return true or false)
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

this is what I have attempted for the isAbleToFly
 
Liutauras Vilda
Sheriff
Posts: 7421
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

example 1:

example 2:

example 3:
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I try and compile the following in the interface it says interface methods cannot have body. so how else can i write it?

public boolean isAbleToFly() {
return true;
}
 
Liutauras Vilda
Sheriff
Posts: 7421
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lilly Potter wrote:. . .
return true;
{
else
}
return false;

Have a look at the old Sun Style Guide, which suggests you shouldn't be using if‑else to return a boolean. Also you had the {} the wrong way round in that post.
 
lowercase baba
Posts: 12792
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Saloon Keeper
Posts: 21603
147
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
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
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!