Does Python support operator overloading

Override attributes and methods in classes

Our program from the last chapter is for the cat (literally). The dog loses out because it cannot communicate correctly. The inherited method does not fit!

But what about methods that don't work that way? Our method "tut_miauen ()" may still be suitable for cats, but it is strange for dogs. So the parent class gets the method "tut_reden ()" (sorry, I can't think of a better method name). In the "speech" there is also an output showing who is talking:

We obtain:

Sammy says: meow

Bello says: meow meow meow

Our complete code created so far:

As a result, our dog is now meowing :(

Sammy says: meow

Bello says: meow meow meow

This is of course frustrating for the dog and leads to dog depression in the long run. We want to prevent this.

Methods overwriting in object-oriented programming

We can override methods. If an inherited method does not fit, we can simply overwrite it in the child class. Our dog in the example should bark appropriately.

So we create a method in the dog class with the exact same name! Thus, this is executed when called and thus it overwrites the method of the parent class:

Our dog class:

If a conversation is held between dog and cat, it will proceed as usual:

With the result:

Sammy says: meow

Bello says: WAU WAU WAU

And the complete code:

Time to practice: Overwriting inheritance in the car and methods

Our class “car” from the last exercise should have another sibling class (not from which one inherits), the “truck”. All previously existing properties and methods are to be relocated to the “vehicle” parent class.

The car should also have this property.

The method “park ()” is to be overwritten for the truck. It should come as an output "parked on company yard".

The truck should have an additional method: recharge ()

Our previous code from the last solution, which should now be expanded.

Have fun converting.

Recommend • Social Bookmarks • Thank you