Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Why Microsoft has partial classes and Java does not?

3.50/5 (27 votes)
18 Sep 2014CPOL2 min read 80.5K  
In this article we have explained Why Microsoft has partial classes and Java does not have it and how new era of RAD (Rapid application development) and visual programming started.

The secret success (it’s not a secret anymore) of Microsoft was GUI (Graphical User interfaces).

In case you are an old guy like me, you would remember until 80s the operating system were DOS based, with those black windows and cruel commands like dir, edit, mkdir, etc.

Image 1

Suddenly when Microsoft launched Windows 3.X, the whole world went crazy. Simple User Interfaces, no need to remember those DOS commands and windows became THE NUMBER ONE OPERATING SYSTEM in every end user desktop.

So Microsoft tasted its first success by the magic word GUI (Graphical User Interface).

They wanted to take the same success story to PROGRAMMING and Visual Studio was born. A new era of RAD (Rapid Application Development) and visual programming started.

In order to enforce the GUI magic, Microsoft introduced Visual Studio where developers get User Interfaces like grids, buttons, combo boxes, etc. They can drag and drop these UI elements and create user interfaces quickly. Developers also had the luxury to write code easily when these User Interfaces where clicked.

“Behind every successful RAD, there is code behind”.

Now there are two kinds of code in RAD tools:

  • One is auto-generated by the tool where developers drag and drop UI elements like button, text boxes. These kinds of codes are marked with standard Microsoft comments like something below. They expect developers to not touch it as it would lead to undesirable effects.
    ”cs”
    //---------------------------------------------------------------------------
    // <auto-generated>
    //    This code was generated from a template.
    //
    //    Manual changes to this file may cause unexpected behavior in your application.
    //    Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //---------------------------------------------------------------------------
    
    using System;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Data.EntityClient;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using System.Runtime.Serialization;
  • Second is the custom code which developers need to write to fulfill business functionality. This code is not auto-generated and very custom code which developers write for specific needs.

Image 2

Now the biggest challenge for Microsoft was how to keep custom and auto-generated code separate. Because if they are kept in a single file, there are high chances that the code which is auto-generated can overwrite the custom code written by developer leading to havoc.

The answer was “Partial classes”. The concept of partial class is your class can stay into two separate physical files but when they compile, they compile into one DLL unit.

So now, you have one physical file where you have the VS generated code and in the other physical file, the developer can contain custom coding and after compiling, both these files merge into one DLL.

Image 3

When we look at Java, they do not have such kind of RAD approach. So there are no partial classes, no code behind, etc. With full respect, I personally love the idea of partial classes when it comes to RAD and Visual programming.

Some months ago, I just wrote a consolidated article where partial classes are useful, 4 real time use of C# partial classes.

Also below is a simple C# partial classes video created by www.questpond.com team. It talks about the importance of partial classes and partial methods.

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)