The Simple Pillars of OOP

Polymorphism does not have to be a scary word

Photo by Nong Vang on Unsplash
def make_fried_chicken
look_up_recipe
buy_ingredients
prep_chicken
preheat_oven
pan_fry
end
Figure 1: Abstraction of cooking fried chicken into a method
class Tylenol   def create_pill
measure_ingredients
mix_ingredients
seal_ingredients_in_capsule
end
private def measure_ingredients
...
end
def mix_ingredients
...
end
def seal_ingredients_in_capsule
...
end
endFigure 2: Encapsulation tylenol pill via private methods
class Animal
def eat
...
end
def sleep
...
end
end
class Dog < Animal
def speak
puts "bark bark"
end
end
Figure 3: Dog class inherited the methods from Animal
class Dog
attr_reader :name, :breed
def initialize(name:, breed:)
@name = name
@breed = breed
end
def speak
puts "bark bark"
end
end
dog_1 = Dog.new(name: "Rover", breed: "Golden Retriever")
dog_2 = Dog.new(name: "Hunter", breed: "Dachshund")
dog_1.name
# "Rover"
dog_2.name
# "Hunter"
Figure 4: Polymorphism of the Dog class via dog_1 and dog_2

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mathew Phillip Wheatley

I am a software engineer with a mechanical background. Interests swing from aerospace, to woodworking, travel, skiing, hiking, running, climbing, and lasers.