| Author |
Interface
|
John Paterson
Ranch Hand
Joined: Mar 12, 2012
Posts: 81
|
|
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
Joined: Apr 06, 2010
Posts: 3785
|
|
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
Ranch Hand
Joined: Mar 17, 2012
Posts: 220
|
|
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
Joined: Nov 01, 2011
Posts: 451
|
|
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
Joined: Mar 12, 2012
Posts: 81
|
|
|
Thanks guys
|
 |
 |
|
|
subject: Interface
|
|
|