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

Inheritance & Polymorphism A) Describe and contrast the three visibility modifie

ID: 3903511 • Letter: I

Question

Inheritance & Polymorphism

A) Describe and contrast the three visibility modifiers: [3]

B) Examine a real world (non-code) example of Polymorphism, explain how the polymorphism is expressed: [3] C) Given this abstract base class: [4] public class Shape { protected int x; protected int y; public Shape(int x, int y) { this.x = x; this.y = y; } public abstract double CalculateArea(); } ?

C) Given this abstract base class: [4]

public class Shape{

protected int x;

       protected int y;

      

       public Shape(int x, int y)

       {

             this.x = x;

             this.y = y;

       }

      

       public abstract double CalculateArea();

}

Create the following classes that will inherit from Shape:

Square

Circle

Provide constructors and a definition for CalculateArea() and in each.

Arrays

A) Referring to problem C above - Exploit Java’s support of inheritance and polymorphism to create a single array of size int NumberOfShapes that can contain any number of Squares and Rectangles:

B) Given two variables: int xSize and ySize and the knowledge that these variables have been given valid integer input… give a code snipet to create a 2D integer array called MultTable and fill it with a multiplication table:

Explanation / Answer

A. answer)

The three visibility modifiers are.

1) Public

Any class, method or a class variable declared with the keyword public is accessible anywhere, in the class, out of class, to other classes, other packages, everywhere.
e.g. public int x;

2) Protected

Any class, method or variable declared with keyword protected is accessible within the same class, different classes of same package, within its subclasses existing in different package, but not in a new class (which is not a subclass) defined in another package.

e.g. protected int y;

3) Private

Any class, method, variable defined with the keyword private is accessible only and only into that class itslelf and nowherelse outside of that class.

e.g private int z;

---------------------------------------------------------------------------------------------------------------------------

B answer)

Basically, Polymorphism means ability to exist in multiple forms based on the required situations.

A office has a Employee, that employee can be a Salaried Employee, A salaried employee can also be a Manager, A Manager can also be an Engineer

By the above example we can see that an Employee can exist in multiple forms even if he is an Employee first, he can act as manager when needed in managerial activities, act as Salaried Employee when he wants to withdraw his Salary, can work as an Engineer when tasked with Engineering tasks.

-------------------------------------------------------------------------------------------------------------------------------

C answer)

// class square

public class Square extends Shape{
public Square(int x)
{
super(x, x);
}
@Override
public double CalculateArea()
{
return super.x * super.y;
}

// class circle

public class Circle extends Shape{
public Circle(int x)
{
super(x, 0);
}
@Override
public double CalculateArea()
{
return Math.pi * super.x * super.x;
}

==========================================================================

Arrays

A answer)

int NumberOfShapes = 100;
Shape[] shapes = new Shape[ NumberOfShapes ];

---------------------------------------------------------------------------------------------------------------------------------------

B Answer)

int[][] MultTable = new int [Size] [ySize];
for(int i = 1; i < sSize; i++)
{
for(int j = 1; j < ySize; y++)
{
int product = i * j;
MultTable [i] [j] = product;
}
}