← homeProgramming (Програмування)

What is polymorphism? An example of using polymorphism in Ruby.

Polymorphism is a principle of object-oriented programming that allows objects of one class to use methods of another class. This can be achieved through special mechanisms such as method overriding or interfaces.In R...

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
Polymorphism is a principle of object-oriented programming that allows objects of one class to use methods of another class. This can be achieved through special mechanisms such as method overriding or interfaces.
In Ruby, polymorphism can be created by using a common method name for different classes.

Example of Using Polymorphism in Ruby

# Creating a Shape class with a draw method
class Shape
  def draw
    raise NotImplementedError, 'Subclasses must implement the draw method'
  end
end

# Creating a Circle class that inherits from Shape
class Circle < Shape
  def draw
    puts 'Drawing a circle'
  end
end

# Creating a Rectangle class that also inherits from Shape
class Rectangle < Shape
  def draw
    puts 'Drawing a rectangle'
  end
end

# Using polymorphism
circle = Circle.new
rectangle = Rectangle.new

# Calling the draw method for the circle
circle.draw

# Calling the draw method for the rectangle
rectangle.draw
In this example, both classes Circle and Rectangle inherit from the base class Shape and implement the draw method. When the draw method is called for objects of the Circle and Rectangle classes, the corresponding implementations of the method are invoked, which expresses polymorphism. It will also be useful to read about inheritance in ruby.

🔥 More posts

All posts
Programming (Програмування)Jun 2, '23 12:53

What does super do in Ruby?

In Ruby, the keyword "super" is used to call a parent method from a subclass. When you declare a ...

What is a function in programming?
Programming (Програмування)Jun 24, '24 18:15

What is a function in programming?

A function is the fundamental building block of programming that defines a set of instructions or...

What does .map(&:name) mean in Ruby?
Programming (Програмування)Jul 28, '24 11:18

What does .map(&:name) mean in Ruby?

In Ruby, the map(&amp;:name) construct is a shorthand for applying a method to each element of a ...

ZOMBIE in Ruby. What is it?
Programming (Програмування)May 3, '24 12:41

ZOMBIE in Ruby. What is it?

Ruby is a programming language. Everything is clear here. In the code of this language, you may e...

Programming (Програмування)Dec 9, '23 12:15

What is best practice in programming?

Best practice in programming refers to recommendations and methods that are considered optimal or...

Programming (Програмування)Dec 7, '23 07:42

What is encapsulation in OOP?

Encapsulation is one of the fundamental principles of object-oriented programming (OOP). This pri...

Programming (Програмування)Dec 18, '23 08:25

What is DNS? What is DNS used for?

DNS, or Domain Name System, is a system that converts human-readable domain names (for example, t...

Programming (Програмування)Dec 18, '23 08:44

What is a domain (domain name)?

Domain (domain name) - is a readable textual logical identifier used to identify a resource on th...