35

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.

3
  • 11
    "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 Commented Mar 26, 2012 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. Commented Mar 26, 2012 at 17:02
  • 1
    @Jesper: He's actually trying to implement the single responsibility principle, but is not using composition to achieve it. Commented Mar 26, 2012 at 17:07

5 Answers 5

46

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

Sign up to request clarification or add additional context in comments.

8 Comments

@EricJ. - I understand that partial classes have their uses. However, they can easily turn into a nightmare. Microsoft itself suggests 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.
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...
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.
@KonradMorawski - But the danger is greatly enhanced. From the docs: "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.
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.
|
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.

Comments

1

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.

2 Comments

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

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

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

1 Comment

Implementing multiple interfaces in a single class is more or less the inverse of partial classes.
-1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.