Sunday, November 29, 2009

Structure of an Object Oriented Program


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

In the last post of this series, we have seen how the structure of a very simple program in C++/Java/C# looks like. The C++ program was written in procedural manner, since at least the entry point function main must be global (not member of any class). Java is a near pure OO language (apart from the non-object primitive datatypes like int, long, float). So every method must be part of a class. Many argue that C# is a pure OO language since its primitive types are implicitly derived from System.ValueType which is, in turn, derived from System.Object. Anyway, like Java all data members and methods in C# belong to some class. In today's post, we will see the various elements of an OO program, their commonalities and variabilites.

Namespace/Package: Sets context for an item

A namespace groups a set of related elements and guarantees identity of their names. It draws a conceptual boundary around the items which distinguishes them from elements with the same name in other namespace. It is not mandatory to use namespace, but absence of it can confuse you even for moderate size programs. If you are a library programmer, you should use namespaces, otherwise user of the library can easily be confused due to frequent compiler errors caused by duplicate names. The namespace is hierarchical in nature. You can have a namespace containing another namespace.

Let's take a code snippet as an example. The following C++ code will not compile due to duplicate identifiers.

int doSum(int x, int y);
int doSum(int x, int y);


If you really need the same identifier name, you can distribute them in different namespaces.

namespace Aggregate
{

    int
 doSum(int x, int y);
}


namespace Separate
{

    int
 doSum(int x, int y);
}

Java has implemented namespace concept bit differently than C++/C#. Java uses package to organize the classes of the program and defines the hierarchy how the class files will be deployed in the file system of target machine. Package inherently defines the namespace. A package name java.lang has the following meanning:
  1. java.lang must be the first executable line in the source file
  2. java.lang is a namespace
  3. java is a namespace
  4. There exists a directory named java (may be only virtual if e.g. inside a .jar file)
  5. There is a subdirectory named lang inside java (java\lang)

Using Namespace/Package: The namespace (in C++/C#) or package (in Java) must be imported before they can be used in the source. The using namespace keyword serves the purpose in C++ and C# while in Java import keyword must be used. You can also obviously use the fully qualified name without using the keyword in all these languages.
C++
Definition:
namespace HexEditorApp
{

  public
:
      int
 count = 10;
      int
 doSum(int x, int y)
      {

        return
 x + y;
      }
}

Usage:
using namespace HexEditorApp;

public class
 HexConverter 
{
 
public
: 
    void
 Convert() 
    {
 
        int
 countElement = HexEditorApp::count; // fully qualified name 
        int sum = doSum(10, 20); // call directly, as the method is already imported 
    } 
}

Java
Definition:
package HexEditorApp; // it must be the first executable line of the file 

/* In java (as well as C#), you can't declare a variable or method outside a User Defined Type (UDT) definition e.g. class, enum, struct (for C#)*/


public class
 HexEditor {
  public
 int count = 10;
  public
 int doSum(int x, int y){
     return
 x + y;
  }
}

Usage:
import HexEditorApp; // import keyword for type inclusion

public class
 HexConverter {
  public
 void Convert() {
     int
 countElement = HexEditorApp::count; // fully qualified name
     int sum = doSum(10, 20); // call directly, as the method is already imported
   }
}

C#
Definition:
namespace HexEditorApp
{


/* In java (as well as C#), you can't declare a variable or method outside // a User Defined Type (UDT) definition e.g. class, enum, struct (for C#)*/


    class
 HexEditor
    {

        public
 int count = 10;
        public
 int doSum(int x, int y)
        {

            return
 x + y;
        }
    }
}

Usage:
using namespace HexEditorApp;

public class
 HexConverter
{

    public
 void Convert()
    {

        int
 countElement = HexEditorApp::count; // fully qualified name
        int sum = doSum(10, 20); // call directly, as the method is already imported
    }
}

Definition vs. Declaration: Make identifier known to compiler w/o values
We declare an identifier to make it known to the compiler so that we can use it afterwards. But this is only the half of the story. With definition, we give a concrete meaning to this identifier.

// declaration of variable
int count;
// definition of the variable
count = 0;
//declaration + definition of variable
int count = 0;

//declaration of a method
int doSum(int x, int y);

//definition of a method
int doSum(int x, int y)
{

     return
 x+y;
}

Both Java and C# assign default value to any primitive type declaration i.e. these identifiers are defined in place of declarations (more on this in the next post). These languages also differ from C++ on how they define methods. In C++ it is possible to declare the methods at one place and define them at some other place. But in Java and C#, you must define the method at the place of declaration.

No comments:

Post a Comment