Search⌘ K

Challenge 1: Implement Rectangle Class Using the Encapsulation

Explore how to implement a Rectangle class in Python by applying encapsulation principles. Learn to define private properties for length and width, and develop methods to calculate the area and perimeter. This lesson helps you understand information hiding by coding practical class methods step-by-step.

Problem statement

You are given a partially completed code of a Rectangle class in the editor. Implement the class by completing the tasks below.

Task 1

Implement a constructor to initialize the values of two private properties: length and width.

Task 2

Implement a method, area(), in the Rectangle class that returns the product of length and width. See the formula below:

Area=length×widthArea = length \times width ...