Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Polymorphism int 1: private protected double 22: //private protecte

ID: 3842845 • Letter: P

Question

public class Polymorphism int 1: private protected double 22: //private protected. Polymorphism z1. 2; z2 4: public void addition (int x, int y) z1. System out println ("The S f an int and int: "+z1); void addition (double x, int y) z2 x y System out println ("InThe sum o le and int :"+z2T public void addition (int x, double y) z2 x y System out println ("InThe sum of an int and double +z2) public void addition (int x, double y, double q) z2 System out println. InThe sum of an int, double and double :"+z2): void subtraction (int x, in y) //protected //private z1 System sut println ("InThe di erence between int and int +z1) public void traction (double x int y) z2 System out prin n ("InThe difference ween double and int :"+z2): class More Poly extends Polymorphism public void addition (int x, int y) Z1 x y System out println Vnoverride fPem More Poly System out println Over The sum of he gr numbers:"+z1); Systems out println OUtput from More ly public void subtraction double x int y) System out println Vnoverride from More Poly Systems out println The difference between the given numbers +z2) Systems out println OUtput from More poly")

Explanation / Answer

The written code illustrates the working of overloading and overriding.

First we understand what is overloading and overriding?

Overloading:- Function overloading can be simply defined as multiple definitions for the same function name in the same scope/class. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. We can’t overload function declarations that differ only by return type.

Overriding:- Function overriding can be defined as when function have same prototype in base class as well as its child class. It occurs when one class acquire the property of another class that is called its parent class.

Now we come to the program

A) This section contains a constructor. A constructor has the same name as the class and it does not return any value. It initializes an instance of its class. A constructor can have any number of parameters.

In the A section there is a constructor named Polymorphism same as its class name and there are two class variables int type z1 and double type z2. Given constructor is initializing the values to the z1 and z2.

B) Section B shows the working of function overloading. There are three functions named addition which have three different definitions under single name. They are differ on the basis of parameters which are passed in the functions like

addition(int x, int y): This function is adding the value of x and y and assigning it’s to z1.

addition(double x, int y): This function is adding the value of x and y and assigning it’s to z2.

       addition(int x, double y):This function is adding the value of x and y and assigning it’s to z2.

C) Section C shows inheritance, where a sub class is inheriting the property of another class. Here class More_Poly extends class Polymorphism and acquires its properties like its protected and public members z1 and z2 and its functions.

D) Section D shows the working of function overriding. There are two functions named addition and subtraction which have same prototype as in base class that is Polymorphism. Working of addition function in child class is again adding its passing parameter and assigns the value to z1. Same as subtraction function in which the subtraction operation is performed on passing parameters and assigning the value to z2.