09 Java Lab Exercise Polymorphism Solution 1
09 Java Lab Exercise Polymorphism Solution 1
Q1. Predict the output of the following program: ( final & instanceof )
(i):
Program Output
(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
https://www.coursehero.com/file/34709614/09-Java-Lab-Exercise-Polymorphism-Solution-1docx/
Computer Programming-2 (CS2301) Polymorphism Lab Session-#09
Define a third class named FindArea contains a main method that test the functionality of the
above classes by using super class reference..
Solution:
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)