Take the 2-minute tour ×

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?

share|improve this question

migrated from stackoverflow.com Sep 28 '12 at 2:13

This question came from our site for professional and enthusiast programmers.

why can't these methods be private? –  Scott Sep 27 '12 at 18:06
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 '12 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 '12 at 18:09
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 '12 at 18:35
What is the expected behaviour of those methods? –  ysdx Sep 28 '12 at 7:00

2 Answers 2

up vote 11 down vote accepted

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....

share|improve this answer

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.

share|improve this answer
private abstract methods? –  js3v Sep 27 '12 at 18:14
Sorry, I actually meant protected. –  Jack Sep 27 '12 at 18:19

Your Answer


