Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

abbiecoghlan
abbiecoghlan

Posted on • Edited on

     

Class Inheritance VS Modules in Ruby

In Ruby, we can use either class inheritance or modules to provide shared functionality throughout a program. This post explores the difference between class inheritance and modules, and considerations to help determine the appropriate use of each.

Class Inheritance

Inheritance between classes in Ruby allows for the creation of classes that have access to shared methods while still maintaining distinct, unique classes. When one class, known as the child or subclass, is inherited from another class, known as the parent or “super” class, it is given access to all of the methods of the parent. For example, we might have a parent class “Dog”, with a child class of “Dalmatian”. In the code below, we define these classes without the use of inheritance.

classDogdefbark"Woof!"enddefsleep"zzzzz"enddeffind_that_smell"sniff sniff sniff"endendclassDalmatiandefbark"Woof!"enddefsleep"zzzzz"enddeffind_that_smell"sniff sniff sniff"endend
Enter fullscreen modeExit fullscreen mode

The acronym DRY stands for "don't repeat yourself", and it is an important principle in software design. In the code below, using class inheritance, we can get the same functionality as above, but we have the benefit of keeping our code DRY. Our Dalmatian class now has access to all of the behavior of the Dog class.

classDogdefbark"Woof!"enddefsleep"zzzzz"enddeffind_that_smell"sniff sniff sniff"endendclassDalmatian<Dogend
Enter fullscreen modeExit fullscreen mode

The use of inheritance allows us to share methods between classes in a way that reflects the real-world hierarchical relationship between our classes, and provides shared functionality accordingly. If a dog can bark, a Dalmatian can bark.

An important note about class inheritance when considering use is that, although you can inherit from a class that inherits from another class that inherits from yet another class- a single class can only inherit fromone class at a time when it is defined.

Modules

Let's imagine we wanted to define an instance method that returns true if the animal is able to be housebroken. We could define this as an instance method within our dog class and have our individual dog breeds inherit the method. But what if our program had other animal classes, such as cats and rabbits, that we wanted to provide this method to as well?

classAnimalendclassDog<Animaldefcan_be_housebroken?trueendendclassCat<Animaldefcan_be_housebroken?trueendendclassRabbit<Animaldefcan_be_housebroken?trueendend
Enter fullscreen modeExit fullscreen mode

As you can see above, we would have to define those methods over and over again. Sometimes, as in this case, we might want to share functionality between classes that do not have a clear hierarchical arrangement. Instead of making a new class that is built from the blueprint of an existing class, like when we inherit, we may simply want to group methods together and make those methods available to a variety of other classes. Modules allow us to do just that.

Let’s use a module instead of class inheritance. First, we will define our module. Then, within the definition of our classes, we can use the keywordinclude along with the module name in any number of classes in which we would like to provide access to the functions we defined in our module.

moduleIndoorPetdefcan_be_housebroken?trueendendclassAnimalendclassDog<AnimalincludeIndoorPetendclassCat<AnimalincludeIndoorPetendclassRabbit<AnimalincludeIndoorPetend
Enter fullscreen modeExit fullscreen mode

As you can see, the use of Modules provides a great solution for extending functionality and avoiding repetitive code in situations where there is no clear parent-child relationship. Once it is defined, a module can be included in many classes. There is also no limit to how many modules you can include in an individual class. One thing to keep in mind about modules when considering use is that, unlike with classes, you cannot have aninstance of a module.

Class Inheritance vs Modules

Class inheritance and modules both allow us to extend functionality throughout our program while reducing repetitive code. When trying to figure out when to use modules vs class inheritance, some considerations include whether or not you need to create instances, whether or not there is a hierarchical relationship between classes, and how the extended functionality is related to the relationships between groups. If you are trying to build a class from the framework of an already defined class, you can use class inheritance to adopt the behaviors of a parent class. If you've created a group of methods that you’d like to use repeatedly throughout your program in many different classes that are not hierarchically related, you can use modules to provide your classes access to the methods defined within your modules.

In our next post, we dig deeper into modules in ruby, exploring theinclude,extend, andprepend keywords. You can read that posthere.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
niyanta_zamindar_63b4bdbc profile image
Niyanta Zamindar
  • Joined

HI, in the example under Modules, why haven't you kept the methodcan_be_housebroken inside the Animal class so that when the other classes inherit from it they get the method? Instead of creating a module for it. I am not clear with use case here.

CollapseExpand
 
prasoondavid profile image
Prasoon David
  • Education
    Birla Institute of Technology
  • Work
    Senior Software Engineer- Byjus
  • Joined

@Niyanta Interfaces or Module are used when there is no proper hierarchical relationship
Suppose you keep the method 'can_be_housebroken?' in Animal class,
and you create a childClass Lion from animal. Suppose Lion's house cant be broken , then the method 'can_be_housebroken?' will return true.

CollapseExpand
 
aruna profile image
aruna-x
Software Engineer. Lover of soft pillows and hard puzzles.
  • Education
    B.A. Philosophy, Mathematical Logic @ Harvard University
  • Joined

Clear and helpful. Thanks!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromabbiecoghlan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp