aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

John Paterson
Ranch Hand

Joined: Mar 12, 2012
Posts: 116
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: 4240
    
    7

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

Joined: Mar 17, 2012
Posts: 761
    
    9
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: 116
Thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface
 
Similar Threads
Mikalai Zaikin SCBCD5.0 example problem
Multiple inheritence with interface
packages
Inheritance / Komposition of multiple abstract classes
What is the real use of interface in java apart from Multiple inheritance,