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

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

Post on 19-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

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

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

Page 2: 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”

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

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;

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

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

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

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

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

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.

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

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.

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

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

height; End Variable ... t : Triangle;

Multiple places are affected

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

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

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

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

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

Problems in static binding

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

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

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.

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

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

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

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

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

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; } }

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

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.

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

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.

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

Overloading method namesMethods having the same

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

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

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

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

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.

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

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)

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

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.

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

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() {}

}

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

OverloadingDeclaring methods of the same

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

Sample code

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

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.

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

Benefit of PolymorphismeSoftware maintanabilityIncremental developmentIncrease Code Readability