Welcome back! We're continuing our journey into object-oriented programming (OOP) with a new and exciting topic: Polymorphism. You've already learned about classes, objects, and inheritance, which are essential building blocks of OOP. Now, it's time to explore how polymorphism can make your code more flexible and reusable.
Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. This provides a way to use a single interface to represent different types of objects. Here’s what we’ll cover in this lesson:
- Understanding Polymorphism: We'll discuss what polymorphism is and why it's a powerful concept in
Ruby
programming. - Implementing Polymorphism in
Ruby
: You'll learn how to use method overriding and duck typing to achieve polymorphism.
Polymorphism in Ruby
allows you to call methods of derived classes through a base class reference. This makes your code more dynamic and general. For example, consider the following Ruby
code:
Ruby1# Define a base class Person with name and age attributes 2class Person 3 def initialize(name, age) 4 @name = name 5 @age = age 6 end 7 8 # Define a display method, which can be overridden by derived classes 9 def display 10 puts "Name: #{@name}, Age: #{@age}" 11 end 12end 13 14# Define a derived class Student with a major attribute 15class Student < Person 16 def initialize(name, age, major) 17 super(name, age) 18 @major = major 19 end 20 21 # Override the display method to include the major attribute 22 def display 23 super 24 puts "Major: #{@major}" 25 end 26end 27 28# Define a derived class Teacher with a subject attribute 29class Teacher < Person 30 def initialize(name, age, subject) 31 super(name, age) 32 @subject = subject 33 end 34 35 # Override the display method to include the subject attribute 36 def display 37 super 38 puts "Subject: #{@subject}" 39 end 40end 41 42# Creating instances of Student and Teacher 43person1 = Student.new("Alice", 30, "Computer Science") 44person2 = Teacher.new("Bob", 25, "Mathematics") 45 46# Calling the display method on both instances 47person1.display 48person2.display
In this example, the display
method of the derived classes (Student
and Teacher
) overrides the base class method in Person
. When we call display
on an instance of Student
or Teacher
, the Ruby
interpreter automatically invokes the appropriate derived class method.
Let's also understand how Ruby resolves methods. Ruby uses a method lookup chain to resolve which method to invoke. When person1.display
is called, Ruby first checks if the display
method exists in the Student
class. If found, it executes it. If not, it moves up to the Person
class.
Polymorphism is crucial because it introduces flexibility and scalability to your code:
- Code Flexibility: Polymorphism allows you to write functions that can operate on objects of different types through a common interface.
- Reusability: You can extend and reuse your code more efficiently by leveraging polymorphism.
- Simplified Code Management: Polymorphism helps you manage and understand your code better, as similar operations are handled in a unified manner.
Are you excited to dive into the practice section and apply polymorphism to your programs? Let's get started and see the power of this concept in action!