Polymorphism/Polymorphic Associations
Polymorphism / Polymorphic Associations
Polymorphism
Inheritance
class GenericParser
def parse
raise NotImplementedError, 'No method'
end
end
# inheritance 1
class JsonParser < GenericParser
def parse
puts 'JsonParser instance received a message.'
end
end
# inheritance 2
class XmlParser < GenericParser
def parse
puts 'XmlParser instance received a message.'
end
end
puts 'Using the XmlParser'
xml_parser = XmlParser.new
xml_parser.parse
puts 'Using the JsonParser'
json_parser = JsonParser.new
json_parser.parse
# Using the XmlParser
# XmlParser instance received a message.
# Using the JsonParser
# JsonParser instance received a message.Duck Typing
Decorator
Polymorphic Associations
Last updated