Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to implement partial classes in java

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.

like image 902
SoftwareSavant Avatar asked Mar 26 '12 16:03

SoftwareSavant


People also ask

How do you create a partial class in Java?

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.

How do you implement a partial method?

In order to create a partial method, it must be declared first(like an abstract method), with a signature only and no definition. After it is declared, its body can be defined in the same component or different component of the partial class/struct . A partial method is implicitly private.

How do you create a partial class?

A partial class is created by using a partial keyword. This keyword is also useful to split the functionality of methods, interfaces, or structure into multiple files.

What is a partial class definition in Java?

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler.


2 Answers

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

like image 89
Ted Hopp Avatar answered Oct 13 '22 21:10

Ted Hopp


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.

like image 38
abgalphabet Avatar answered Oct 13 '22 23:10

abgalphabet