This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
John Paterson
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I was writing out some code to test my understanding of interfaces when I ran into some trouble. Following are the the 3 files:








I am getting three error messages in the 'Calculator' file for the 3 methods divide, subtract and add. The error message is
missing method body, or declare abstract public void 'methodName'();
I don't understand why I am getting this. My underdstaning is that I can choose not to implement the methods in the first concrete implementing class as long as it's subclass implements them. Hope someone can help me here. Thanks.

regards
John
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Paterson wrote:My underdstaning is that I can choose not to implement the methods in the first concrete implementing class as long as it's subclass implements them.

No, if it's a concrete class then it can be instantiated. And that means somebody could create one, and then call the method. So the method body has to exist.

You can choose to not implement all the methods and force a subclass to do that. But in this case you have to declare the class as abstract so that it can't be instantiated.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Paterson wrote:... My underdstaning is that I can choose not to implement the methods in the first concrete implementing class as long as it's subclass implements them..

That's the incorrect part. The first concrete class must implement all of then unless if it's (abstract) super class has implemented them.
 
Helen Ma
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Paterson wrote:Hi folks,

I was writing out some code to test my understanding of interfaces when I ran into some trouble. Following are the the 3 files:








I am getting three error messages in the 'Calculator' file for the 3 methods divide, subtract and add. The error message is
missing method body, or declare abstract public void 'methodName'();
I don't understand why I am getting this. My underdstaning is that I can choose not to implement the methods in the first concrete implementing class as long as it's subclass implements them. Hope someone can help me here. Thanks.

regards
John


In the Calculator class, provide some implementations for the divide, subtract and add methods.

That reminds me of a rule : if you declare one abstract method in a class, the class should be declare abstract. So, if you have an abstract method in Calculator, declare this class as abstract.
 
John Paterson
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic