Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

partial classes/partial class file

In C# .net there is a provision to have two different class files and make them a single class using the keyword partial keyword.this helps it to keep [for ex]UI and logic seperate. of course we can have two classes to achieve this one for UI and other for logic. Can this be achieved in java some how?

like image 209
Ravisha Avatar asked May 04 '10 09:05

Ravisha


People also ask

How do you use partial classes?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

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. Each part of a partial class has the same accessibility.

What is partial class in dot net?

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.

What is partial class in MVC?

A partial class is one that can be split among multiple physical files. This feature came in C# 2.0. The partial class break the definition of class two or more than two class files, but it will be together at compile time as one class. Now here we will be using partial concept in MVC using entity framework.


1 Answers

On source file splitting

No. Java source codes can not be split across multiple files.

From the Wikipedia article Comparison of Java and C Sharp

The Sun Microsystems Java compiler requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later allows a class definition to be split into several files, by using the partial keyword in the source code. In Java, a public class will always be in its own source file. In C#, source code files and logical units separation are not tightly related.


On separating logic and UI into their own classes

The two classes approach is a much better solution than the one-class-two-source approach in this case, because the separation is enforced in the design, not just physical separation in the source codes.

See also

  • Separation of logic and UI
  • oo question - mixing controller logic and business logic
  • Why is good UI design so hard for some Developers?
like image 112
polygenelubricants Avatar answered Sep 28 '22 16:09

polygenelubricants