34

I have an interface that I want to implement in separate classes after doing a quick google search apparently, Java doesn't have partial classes. Is there a way that I can do this or am I stuck throwing all of my code into one class?

Basically, I am trying to write a service. Some of the service methods really belong in their own class and seem kind of illogical in the same class. Here is an example of what I am trying to do.

package com.upmc.esdm.messaging.epcd.service;
import java.util.List;

import javax.ejb.Remote;

import com.upmc.esdm.messaging.epcd13jpa.entities.EmailDomainTrust;


@Remote
public interface MessagingInterfaceRemote {
public List<EmailDomainTrust> getEmailDomains();

    public int upDateQualityTable();

    public string isLogial();

    public byte[] ToWritePartialClassesInJava();
}

I would normally have partial classes in C# and I would put similar methods that return similar values into one partial class (or maybe classes that update a record in one class). How would I implement this? Or should I just put all the method implementations into one class?

Thanks.

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195
  • 10
    "Some of the service methods really belong in their own class and seem kind of illogical in the same class." Then why are you trying to put them in one class? Why not just put them in separate classes? see [Single responsibility principle](http://en.wikipedia.org/wiki/Single_responsibility_principle) – Jesper Mar 26 '12 at 16:56
  • Java does not have built-in functionality that will make this easy for you. There are a few third party libraries that can help you implement this. My suggestion is to try to solve your problem without such a preprocessor. For simple things, introducing a preprocessor may cost you far more than it saves you. – Mike Clark Mar 26 '12 at 17:02
  • 1
    @Jesper: He's actually trying to implement the single responsibility principle, but is not using composition to achieve it. – Eric J. Mar 26 '12 at 17:07

5 Answers5

43

There is nothing like partial classes in Java. You can achieve many of the same benefits using aggregation, delegation, and abstract base classes.

(I have caved to peer pressure and eliminated the “thankfully” remark that has generated so much heat in the comments. Evidently that little aside seems to have earned me four downvotes, despite being irrelevant to the answer.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 6
    @EricJ. - I understand that partial classes have their uses. However, they can easily turn into a nightmare. Microsoft itself [suggests](http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx) that they are useful for multiple programmers to work separately on a class implementation. Imagine how much fun it would be when a partial class you're working on all of a sudden (unbeknownst to you) inherits from some new base class because some other programmer decided her partial class would benefit from it. There are other such horrors. I repeat: thankfully, Java is free of this. – Ted Hopp Mar 26 '12 at 19:20
  • 11
    Partial classes are very usefull in forms when you have to separate UI management code and everything else. But I admit, I tried to use them in other circumstances, and sometime I got lost. If you think you need to split a class (not a form) maybe you need to reingegnerize your approach... – Seraphim's Jan 07 '13 at 11:24
  • 17
    `Imagine how much fun it would be when a partial class you're working on all of a sudden (unbeknownst to you) inherits from some new base class because some other programmer decided her partial class would benefit from it` - it's also bad if the class in question was not a partial class. I don't think the class being partial is very important here at all. – Konrad Morawski Dec 28 '13 at 15:36
  • @KonradMorawski - But the danger is greatly enhanced. From [the docs](http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx): _"If any of the parts declare a base type, then the entire type inherits that class."_ Usually if you inherit from some class, you have every reason to expect to be informed of changes to the base class (at a minimum, there's usually a new version release). With partial classes, you might get no warning at all because the change can come from a peer. – Ted Hopp Dec 28 '13 at 23:07
  • 26
    Partial classes are highly useful if one of the "programmers" is not human, i.e. code generation. That is the only use case I can see, but a *very* strong one, that separates structure at the source level from structure at the API level. – Krumelur Jul 19 '15 at 12:04
  • @Krumelur - I completely agree that partial classes are useful with automatic code generation. Unfortunately, Microsoft also sees another use case—independent of code generation: _"When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously."_ That's a use case that I see as likely to cause more problems than it solves. Avoiding those problems requires an extra layer of discipline that is not enforced by the language and not defined in any documents that I've seen. – Ted Hopp Jul 19 '15 at 14:43
  • Another useful case for partial classes is if a class implements more than one interface. You can move the separate implementations to partial classes instead of using regions: Foo.cs (inheritance, ctor, private method, field) Foo.IFoo.cs (public methods for iFoo) Foo.IBar.cs (public method for IBar) This also helps when you have lots of people on the project and need to delegate the implementation of different aspects to different team members. – CNad Apr 16 '21 at 18:30
3

Aspectj can be the answer to C#/.net partial class feature! Spring Roo is one of the typical development framework using aspectj to divide a class functionalities into several different files.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
2

Before going that route, you might want to consider the Builder design pattern. That allows your service to aggregate implementations that are implemented in separate classes. No need for a pre-compiler, and very clean from an OO perspective.

Each component class has a specific, narrow responsibility, with the service class enlisting the services of it's component classes to implement just the responsibility of the service.

If you really want to (not recommended), you can use a pre-processor to assemble parts of a class prior to compilation.

The only scenario where a pre-processor might make sense is if some of your implementation is code-generated and other parts are hand-coded. A straightforward approach might be to #include the externally defined class fragments in your main .java file and run a C pre-processor over the file before compilation.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • That would lead to unreadable and hard to maintain code. With Java it is better to stay away from any pre-processors like that. – Eugene Kuleshov Mar 26 '12 at 17:00
  • 1
    @Eugene: Correct. First, I answered his question then suggested a better approach. I edited my answer to make it clearer that a pre-processor route is not recommended. – Eric J. Mar 26 '12 at 17:01
0

I hate to bring up an old thread, but thought I would bring a new perspective in respect to the given answers. Aspect Oriented programming may better achieve what you're trying to implement cross-cutting concerns.

You're looking for a robust associative solution where you can parse out the implementing details to different controlling methods. The Spring framework is very prominent in this space and often is associated with micro-services these days.

Chad Harrison
  • 2,836
  • 4
  • 33
  • 50
0

You can declare methods trough multiple interfaces and then let your concrete classes implement multiple interfaces.

More over, using java.lang.Proxy you can assemble your service from multiple interfaces and delegate actual method calls to a separate implementation.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • 3
    Implementing multiple interfaces in a single class is more or less the inverse of partial classes. – Ted Hopp Mar 26 '12 at 17:25