Computer Programming-2 (CS2301) Polymorphism Lab Session-#09
Java Lab Exercise #09 Polymorphism Evaluation:
Student Name & Number
Instructor: Date:
Q1. Predict the output of the following program: ( final & instanceof )
(i):
Program Output
ERROR! Can't override.
(ii):
Program Output
ob now refers to d
ob is instance of D
ob now refers to c
ob cannot be cast to D
ob can be cast to A
a may be cast to Object
c may be cast to Object
d may be cast to Object
Q2. Write a program (Polymorphism)
Define an class named Figure containing:
- Two instance variable named dim1, dim2 of type double.
1
This study source was downloaded by 100000761285818 from CourseHero.com on 10-03-2022 22:22:57 GMT -05:00
https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Computer Programming-2 (CS2301) Polymorphism Lab Session-#09
- A full parameterized constructor which initiaizes the instance variables.
- A method named area which returns double and also prints a message as “Area for Figure is
undefined”.
Define another class named Rectangle which is extended from class Figure containing:
- A two parameter constructor which initializes all the instance variables of its super class (by
calling its constructor).
- An overridden method named Area which returns the calculated area of the rectangle: (L*W).
Define another class named Triangle which is extended from class Figure containing:
- A two parameter constructor which initializes all the instance variables of its super class (by
calling its constructor).
- An overridden method named Area which returns the calculated area of the triangle: (L*W)/2.
Define a third class named FindArea contains a main method that test the functionality of the
above classes by using super class reference..
Solution:
// Using run-time polymorphism.
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
2
This study source was downloaded by 100000761285818 from CourseHero.com on 10-03-2022 22:22:57 GMT -05:00
https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Computer Programming-2 (CS2301) Polymorphism Lab Session-#09
class FindAreas {
public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}}
3
This study source was downloaded by 100000761285818 from CourseHero.com on 10-03-2022 22:22:57 GMT -05:00
https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Powered by TCPDF (www.tcpdf.org)