The final major concept that make the "four pillars" of Object Oriented Programming, is Polymorphism. In order to fully understand polymorphism, I recommend being comfortable with the other 3 pillars (encapsulation, abstraction and inheritance), first.
Just like the Fresh Prince said, "Parents just don't understand". Just because child classes (aka sub-classes) inherit properties and behaviors from their parent classes (aka super-classes), that doesn't mean they do things the exact same way. I mean, you don't act exactly like your parents. do you? (it's okay if you do).
the point is, while parents and children may have many of the same attributes and behaviors, they don't have to be one for one exactly. And, especially when it comes to behaviors, children don't always perform those behaviors the same way.
Every Dog Has His Day
As an example, lets make some classes about different dogs.
Here is our dog class:
class Dog def initialize(breed) @breed = breed @number_of_legs = 4 end def bark puts "Bark, Bark" end end
Let's also say that our program is going to let us differentiate between small dogs and big dogs, and so we will need a class for both small dogs and big dogs.
Since we already have a Dog class, we can have Small Dog and Big Dog inherit from the Dog super class. This makes sense because a Small Dog is a Dog, and a Big Dog is also still a Dog.
class SmallDog < Dog end class BigDog < Dog end
Sweet, now SmallDog and BigDog can be initialized with a breed, they both will have a number_of_legs set to 4, and they both have a bark method. Thanks inheritance!
The problem is, though, we want to make sure there's a difference between small dogs and big dogs. Right now, they both do everything exactly the same as their superclass (a generic dog), and therefore exactly the same as each other.
Specifically, it make more sense for small dogs to say "yap, yap" and big dogs to say "ruff, ruff" when they bark.
By invoking the OOP principle of polymorphism, we can do that!
class SmallDog < Dog def bark puts "yap, yap" end end class BigDog < Dog def bark puts "RUFF, RUFF" end end
By giving our subclasses a method with the same name as its super class, our program will prefer to use the subclass implementation when the method is called (on an instance of that subclass). This is called method overriding and is they key to polymorphism.
What that means is that, now,
Dog.bark will print "Bark, Bark",
SmallDog.bark will print out "yap, yap" and
BigDog.bark will output "RUFF, RUFF".
All dogs bark, but the way they bark can change depending on the exact class of Dog (aka subclass)
All the Pillars
With that, we have gone over all 4 of the major concepts in Object Oriented Programming (also known as the 4 pillars of OOP).
As a quick recap:
- Encapsulation - bunding properties and behaviors into an object/class
- Abstraction - Ignoring unnecessary details and properties
- Inheritance - Using super and sub classes to share behaviors and properties with related classes
- Polymorphism - letting sub classes implement their methods differently than their super class
This concludes the series on OOP concepts. If you have any comments or questions, feel free to respond below!
Thanks, see you next time.
Top comments (0)