4

I'm designing a file handler interface:

public interface FileHandler
{
    public void openFileHandler(String fileName);
    public void closeFileHandler();

    public String readLine();
    public String [] parseLine(String line);
    public String [] checkLine(String line[]);

    public void incrementLineCount();
    public void incrementLineSuccessCount();
    public void incrementLineErrorCount();

    public int getLineCount();
    public int getLineSuccessCount();
    public int getLineErrorCount();     
}

It is soon apparent to me that these methods can't be made private. I don't want incrementLineCount to be public.

What is proper way to design an interface like this?

5
  • 1
    why can't these methods be private?
    – Scott
    Sep 27, 2012 at 18:06
  • 2
    Then don't add them to interface.. Just have them as private method in your class.. And I think you mean - "can be made private"
    – Rohit Jain
    Sep 27, 2012 at 18:06
  • When I do something like this: private void openFileHandler(String fileName); . I get an error: Illegal modifier for the interface method openFileHandler; only public & abstract are permitted
    – lewicki
    Sep 27, 2012 at 18:09
  • 1
    You don't have private methods in interface. In fact all the methods in an interface are by default: - public abstract.. I said that have them as private method in your implementing class..
    – Rohit Jain
    Sep 27, 2012 at 18:35
  • What is the expected behaviour of those methods?
    – ysdx
    Sep 28, 2012 at 7:00

2 Answers 2

14

An interface specifies behavior, not specific implementation details.

You should specify the operations you want anything that implements your interface to support. One way of determining that is asking "How will consumers use my interface; what is the contract I am specifying?"

Leave the details (e.g. the private methods) up to the implementations. That's the point of interfaces -- you can have different implementations that behave in different ways, e.g. the difference between reading a file off disk and reading a file off the cloud....

2

Interfaces should be designed by keeping in mind what outside objects will need to call on classes that implement the specific interface.

This is just what the class should be able to do from the outside. For everything else you can use abstract classes with protected methods (so you will have the same commodity to force implementation in subclasses but without being forced to let them public).

Something like:

interface FileHandler {
  public void openFileHandler(String fileName);
  public void closeFileHandler();
}

abstract class AbstractFileHandler implements FileHandler {
  ...
  protected abstract void incrementLineCount();
  protected abstract void incrementLineSuccessCount();
  protected abstract void incrementLineErrorCount();
}

class ConcreteFileHandler extends AbstractFileHandler {
  ...
}

If you don't need to have a common ancestor just declare the private methods in your concrete class and you are done.

2
  • private abstract methods?
    – js3v
    Sep 27, 2012 at 18:14
  • Sorry, I actually meant protected.
    – Jack
    Sep 27, 2012 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.