Typing with explicit type checks – approach 2
In this section, we will look at another approach for applying specific data types to variables. In the first approach, we developed a typecheck
class and used the type checking methods themselves to create new data types. In this example, we will be creating the typecheck
class with each type checking method to check that the input value belongs to the expected type and returns a Boolean value based on the condition’s result. This method of type checking gives us the flexibility of modifying the Fashion
class to provide variable-specific error messages when the condition is not met.
Creating a class to implement type checking
In this example, let’s begin by creating the typecheck
class.
The typecheck
class is created here to make all the methods in this class reusable just in case all the methods in the type check code need to be exported into a different file for later use.