Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1. Write a program that defines a Shape class that demonstrates encapsulation by using private variables and public getters and setters. The class also
Question 1. Write a program that defines a Shape class that demonstrates encapsulation by using private variables and public getters and setters. The class also demonstrates polymorphism by having a draw() method that can be overridden in subclasses. The Rectangle and Circle classes inherit from the Shape class and demonstrate inheritance by having access to the variables and methods of the Shape class and also they override the draw() method. You are required to: i) Implement the inheritance hierarchy illustrated above. ii) In the main method, apply polymorphism by creating objects of Rectangle, Circle, and Shape and calling the 'draw' method on them with the same reference. iii) In the Shape class, create Area() methods that calculate the areas of a Cube, Triangle, and Rectangle individually in that order. From the main method, create a Shape object and call these area methods to demonstrate method overriding. Sample output is given below
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started