Polymorphism (Part 2) Fattane Zarrinkalam Why Polymorphism? 2 Paint Program 3 Inheritance Hierarchy Shape - position + getPosition() + draw() + getArea() Circle - radius + getRadius() + draw() + getArea() Rectangle - length - width + + + + getLength() getWidth() draw() getArea() 4 Paint Program • Let’s develop two versions of Paint program • Without polymorphism • With polymorphism 5 Paint Program • Let’s develop two versions of Paint program • Without polymorphism • With polymorphism 6 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; int circlesCount = 0, rectanglesCount = 0; loop: while(true) { showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: circles[circlesCount++] = new Circle(); break; case 2: rectangles[rectanglesCount++] = new Rectangle(); break; } } for(Circle circle: circles) circle.draw(); for(Rectangle rectangle: rectangles) rectangle.draw(); 7 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; A separate array for each type of shape 8 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; A separate array for each type of shape int circlesCount = 0, rectanglesCount = 0; loop: while(true) { A separate counter for each type of showMenu(); shape int choice = readUserChoice(); A separate case for each type of shape switch (choice) { case 0: break loop; case 1: circles[circlesCount++] = new Circle(); break; case 2: rectangles[rectanglesCount++] = new Rectangle(); break; } } 9 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; A separate array for each type of shape int circlesCount = 0, rectanglesCount = 0; loop: while(true) { A separate counter for each type of showMenu(); shape int choice = readUserChoice(); A separate case for each type of shape switch (choice) { case 0: break loop; case 1: circles[circlesCount++] = new Circle(); break; case 2: rectangles[rectanglesCount++] = new Rectangle(); break; } } for(Circle circle: circles) circle.draw(); A separate loop for processing every type of shape for(Rectangle rectangle: rectangles) rectangle.draw(); 10 Paint Program • Let’s develop two versions of Paint program • Without polymorphism • With polymorphism 11 Paint Program – With Polymorphism Shape[] shapes = new Shape[200]; int shapesCount = 0; loop: while(true) { showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: shapes[shapesCount++] = new Circle(); break; case 2: shapes[shapesCount++] = new Rectangle(); break; } } for(Shape shape: shapes) shape.draw(); 12 Paint Program – With Polymorphism Shape[] shapes = new Shape[200]; A single array for all different types of shapes 13 Paint Program – With Polymorphism Shape[] shapes = new Shape[200]; A single array for all different types of shapes int shapesCount = 0; A single counter for all different types of loop: shapes while(true) { A separate case for each type of shape showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: shapes[shapesCount++] = new Circle(); break; case 2: shapes[shapesCount++] = new Rectangle(); break; } } 14 Paint Program – With Polymorphism Shape[] shapes = new Shape[200]; A single array for all different types of shapes int shapesCount = 0; A single counter for all different types of loop: shapes while(true) { A separate case for each type of shape showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: shapes[shapesCount++] = new Circle(); break; case 2: shapes[shapesCount++] = new Rectangle(); break; } } for(Shape shape: shapes) shape.draw(); A single loop for processing over different types of shapes 15 Real World Scale! • What if we have tens of types of shapes? • Extendibility 16 Inheritance Hierarchy Shape - position + getPosition() + draw() + getArea() Circle - radius + getRadius() + draw() + getArea() Rectangle - length - width + + + + getLength() getWidth() draw() getArea() Square - side + getSide() + draw() + getArea() 17 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; Square[] squares = new Square[100]; 18 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; Square[] squares = new Square[100]; int circlesCount = 0, rectanglesCount = 0, squaresCount = 0; loop: while(true) { showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: circles[circlesCount++] = new Circle(); break; case 2: rectangles[rectanglesCount++] = new Rectangle(); break; case 3: squares[squaresCount++] = new Square(); break; } } 19 Paint Program – Without Polymorphism Circle[] circles = new Circle[100]; Rectangle[] rectangles = new Rectangle[100]; Square[] squares = new Square[100]; int circlesCount = 0, rectanglesCount = 0, squaresCount = 0; loop: while(true) { showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: circles[circlesCount++] = new Circle(); break; case 2: rectangles[rectanglesCount++] = new Rectangle(); break; case 3: squares[squaresCount++] = new Square(); break; } } for(Circle circle: circles) circle.draw(); for(Rectangle rectangle: rectangles) rectangle.draw(); for(Square square: squares) square.draw(); 20 Paint Program – With Polymorphism Shape[] shapes = new Shape[300]; 21 Paint Program – With Polymorphism Shape[] shapes = new Shape[300]; int shapesCount = 0; loop: while(true) { showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: shapes[shapesCount++] = new Circle(); break; case 2: shapes[shapesCount++] = new Rectangle(); break; case 3: shapes[shapesCount++] = new Square(); break; } } 22 Paint Program – With Polymorphism Shape[] shapes = new Shape[300]; int shapesCount = 0; loop: while(true) { showMenu(); int choice = readUserChoice(); switch (choice) { case 0: break loop; case 1: shapes[shapesCount++] = new Circle(); break; case 2: shapes[shapesCount++] = new Rectangle(); break; case 3: shapes[shapesCount++] = new Square(); break; } } for(Shape shape: shapes) shape.draw(); 23 • Consider a project that you have done previously: • Think about how inheritance and polymorphism can be applied in that project. • Implement the improved version of the code after applying polymorphism. 24