Tuesday, February 2, 2010

Object- Oriented Design I

Object-oriented programming (OOP) languages represent a programming paradigm in which objects, data structures consisting of related state and behavior, are used. Object-oriented programming languages are widely used and are considered far more flexible than other programming languages. In object-oriented languages like C++, Java, or C# the principle is that an object encloses everything that should be inside the object. In object-oriented programming, the code that is written once can be reused through inheritance.

Java, originally developed by Sun Microsystems, is a powerful object oriented programming language that can be used to build many web applications. Java was designed in 1991 with the name Oak and in 1995 was renamed Java. Since 1995, Java, a simple, object-oriented, robust interpreted, secure, portable, dynamic and high-performance language, became one of the most popular programming languages for Internet applications (Liang, p.14).

Java can be used on the Web to:
• Build data-driven Web applications for server-side technologies
• Java Servlets can add a Web interface to a database
• Java Servlet API and cookies are used to personalize content
• Building dynamic Web pages (JSP)
• Develop standalone applications for mobile devices

The initial attraction of Java comes from the fact that it was run from a Web browser as an applets. Initially, applets built with Java enhanced user interfaces with buttons, text fields, text areas and radio buttons, and made the Web more responsive and interactive (Liang, p.14).

The critical elements in writing code in Java are objects. Java objects share characteristics with real-world objects, and they have state and behavior. For instance, a car may have the following states: speed and current gear, and its behavior may be accelerating and slowing down. In Java, state of an object is stored in variables, and its behavior is expressed by methods, like in the example code below.
class Car {

//States of an object (car) are expressed variables
int speed = 65;
int gear = 4;


//Object behavior is expressed by methods

void changeGear (int newGear) {
gear = newGear;
}

void accelerate(int increaseSpeed) {
speed = speed + increaseSpeed;
}

void slowDown(int decreaseSpeed) {
speed = speed - decreaseSpeed;
}

void printStates() {
System.out.println(Current speed:" + speed + "Current gear:"+gear);
}
}

A class is the main component of Java programming language and a base from which individual objects are created. The class Car serves as a blueprint to construct new car objects because all cars share same characteristics, and it may be reused multiple times without re-writing of the code.
If there is a need to expand an existing Java object, it is done through inheritance, a mechanism that allows sharing common states and behaviors among other classes. The class Car from the above example becomes a superclass for all classes that reuse it, i.e. for new classes Sport Car, or LuxuryCar.
Creating a new class that inherits from superclass is simple and easy to read, like in the following code:
class SportCar extends Car {

// here come variables and methods for the subclass, SportCar
}

This way the new object, SportCar, receives all characteristics of the object Car. That allows a programmer to focus exclusively on unique features of the new object.



References
Liang, D. (6th ed.). Introduction to Java Programming. Pearson Prentice Hall.
Sun Developer Network (SDN). (n.d.). Retrieved October 28 2009, from http://java.sun.com/

No comments: