wood burning stoves
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design suggestion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design suggestion." Watch "Design suggestion." New topic

Design suggestion.

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 761

I have written Class

which has got one verfy method which is the main method of class. and verify method needs some helper method so i have got 8 helper methods in the same class.

Now i have declared verify method with public modifier and and other helper method should be private.

now for my class i have written one interface which has the defination of the my class.

now my interface can not declare the method as private.

so how should i arrange the stuffs.

Jigar Naik

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Hi Jigar,

this if I understand you right you're trying to declare all the methods of your concrete class in the interface?!?

Well, as you said method declarations inside an interface are implicitly public but that's OK. You should only declare the methods which belong to the public API of a class, i.e. the methods which should be called by client code! So just leave out the private methods in the interface definition. This has nothing to do with the public interface of you class. Without the interface you concrete class at the moment offers only one public method "verify" to clients. A client of your code won't (and can't) care of the private methods as long as the one and only public method does its job. You only created the private helper methods to keep your code clean instead of putting everything into one big method. That's definitely a good decision but it is not relevant for the public interface!

You can look at this from another point of view. If it WOULD be possible to declare private methods in an interface then you would tell an implementing class HOW it should do its work but the reasoning behind an interface is not to tell HOW something should be implemented but just WHAT functionality has to be implemented by a concrete class. Therefore it's easy to have multiple concrete classes which implement the same interface without caring about how they do their work internally as long as they fulfill the contract of the public API (= interface).

steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
If you wish to inherit your helper functions and reuse their implementation details in derivative classes then you can make the package or protected level access and have this class implement the public interface. Other classes would extend this base class and so have the same interface plus access to some of the implementation details.

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 761

Thanks a lot... Marco & Steve..

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 761
Hi again..

I have written on helper interface...

and implementing subclass which i have declared as abstract. so that only class extending helperImpl can access the helper methods.

but what if i have 2 class for example ClassA and ClassB which extends HelperImpl

and class D which create

10 instances of ClassA and ClassB

what will be the effect on HelperImpl

Will it create 10 (ClassA)+ 10(ClassB) instances of HelperImpl (Abstract Class). ???
I agree. Here's the link: http://aspose.com/file-tools
subject: Design suggestion.
It's not a secret anymore!