Shape and Method Overriding Problem Statement
Create a base class called Shape
that contains a field named shapeType
and a method printMyType
.
Implement two derived classes:
- Square: This class inherits from
Shape
and includes an additional fieldlength
. It has two methods,calculateArea
that returns the area, andprintMyType
that outputs the type of the object. - Rectangle: This class also inherits from
Shape
and includes additional fieldslength
andbreadth
. It includes the same two methods asSquare
.
Example:
Input:
// No specific input - This is an object-oriented programming task
Output:
Create a Square object with a length of 5:
Square
Area: 25
Create a Rectangle object with a length of 5 and breadth of 4:
Rectangle
Area: 20
Explanation:
In the main
method, you should follow these steps:
- Create an instance of the
Square
class withlength = 5
, use theprintMyType
method, and then callcalculateArea
. - Create an instance of the
Rectangle
class withlength = 5
andbreadth = 4
, use theprintMyType
method, and then callcalculateArea
.
Constraints:
- Ensure methods and variables are correctly encapsulated within the appropriate classes.
length
andbreadth
are non-negative integers.
Note:
Focus on proper class design and method overriding principles. This problem emphasizes object-oriented programming concepts.
AnswerBot
6d
Create base class Shape with field shapeType and method printMyType. Implement Square and Rectangle classes with calculateArea method.
Create a base class Shape with shapeType field and printMyType met...read more
Help your peers!
Add answer anonymously...
Top Hexaware Technologies Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
>
Hexaware Technologies Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app