DEV Community

ericksong91
ericksong91

Posted on

Object Inheritance Basics Review (Ruby)

Introduction

Object inheritance means that a child (or sub) class will inherit all the methods and attributes of a parent (or super) class. This allows the user to have some reusability of code and reduces redundancy.

For example, if we have a Car, Airplane and Boat class, they would share a lot of common attributes such as name, manufacturer, fuel type etc. If we suddenly had to change one of these attributes, we would have to change it for all three classes!

We can circumvent this problem by make a fourth class, Vehicle, that could encompass a lot of the common attributes and methods for each vehicle type. This would reduce the amount of code and changes would be a lot less time consuming.

Coding Example: Book and Textbook Class

Lets say we have a Book class:

class Book

 attr_accessor :title, :author, :pages

 def initialize(title, author, pages)
  @title = title
  @author = author
  @pages = pages
 end

 def flip
  "Flipping through the pages!"
 end

 def cost
   "I got this cool book for free!"
 end
end
Enter fullscreen mode Exit fullscreen mode

Now lets also include a Textbook class:

class Textbook
 attr_accessor :title, :author, :pages

 def initialize(title, author, pages)
  @title = title
  @author = author
  @pages = pages
 end

 def subject
   "Subject: Math"
 end
end
Enter fullscreen mode Exit fullscreen mode

You can already see that Book and Textbook share the title, author and pages attribute. You also want to be able to flip through both books. The only difference is that the Textbook class teaches a subject, Math.

If we instead change the Textbook class into a subclass or child class of Book, we can save ourselves some code. We use the < symbol to inherit the class:

class Textbook < Book
 def subject
   "Subject: Math"
 end
end
Enter fullscreen mode Exit fullscreen mode

Now lets initialize a new instance with both classes:

new_book = Book.new("Hello!", "John Smith", 60)
new_textbook = Textbook.new("Calculus", "Mister Math", 450)

puts new_book.title
 => Hello!

puts new_book.author
 => John Smith

puts new_book.pages
 => 60

puts new_book.flip
 => Flipping through the pages!

puts new_book.cost
 => I got this cool book for free!

puts new_textbook.title
 => Calculus

puts new_textbook.author
 => Mister Math

puts new_textbook.pages
 => 450

puts new_textbook.flip
 => Flipping through the pages!

puts new_textbook.subject
 => I got this cool book for free!
Enter fullscreen mode Exit fullscreen mode

Now the issue we're running into is that textbooks usually don't cost nothing! Fortunately, we can supersede the method cost from Book with our own cost in Textbook:

class Textbook < Book
 def subject
   "Subject: Math"
 end

 def cost
   "I got this used for $300!"
 end
end
Enter fullscreen mode Exit fullscreen mode

Now calling the method cost on Textbook will result in:

puts new_textbook.cost
 => I got this used for $300!
Enter fullscreen mode Exit fullscreen mode

When Ruby executes the code, it first looks at the class of the instance and looks for the method. If it cannot find the method, it will go up to the parent class and look for that method there.

Conclusion

Object inheritance is a useful and key component to how classes work in Ruby. It helps improve reusability of code and allows for modular design which improves flexibility and saves time.

Notes

Please let me know in the comments if I've made any errors. Still new to Ruby but would like to know your thoughts :)

Credits and Additional Resources

Ruby Class
How Does Inheritance work in Ruby? - Tutorials Point
Object Inheritance - Geeks for Geeks

Top comments (0)