This will be the first time I attempt to explain a concept that is still kind of new (and confusing) to me. In doing so, my hopes is that I will further reinforce my knowledge of the subject. Since Learn labs are based on TDD, I feel as though I've gotten pretty good at reading error messages; I'm able to figure out what I need to do to pass a lab. But my goal is not to just pass them. I want to UNDERSTAND. So here we go!
By the way, feel free to correct me if I'm wrong in my topic summaries, or if you want to add to them! I'll gladly accept feedback!
What is inheritance and why should we care?
When I think about inheritance in the real world, I morbidly think about the parents of some wealthy family tragically dying, leaving all of their riches behind. Ok ok, I know we don't have to get that dark but let's carry on! Think about a scenario in which children will inherit their deceased parents' wealth. #RIP Then think about those families, or children, that fight over their inheritance, instead of sharing them. Heaven forbid their parents' didn't leave a will! These situations literally tear families apart! So this is obviously a big deal!
Well in Ruby, inheritance kind of works in the same way, (no dead parents necessary - score!). But you could think about the parents' wealthy possessions as being Classes (superclasses) filled with very valuable resources (methods). As its child (subclass), he/she COULD keep on working his/her way up in life (writing similar methods over and over again, that can be found in the superclass), but why do all that work when the parents left behind a gold mine? By the way, this is just a scenario; I highly value working hard for your goals!
This is where one type of inheritance comes into play! In a hierarchal situation as described above, the child could easily obtain the inheritance, as stated in the will, which will save him/her a lot of extra work in life! This is how it would look like:
class Child < DeadWealthyParents
"class Child" is the subclass here, and it will inherit all the attributes, behaviors, and instances of the class "DeadWealthyParents", as described in their will "<". What's great about this too is let's say there are multiple children. Each child can share the inheritance, no problem! Thank goodness! Each child can be unique within its own, but they will also have access to the methods of its superclass! That subclass can also override any of the inheritance it doesn't like by defining it within its own class. Win-win. You could even use a keyword "super" within a method to refer to its respective method in the super class, whilst adding specific adjustments needed for your subclass.
What if this referenced wealthy family didn't have any children? Could they still divvy out their wealth... perhaps to charities?
This is when Modules come into play. Modules are similar to Classes because it's like a container full of useful goods (methods), that can be shared even though they aren't related hierarchically. You would have to define DeadWealthyParents as a module on its own first, requiring the charities you want to loan out to. Then you could divvy out its behaviors (methods) using the keywords "include" or "extend" within the class Charity. This bit is still a little fuzzy for me, but I believe we could use "include" if we want to borrow instance methods from the module, whereas "extend" would loan out class methods.
include DeadWealthyParents # adds methods to an instance of a class.
extend DeadWealthyParents # adds class methods to the class itself.
Ok, I think that's all for now. I admit that I still need to practice these concepts so bare with me until then. Hopefully I'm on the right track as I redo all of the Object Orientation labs for better understanding.