Beyond Noob
Beyond Noob

Reputation: 115

Separating the interface from an implementation in Java

How can you separate the interface from an implementation in Java?

In C/C++, this can be done by creating 2 files a .c and .h file with the same file name. How can you do this in Java?

Upvotes: 2

Views: 2157

Answers (5)

Paul Draper
Paul Draper

Reputation: 83253

The closest analogy to .h and .c separation is interfaces.

MyInterface.java

interface MyInterface {

    void doSomething();

}

MyImplementation.java

class MyImplementation implements MyInterface {

    public void doSomething() {
        System.out.println("Hello world");
    }

}

You then use the interface type everywhere except for the actual constructor

MyInterface instance = new MyImplementation();

Of course, there are several differences.

  • A single class may implement multiple interfaces, and multiple classes may a single interface
  • Member values are not included in an interface, only methods.
  • Only public methods appears in a Java interface.
  • No implementation is ever allowed in an interface (as opposed to C++ templates, which depend on .h implementation).

But this is how "programming to interfaces" is accomplished.


There is no consistent convention for naming interfaces vs concrete classes in Java. C# (nearly identical in its treatment of interfaces) has a convention which I have come to use where the interface begins with I, e.g. IList. The Java standard libraries tend to use the "pure" name for the interface, and then modify it for the concrete implementation, such as List (interface) and ArrayList (implementation).

Upvotes: 5

James
James

Reputation: 597

In Java, interfaces and classes (ie "implementation") are NOT distinguished at file level or by their names. They are both declarations. Specifically, a class declares which interface(s) it implements. For example:

public class MyClass implements Interface1, Interface2, .... {
    // implementations
}

Java does not have the same "linking" concept as in C/C++. You can call your class or interface in any legal form. The "link" between an interface and its implementation classes at runtime are done through the class loader.

Upvotes: 2

vinayknl
vinayknl

Reputation: 1252

To have an interface and implementation separated, suggest below steps..

( 1).Identify methods which be part of definition of your class. This becomes your template for new classes with similar functionality.

(2 ). Create a new interface which contains methods which were identified to be part of interface definition (may be many interfaces)

( 3.) Modify your class to implement the interface(s) you have created

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93569

You don't. You could do it via Java interfaces, but doing that for every class would be bad design. Interfaces should only be used when you expect multiple classes to implement them, or when used as a callback function description. Really the point of separating the code from the .h file in C++ is

1)To prevent bloating the binary as compilers would make multiple versions of each function for each file its included in

2)To make it easier to read for those who just want the implementation.

In java, 1 isn't an issue. And Java prefers to solve 2 via automated documentation tools (JavaDoc).

TLDR- you don't.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201447

By creating an interface, and implementing it also (generally) in two files -

Say.java the interface -

public interface Say {
  public void say(String msg);
}

and Hello.java, the concrete implementation (and in this case) also a main() method -

public class Hello implements Say {
  @Override
  public void say(String msg) {
    System.out.printf("%s, World!%n", msg);
  }
  public static void main(String[] args) {
    Say s = new Hello();
    s.say("Hello");
  }
}

Note: In Java you can have multiple classes with main() methods. It is also a good practice to use the @Override annotation.

Upvotes: 2

Related Questions