CrystalLang-Polymorphism

GeekNationGeekNation
3 min read

In Crystal, one does not have anything like the variable types as there is in other programming languages. Every variable is an “object” which can be individually modified. One can easily add methods and functions on every object. So here, the Object Oriented Programming plays a major role. There are many pillars of Object Oriented Programming in every other programming language, like Inheritance, Encapsulation etc. One of these pillars is Polymorphism.

Polymorphism is a made up of two words Poly which means Many and Morph which means Forms. So Polymorphism is a method where one is able to execute the same method using different objects. In polymorphism, we can obtain different results using the same function by passing different input objects. One can also write the If-Else commands but that just makes the code more lengthy. To avoid this, the programmers came up with the concept of polymorphism.

In Polymorphism, classes have different functionality but they share common interference. The concept of polymorphism can be studied under few sub categories.

Polymorphism using Inheritance

Polymorphism using Duck-Typing

Polymorphism using inheritance

Inheritance is a property where a child class inherits the properties and methods of a parent class. One can easily implement polymorphism using inheritance. It can be explained using the following example

# Crystal program of Polymorphism using inheritance 
class Vehicle 
    def tyreType 
        puts "Heavy Car"
    end
end

# Using inheritance 
class Car < Vehicle 
    def tyreType 
        puts "Small Car"
    end
end

# Using inheritance 
class Truck < Vehicle 
    def tyreType 
        puts "Big Car"
    end
end

# Creating object 
vehicle = Vehicle.new
vehicle.tyreType() 

# Creating different object calling same function 
vehicle = Car.new
vehicle.tyreType() 

# Creating different object calling same function 
vehicle = Truck.new
vehicle.tyreType()

Output:

Heavy Car

Small Car

Big Car

The above code is a very simple way of executing basic polymorphism. Here, the tyreType method is called using different objects like Car and Truck. The Car and Truck classes both are the child classes of Vehicle. They both inherit the methods of vehicle class (primarily the tyretype method).

Polymorphism using Duck-Typing

In Crystal, we focus on the object’s capabilities and features rather than its class. So, Duck Typing is nothing but working on the idea of what an object can do rather than what it actually is. Or, what operations could be performed on the object rather than the class of the object. Here is a small program to represent the before mentioned process. Example :

# Crystal program of polymorphism using Duck typing 

# Creating three different classes 
class Hotel 

def enters 
    puts "A customer enters"
end

def type(customer) 
    customer.type 
end

def room(customer) 
    customer.room 
end

end

# Creating class with two methods 
class Single 

def type 
    puts "Room is on the fourth floor."
end

def room 
    puts "Per night stay is 5 thousand"
end

end


class Couple 

# Same methods as in class single 
def type 
    puts "Room is on the second floor"
end

def room 
    puts "Per night stay is 8 thousand"
end

end

# Creating Object 
# Performing polymorphism 
hotel= Hotel.new
puts "This visitor is Single."
customer = Single.new
hotel.type(customer) 
hotel.room(customer) 


puts "The visitors are a couple."
customer = Couple.new
hotel.type(customer) 
hotel.room(customer)

Output :

This visitor is Single.

Room is on the fourth floor.

Per night stay is 5 thousand

The visitors are a couple.

Room is on the second floor

Per night stay is 8 thousand

In the above example, The customer object plays a role in working with the properties of the customer such as its “type” and its “room”. This is an example of polymorphism.

source: geeksforgeeks

code is tested on

https://play.crystal-lang.org

0
Subscribe to my newsletter

Read articles from GeekNation directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

GeekNation
GeekNation