Polymorphism consider the foliowing code publie class Polymorph)( public statie
ID: 3904845 • Letter: P
Question
Polymorphism consider the foliowing code publie class Polymorph)( public statie void main(Stringll arga) ( oblowd poblic Rectangleo public Rectangle / printinI calcRectPerim (1. 4) System.out.printin"rectangle 2 perimeter:"/printin 2 calcRectPerim (1.0, 4.0)) System.out.printin( rectangle 3 perimeterprintin 3 calcRectPerim (5, 4.5)); double width, double height) t this width width hs height height System.out,printinC rectangle 4 perimeterprintin calcRectPerim (rect) public double gotAreao ( return width height publie static double calcRectPerim (double sidel, method A public double getPerimetero ( return (width 2)+ (height 2): return (sidel + side2) 2 public static int calcRectPerim(int sidel // method int side2) ( return (side1 + side2)2 public static double caleRectperim dRectangle r) { // method ? return r.getPerimeter0; For each printin statement in main, which method is called (A, B, or C?) Systerm.out.printin 1 System.out.println 2 System.out.println 3 System.out.printin 4 What does this code print?Explanation / Answer
System.out.println 1 calls Method B and prints rectangle 1 perimeter: 10
System.out.println 2 calls Method A and prints rectangle 2 perimeter: 10.0
System.out.println 3 calls Method A and prints rectangle 3 perimeter: 19.0
System.out.println 1 calls Method C and prints rectangle 4 perimeter: 26.0
Hence the output is:
rectangle 1 perimeter: 10 i.e., ( (1 + 4 ) * 2 )
rectangle 2 perimeter: 10.0 i.e., ( (1.0 + 4.0) * 2 )
rectangle 3 perimeter: 19.0 i.e., ( (5+4.5) * 2 )
rectangle 4 perimeter: 26.0 i.e., ( (9.0*2) + (4.0*2) )