The working of polymorphism and usage of super
In this guide, we are going to discuss another important concept in OOP called polymorphism. Polymorphism occurs when a class that inherits from a parent class overrides the behavior provided by the parent class.
To see a basic example, we'll continue working with our ApiConnector
class:
class ApiConnector def initialize(title:, description:, url: 'google.com') @title = title @description = description @url = url end def api_logger puts "API Connector starting..." end end
I've added a new method to our class called api_logger
.
Next, I'll create a class called PhoneConnector
that inherits from our ApiConnector
class. After we instantiate the class, it can call the api_logger
method that we created in the parent class:
class PhoneConnector < ApiConnector end phone = PhoneConnector.new(title: 'My Title', description: 'Some content') phone.api_logger
If you run this code in the Terminal, you will get the API Connector Starting...