Limitations of Type Hinting for Objects

Learn about the limitations of object type hinting.

We'll cover the following

Limitations of object type hinting

Let’s consider a scenario. A manager at a car rental company that rents only BMWs hires a programmer to write a program that calculates the price for a full tank of gas for each and every car that the company owns. In accordance with these demands, the programmer writes a class Bmw that holds the code for this task. This class has the relevant data about the BMWs, including the license plate number, car model, and most importantly, a method that calculates the volume of the fuel tank.

The function that calculates the volume is called calcTankVolume(). This method calculates the tank volume by multiplying the base area (the square of the base length) by the height. The height, the base length, and the license plate number are introduced to the class through the constructor.

Outside the class, the programmer writes a function calcTankPrice() to calculate the price for a full tank of gas by multiplying the tank volume by the price per gallon. However, since the programmer doesn’t want the function to get any arguments other than those that belong to the Bmw class, they use type hinting.

Get hands-on with 1200+ tech skills courses.