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

Implementing Polymorphism in Java 1. Objectives At the end of this lab you shoul

ID: 3583698 • Letter: I

Question

Implementing Polymorphism in Java

1. Objectives

At the end of this lab you should be able to:

Improve Eclipse usage

Verify your understanding of Java instructions

Practise on understanding a project specifications

2. Principle

NOTE : implement the following classes inside the same package BUT in different files!!!!

Implement a Console Application that creates a Shape class with the following Methods:

Draw() : empty method

Create a Circle class that inherits from SHAPE with the following

Attributes : x,y centre position – R for ray

Methods:

            Constructor () : initialize all the attributes

Draw () : prints out the string : “Drawing a Circle at position …”

Create a Rectangle class that inherits from SHAPE with the following

Attributes : x,y centre position
                   z,w base & high

Methods:

            Constructor () : initialize all the attributes

Draw () : prints out the string : “Drawing a Rectangle at position …”

Create a SHAPES class implementing the main() method and where you create an array of type SHAPE containing 2 elements:

Circle (10,20,30)

Rectangle (20,30,40,50)

Implement a for loop that reads element from the array and call the Draw() method

NOTE : to create the array use the following
Shape [] shapes = {new Circle(….) , new Rectangle(…..)}

Considerations:

What is the result you get when call the Draw() method for the elements inside the array?

Can you explain why you have this result?

Can you create a connection between the result you get and the Polymorphism concept?

IMPORTANT : you can understand this example only if the you have understood the Polymorphism concept!!!!

3. Materials

The Materials needed for this lab are:

Eclipse

4. Key Point

Understanding how to implement Polymorphism in Java

6. Lab Step

To solve this lab, implement the follow

7. Results and discussion

In this lab you improved your knowledge about how to use Polymorphism in Java and implementing the corresponding code in Eclipse

Explanation / Answer

Call the Draw method for the elements inside the array:

public static class Circle
{
public int xPos;
public int yPos;
public int red;
public int green;
public int blue;
public Color circleFill;

public Circle(int xin, int yin, double radius)
{
xPos = xin;
yPos = yin;
red = (int)Math.random()*255;
green = (int)Math.random()*255;
blue = (int)Math.random()*255;
circleFill = new Color(red, green, blue);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
circle.draw(g);

g.fillOval(xPos, yPos, radius, radius);
}
}

Polymorphism concept:

package writing {
public class RollerBallPen extends Pen {
override public function line():void {
trace("RollerBallPen drew a super smooth line.");
}
  
override public function circle():void {
trace("RollerBallPen easily drew a circle.");
}
}
}


Java implements polymorphism :

1) Compile Time which in other terms referred as method overloading where compiler is able to understand two methods having same name but different number type/ order of signature or parameters
2) Runtime Polymorphisim also referred as method overriding : Prerequisite to achive the same is inheritance where you define the same method in child class having same number/type/order of parameters where during compile time compiler will find the reference for parent class but during run time only the method of the actual instance of the object will be executed and hence the behaviour of the method to run is determined at run time by the JVM.

Polymorphism in Java:

Method Overloading:
To call an overloaded method in Java, it is must to use the type and/or number of arguments to determine which version of the overloaded method to actually call.
Overloaded methods may have different return types the return type alone is insufficient to distinguish two versions of a method. .
When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
It allows the user to achieve compile time polymorphism.
An overloaded method can throw different exceptions.
It can have different access modifiers.

Example:

class Overload
{
void demo (int a)
{
System.out.println ("a: " + a);
}
void demo (int a, int b)
{
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloading
{
public static void main (String args [])
{
Overload Obj = new Overload();
double result;
Obj .demo(10);
Obj .demo(10, 20);
result = Obj .demo(5.5);
System.out.println("O/P : " + result);
}
}

Output:
a: 10
a and b: 10,20
double a: 5.5
O/p : 30.25.


Method Overriding:
Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding.

Example:

public class BaseClass
{
public void methodToOverride()
{
System.out.println ("I'm the method of BaseClass");
}
}
public class DerivedClass extends BaseClass
{
public void methodToOverride()
{
System.out.println ("I'm the method of DerivedClass");
}
}

public class TestMethod
{
public static void main (String args []) {
BaseClass obj1 = new BaseClass();
BaseClass obj2 = new DerivedClass();
obj1.methodToOverride();
obj2.methodToOverride();
}
}

Output:

I'm the method of BaseClass
I'm the method of DerivedClass.