Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Polymorfisme “Dynamic binding provides design flexibility and may enhance software maintainability. “"— Presentation transcript:

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

2 Static Binding All code is bound during compilation. Static binding is limited and may lead to difficulty in software maintenance. Let see the example: “Shape”

3 Data 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;

4 Main 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

5 Routine 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

6 Problems in static binding Different 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.

7 Problems in static binding Note 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.

8 Adding triangle in data section Data Section Type... Triangle = Record String name; int base, height; End Variable... t : Triangle; Multiple places are affected

9 Adding Triangle in main code Main 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

10 Adding Routine Routine 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

11 Problems in static binding Multiple places are affected as a result of extending shape types, and this is error prone.

12 Dynamic Binding The 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.

13 Dynamic Binding class 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

14 Dynamic Binding class 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

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

16 Overloading Circle 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.

17 Same Methods Signature Two 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.

18 Overloading method names Methods having the same method signature may pose problems in the compilation process depending on where they are used in the program.

19 Overloading method names class A {... A() {... } A(int x) {... } A(int x, int y) {... } A(String x) {... }... }

20 Overloading method names In 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.

21 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)

22 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.

23 Duplicate method in same class=invalid The 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() {} }

24 Overloading Declaring methods of the same signature in different classes are considered as valid in object-oriented programming: Sample code

25 Polymorphisme The 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.

26 Benefit of Polymorphisme Software maintanability Incremental development Increase Code Readability


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

Similar presentations


Ads by Google