polymorfisme “dynamic binding provides design flexibility and may enhance software...

Post on 19-Dec-2015

218 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Polymorfisme“Dynamic binding provides design flexibility and may enhance software maintainability. “

Static BindingAll code is bound during

compilation.Static binding is limited and may

lead to difficulty in software maintenance.

Let see the example: “Shape”

Data SectionData Section Type Circle = Record String name; int radius; End Square = Record String name; int side; End Variable shapeArray : Array [1..5] of char; c : Circle; s : Square;

Main Code SectionMain Code Section c.name = "Circle C"; c.radius = 2; s.name = "Square S"; s.side = 3; A[1] = 'c'; A[2] = 's'; For int i = 1 to 2 do { Switch shapeArray[i] 'c' : calculateCircleArea(); 's' : calculateSquareArea(); 'n' : do nothing; End (Case) }

Statically bound at compile

time

Routine SectionRoutine Section calculateCircleArea() { float area = 3.14 * c.radius * c.radius; writeln ("The area of ", c.name, " is ", area, " sq. cm.");

} calculateSquareArea() { float area = s.side * s.side; writeln ("The area of ", s.name, " is ", area, " sq. cm.");

}

Two distinct routine or more are required

Problems in static bindingDifferent routine names therefore

have to be devised (most proce-dural programming languages do not allow two routines to have the same name.)

With static binding, problems may arise in code maintenance.

To illustrate, let us now add another shape, Triangle, to the code.

Problems in static bindingNote that changes were made at

the following points: ◦ in the data section where a triangle is

defined; ◦ in the main code section where the

detection of a triangle and the appropriate routine call have to be included in the switch statement;

◦ in the routine section where the specific routine for calculating the area of the triangle has to be included.

Adding triangle in data sectionData Section Type ... Triangle = Record String name; int base,

height; End Variable ... t : Triangle;

Multiple places are affected

Adding Triangle in main codeMain Code Section... t.name = "Triangle T"; t.base = 4; t.height = 5; ... A[3] = 't'; For int i = 1 to 3 do { Switch shapeArray[i] 'c' : calculateCircleArea(); 's' : calculateSquareArea(); 't' : calculateTriangleArea(); 'n' : do nothing; End (Case) }

Multiple places are affected

Adding RoutineRoutine Section calculateCircleArea() { ... } calculateSquareArea() { ... } calculateTriangleArea() { float area = 0.5f * t.base * t.height; writeln ("The area of ", t.name, " is ", area, " sq. cm.");

}

Multiple places are affected

Problems in static binding

Multiple places are affected as a result of extending shape types, and this is error prone.

Dynamic BindingThe binding of variables to routines (or

methods in object-oriented programming terms) is done at run time.

The method has been selected based on the class of the shape referenced at run-time.

This is only possible in programming languages that support dynamic binding.

With dynamic binding, the variable Array is bound to an object method at run time when the class referenced is known.

Dynamic Bindingclass Shape { private String name; Shape(String aName) {name=aName;} public String getName() {return name;} public float calculateArea() {return 0.0f;} public static void main(String argv[]) { Circle c = new Circle(”Circle C”); Square s = new Square(”Square S”); Shape shapeArray[] = {c, s}; for (int i=0; i<shapeArray.length; i++) {

System.out.println("The area of " + shapeArray[i].getName()

+ " is " + shapeArray[i].calculateArea()+" sq. cm.\n");

} } }

switch statement is

not required with

dynamic binding.

Only one method to all kind of shape

Dynamic Bindingclass Circle extends Shape { private int radius; Circle(String aName) { super(aName); radius = 3; } public float calculateArea() { float area; area = (float) (3.14 * radius * radius); return area; } }

Each shape own it’s class

Dynamic Bindingclass Square extends Shape { private int side; Square(String aName) { super(aName); side = 3; } public float calculateArea() { int area; area = side * side; return area; } }

OverloadingCircle and Square have similar

calculateArea() method in their class definition.

Although both methods have the same method signature, they have different method implementation, since the formula for calculating area of each is not the same.

Same Methods SignatureTwo methods are said to have the

same method signature if: ◦the name of the methods are the

same; and ◦the number and type of formal

parameters are the same.

Overloading method namesMethods having the same

method signature may pose problems in the compilation process depending on where they are used in the program.

Overloading method namesclass A { ... A() { ... } A(int x) { ... } A(int x, int y) { ... } A(String x) { ... } ... }

Overloading method namesIn the code fragment below,

method A() is overloaded by A(int x), A(int x, int y), and A(String s).

These four methods are distinguished in the compilation process by the number and type of parameters present in the method call.

Overloading method names A thisA = new A(); A() A thisA = new A(3); A(int x) A thisA = new A(4, 5); A(int x, int y) A thisA = new A(”hello”); A(String x)

Duplicate method in same class=invalid

The definition of method a1() is reported as a duplicate method declaration by the compiler since the two methods have been declared in the same class.

Duplicate method in same class=invalidThe return type of a method does not

distinguish overloaded methods from one another as the following example shows.

Method a1() is flagged as invalid by the compiler as both of them are considered similar.

class A {

A() {}

public int a1() {}

public void a1() {}

}

OverloadingDeclaring methods of the same

signature in different classes are considered as valid in object-oriented programming:

Sample code

PolymorphismeThe ability of different objects to

perform the appropriate method in response to the same message is known as polymorphism.

Polymorphism is facilitated by dynamic binding and the ability to use the same name for similar methods across class definitions.

Benefit of PolymorphismeSoftware maintanabilityIncremental developmentIncrease Code Readability

top related