Solving the exercise using oops concepts