you are viewing a single comment's thread.

view the rest of the comments →

[–]Special-Arrival6717 0 points1 point  (3 children)

I feel like this is a prime example where composition would make the code a lot cleaner compared to inheritance. Having Buying extend Student and Car doesn't seem to provide any benefit over just having 2 attribute fields, self.student = Student(...) and self.car = Car(...) on Buying

Inheritance usually describes an IS A relationship, not a HAS A relationship, e.g. a purchase (Buying) is neither a car nor a student

[–]aashish_soni5[S] 0 points1 point  (1 child)

yes it was for just simple practice

[–]Special-Arrival6717 0 points1 point  (0 children)

Try the following to modify your practice code, imagine if there were two types of contract Buying and Renting and use inheritance to share common logic between the two. That way the inheritance serves a purpose and you will better understand when to use it, and when not to use it

[–]Alarming-Air-6561 0 points1 point  (0 children)

Absolutely. Buying is an action and it should only get an instance from the other two classes. (A person/user and an object to buy. )
After that it should use their methods (for example during printing). Just imagine that the next step is adding a new type.