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 field length. It has two methods, calculateArea that returns the area, and printMyType that outputs the type of the object.
  • Rectangle: This class also inherits from Shape and includes additional fields length and breadth. It includes the same two methods as Square.

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 with length = 5, use the printMyType method, and then call calculateArea.
  • Create an instance of the Rectangle class with length = 5 and breadth = 4, use the printMyType method, and then call calculateArea.

Constraints:

  • Ensure methods and variables are correctly encapsulated within the appropriate classes.
  • length and breadth 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...
Hexaware Technologies Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter