Search for:

Posted on August 29th, 2012, by

OOP (object-oriented programming) can be traced up to 1960s, but the concepts of OOP became widely accepted since 1990s, and nowadays the majority of large projects are object-based; almost every programming language supports OOP concepts and allows to build classes of various difficulty. Today there are two most widely used programming concepts: OOP and RDBMS (relational database management systems), and these ideas usually work in a chain (Eckel 2006, p. 43). The aim of this essay is to analyze the concept of polymorphism ”“ one of the core OOP ideas, its realization in Java language and the interaction of polymorphism with other programming principles.

1. Principles of OOP

OOP is usually considered as the opponent of modular programming, where focus on procedures is done. In OOP the focus is on data rather than procedures, and the whole program consists of blocks (modules), each of them realizes a certain concept or block in the program. Objected-oriented programming and its basic concepts were designed in order to enable programmers to build models of the real world (to be more exact, of certain parts of the real world). The three main principles of OOP are data abstraction, inheritance and polymorphism. It is necessary to mention that Java is strongly OOP-based and all operations in Java are maintained with the help of classes, either embedded or created ones.

2. Polymorphism

The word “polymorphism”ť originates from Greek language, and used to mean “many parts”ť. In the context of object-oriented programming it means methods for different classes that have the same name but perform different actions. Shortly, the idea of polymorphism sounds as “one name, many forms”ť. Generally, polymorphism is used when there should be a single method that works correctly with different classes; the same name is often used for transparency of code and ideas, and for the purpose of compatibility as well.

In Java language, there are two main realizations of polymorphism: overriding, also referred to as run-time polymorphism, and overloading, called also “compile-time polymorphism”ť. For overloading, the computer decides which method to execute on the stage of compiling; for overriding, such decisions are performed dynamically.

Overloading is predicted to work quicker and is less resource-intensive. A good example of declaring overriding methods is the method which finds maximal of two numbers. The realization is the same, however in order to enable this class work for different types, we can use:

long Math.max (long a, long b);

int Math.max (int a, int b);

double Math.max (double a, double b);

float Math.max (float a, float b);

Another example of overloading: if we need a method that returns the biggest module for two integers, and for one number it returns the module of that integer we can write:

Class Example1

{

//method for two integers

int test (int a, int b){

if (a>b)

     return Math.abs(a);

else

     return Math.abs(a);

}

//method for one integer

int test(int a){

return Math.abs(a);

}

}

Second, and more complex form of polymorphism is overriding. Usually, the word “polymorphism”ť implies overriding; it takes place when a subclass has the same name and signature as the method belonging to one of its superclasses. Formally, the polymorphism mechanism realized by overriding is formulated as follows: “If a subclass object is assigned to a superclass reference, and a method of the superclass is invoked which is overridden in the subclass object, then via polymorphism, the correct method of the subclass object will be called”ť (Budd 1999, p. 37).

Let us create a hierarchy of classes illustrating the mechanism of overriding; for example, these can be Employee (base class) and two derivative classes: SalesPerson and Programmer. Suggesting that most employees get a fixed salary, we can calculate the salary per year by simply multiplying the monthly salary by 12. Taking into account that SalesPerson may also get bonuses for good sales, we receive the model where method of counting yearly salary requires overriding. Below parts of code realizing the described hierarchy are showed:

public class Employee

//generic class for all employees

{

     private float fMonthlyPay; //payment per month variable

     public float countYearlyPay()

     {return fMonthlyPay*12.0f;}

}

public class Programmer extends Employee

//class defining programmers; beside payment, they also have the label at which project they are working;

{

     private String project; //

}

public class SalesPerson extends Employee

// class for salesmen; here a yearly bonus value is added to the class and method of calculating salary is overridden;

{

     private float fBonus; //bonus value

public float countYearlyPay()

     {return fMonthlyPay*12.0f + fBonus;}

}

In this case, for all employees beside salesmen salary is calculated as monthly payment multiplied by 12, and for salesmen bonus is also added. Thus, the method is chosen while the program is being executed. It is necessary to mention that polymorphism (overriding form) works best with inheritance (in our example classes Programmer and SalesPerson have inherited method countYearlyPay and for SalesPerson it is overridden).

Conclusion

Though polymorphism is nowadays realized in all programming languages, Java is based on object-oriented technology ”“ “all Java identifiers are dynamically bound and are potentially polymorphic”ť (Lea & Lea,1996, p. 47). Therefore, understanding concepts of inheritance, polymorphism and data abstraction are the clue to understanding the whole  Java programming process.

Posted in Free essays | Tagged | Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *





0 Comments