Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do partial classes in Java (like C#)?

C# has this great concept where a class can be spread across multiple .cs files. This works great where you want a single object (member variables that all the code needs) but there's a ton of code. You can then spread this code out by functionality across source files.

Is there a way to do this in Java?

Update: Ok at first I told myself that this must be a single large class (it performs layout of the content of a DOCX file). But then after posting this I thought about it more and it really bothered me that it is this one large (5,000+ lines at present) class.

So I thought through some alternatives and came up with a good way to break it out into one main class and about 20 helper classes. It works very well this way really separating out the functionality into each part.

So... while I think partial classes is a useful construct at times, in this case the lack of partial classes caused me to come up with a better design. (And this has nothing to do with the initial question, but I thought it was worth sharing.)

like image 247
David Thielen Avatar asked Dec 13 '13 22:12

David Thielen


People also ask

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.

Does C++ have partial class?

There can be zero or more partial class definitions for every full definition of a class. Every partial class definition of a class must lexically precede the one full definition of that class, but doesn't have to precede forward declarations of the class.

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.

Can partial classes be in different assemblies?

No, you cannot have two partial classes referring to the same class in two different assemblies (projects). Once the assembly is compiled, the meta-data is baked in, and your classes are no longer partial. Partial classes allows you to split the definition of the same class into two files.

Do partial classes have to be in same namespace?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project.

What is partial C#?

Partial Class is a unique feature of C#. It can break the functionality of a single class into many files. When the application is compiled, these files are then reassembled into a single class file. The partial keyword is used to build a partial class.


2 Answers

No. Java does not support partial classes. You'll find more in depth discussion in this question and this question.

like image 69
Asaph Avatar answered Sep 21 '22 12:09

Asaph


No, Java doesn't support partial classes.

If this is just idle curiosity, check out Scala. It compiles to .class files just like Java, and has full interop. It supports a rough equivalent of partial classes as "traits".

An example of trait usage:

trait FunctionalityA {
  // some stuff implemented here
}

trait FunctionalityB {
  // more stuff implemented...
}

// etc...

class MyBigClass extends FunctionalityA with FunctionalityB with FunctionalityC
like image 35
Dylan Avatar answered Sep 23 '22 12:09

Dylan