-1

Possible Duplicate:
C++ promotes a separation between class definitions and class implementations but not JAVA

I want to know why does JAVA does not do like in C++ and promotes the separation between class definitions and class implementations. Are there any advantages with that way of working or proceding for a mather of code reuse of extensibility?

Moved to https://softwareengineering.stackexchange.com/questions/118574/c-promotes-a-separation-between-class-definitions-and-class-implementations-bu

Community
  • 1
  • 1
Etienne Noël
  • 5,988
  • 6
  • 48
  • 75
  • 1
    Well, *are there*? It's just a matter of opinion - perhaps your question should be rephrased along the lines of "can anyone point me to a James Gosling quote stating why he didn't do this for Java?". At the moment it might be considered flamebait... – Andrzej Doyle Nov 08 '11 at 17:16
  • 2
    Well, you could use interfaces and classes. Anything that doesn't go into an interface would be an implementation detail anyways. – Thomas Nov 08 '11 at 17:17
  • 1
    This is identical with the previous question http://stackoverflow.com/questions/8054058/c-promotes-a-separation-between-class-definitions-and-class-implementations-bu, which was closed because it should have been moved to programmer's exchange. – CPerkins Nov 08 '11 at 17:41

4 Answers4

7

Java does offer the ability to separate type definitions from their implementations. Use an interface to define the type and a class implementing that interface for the implementation.

jmg
  • 7,308
  • 1
  • 18
  • 22
5

Java supports separate definition of interfaces and classes.

ewan.chalmers
  • 16,145
  • 43
  • 60
3

You can have the same kind of separation between definition and implementation in Java. We do it all the time in our development work through Interface Driven Design. We design the entire system as a set of interfaces that represent behavior and functionality in the system. Once the interfaces are defined, we create implementing classes that realize the behavior defined by the interfaces.

This gives us the ability to refer to our components by there interfaces and the flexibility to replace particular implementations without having to modify the references throughout the code base.

The one caveat about such a design method is the need to do dependency injection to maintain the separation between types. The dependency injection can be done either manually, or, as most people do it, using a DI framework such as the one included in Spring.

John Haager
  • 2,107
  • 1
  • 17
  • 22
3

Java's creators encourage coding by interface (known as design by contract). An interface is somehow similar to an abstract class in C++, with the following enforcement : all declared methods are abstract and public, no class variables are allowed, constants must are public.

bric3
  • 40,072
  • 9
  • 91
  • 111