Comparison between two success Programming Languages, C and Java (structure, union with class)

As we all knows, Java programming language has many similarity with C and C++ programming language. Here I am trying to show the substitution of Structure and Union with class.

If we see in java, C struct construction was omitted because a class gives more than the structure do. Class special feature is the Encapsulation, which make it much better than the struc. A structure typically contains at least two fields: union and a tag.

The C union construct is most frequently used to define structure capable of holding more than one type of data. And tag is just an ordinary field used to indicate which of the possible type is held by the union. A tag is generally an enum type. A structure containing a union and a tag is sometimes called a discriminated union.

Here is an example in which the shape_t type is discriminated union that can be used to represent either a rectangle or a circle. The area function take pointer to a shape_t structure and returns its area or -1.0, if the structure is invalid.

#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef enum {RECTANGLE, CIRCLE} shapeType_t;
typedef struct
{
    double length;
    double width;
}rectangleDimensions_t;
typedef struct
{
    double radius;
}circleDimensions_t;
typedef struct
{
    shapeType_t tag;
    union{
        rectangleDimensions_t rectangle;
        circleDimensions_t circle;
    }dimensions;
}shape_t;

double area(shape_t *shape)
{
    switch(shape->tag)
    {
        case RECTANGLE:
            {
                double length = shape->dimensions.rectangle.length;
                double width = shape->dimensions.rectangle.width;
                return length*width;
            }
        case CIRCLE:
            {
                double radius = shape->dimensions.circle.radius;
                return M_PI * (radius*radius);
            }
        default:
            return -1.0; //invalid tag;
    }
}
void main()
{
    shapeType_t tag;
    shape_t *shape;
    shape->dimensions.rectangle.length=10;
    shape->dimensions.rectangle.width=15;
    shape->tag=RECTANGLE;
    clrscr();
    printf(“The area is %6.2f”,area(shape));
    getch();
}

Output:
The area is 150.00

The union construct has omitted in Java because there is a much better mechanism for defining a single data type capable of representing object of various types: Subtyping . A discriminated union is really just a pallid imitation of class hierarchy.

To transform a discriminated union into class hierarchy, we define an abstract class containing abstract method for each operation whose behavior depends on the value of tag. In above example, there is only one operation, area, which is the root of the class hierarchy.

Next, we define a concrete subclass of the root class for each type that can represented by the discriminated union. In above example, the types are circle and rectangle.

Then we include the data fields to its subclass. In above example, radius is particular to circle, and length and width are particular to rectangle.

So the example goes like this:

abstract class Shape
{
    abstract double area();
}
class Circle extends Shape
{
    final double radius;
    Circle(double radius)
    {
        this.radius=radius;
    }
    double area()
    {
        return Math.PI * radius*radius;
    }
}
class Rectangle extends Shape
{
    final double length;
    final double width;
    Rectangle(double length, double width)
    {
        this.length=length;
        this.width=width;
    }
    double area()
    {
        return length*width;
    }
}
public class StructUnion
{
    public static void main(String args[])
    {
        //Circle cir = new Circle(0);
        Rectangle rec = new Rectangle(15,10);
        System.out.println(“The area is ” + rec.area());  
    }
}

 

Reference from: Effective Java, Joshua Bloch

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: