I finally had my Ah-ha moment when it came to classes in OOP. I’ve been struggling to get my mind around the idea. Now that I’m starting to get it I was wondering what the purpose of extending a class is. Would it not be simpler to just add another method into the class or am I just missing the point?
Share
If you want to add functionality to a class you add a method. However, there are situations that you want to add functionality, but cannot do that in a base class.
The best is show with a example. When adding a method
Radiusto a class makes only sense if the class is aCircle, aRecthas no radius. Adding a methodRadiusto the base class also makes no sense, because theRectclass then also gets a methodRadius.Example: