DEV Community

jaredsurya
jaredsurya

Posted on

Object Inheritance in Ruby

Introduction

In real life, different categories or groups
(like people, reptiles, buildings, etc.) are related in all sorts of ways. Within a single group, or class, there are many ways things can be classified. For example, the "people" category or class includes garbage men, shop owners, scientists, etc. Each class shares common features because they are all people. Even though they are different, they all have specialized and unique traits too.

In coding, we know we always want to cut out repetitious sequences from our code. Object inheritance in Ruby allows us to simplify our code and keep repititious 'code smell' down. We see the similarities between related classes and we abstract out the common methods and behaviors into a parent class.

Basics of Inheritance

Inheritance is one of the most important features in an object-oriented language.
It is when a class inherits behavior from another class. If a programmer wants to create a new class and there is a class that already includes some of the code that the programmer wants, then they can derive a new class from the existing class. The class that is passing along behavior is called the superclass and the class that inherits it is called the subclass. The use of inheritance allows us to create a family of classes with shared behavior, while still differentiating those classes. Using this system reuses existing code and allows new classes (sub-classes) to need less new code written.

Implementing class inheritance creates a family of classes with shared behavior and differentiation. Code that would have been used repetitively gets abstracted and simplified. The new child or subclass adopts attributes and behaviors that are already contained in the parent class. (superclass)

Key terms in Inheritance:

  • Super-class: Classes whose characteristics (methods and behaviors) are inheritable are known as "superclass" (aka. base class or parent class).
  • Sub-class: The class which is derived from another class is known as a subclass or derived class or child class. They are sometimes written with their own objects and methods in addition to the superclass methods and objects, etc.

Examples:

Defining the Superclass:

Below we have written a class called Bear. It has many methods, all of which can be passed down and available to its children, aka. subclass. The superclass normally provides the bulk of key code content, that which is inheritable by the children (aka. subclasses).

class Bear

  attr_accessor :coat_color, :favorite_food

  def initialize(coat_color, favorite_food)
    @coat_color = coat_color
    @favorite_food = favorite_food
  end

  def speak
    "Grrrowlll!"
  end

  def feed
    "om nom nom!"
  end

end
Enter fullscreen mode Exit fullscreen mode

Here we see our Bear super-class. It is initialized with coat_color and a favorite_food. Also present are instance methods, such as speak and feed. Each of these methods will be available to any children, inheriters of this Bear super-class.

Subclass:

The subclass is the class which has been derived from the super's code and which extends the usability of that code. (i.e. class methods, instance methods, etc.)
In the code block below we see Bear's class characteristics being inherited by Panda. We also see the general syntax for inheriting , which is the < placed between the sub-class and the super-class. Panda now inherits all of the methods and behavior of the parent class. It can now use any of the methods found inside of the Bear class and can even have some Panda-specific ones of its own.

class Panda < Bear

  def speak
    "Grrr..."

end
Enter fullscreen mode Exit fullscreen mode

Because Panda is a child of Bear, all of the methods defined within Bear are available for Panda to use. All of the methods of the parent are inherited by the child. For example, Panda.feed returns "om nom nom!" Also, a panda could be initialized with a coat_color and favorite_food.

Overwriting Inherited Methods

One feature that differs between the parent class and child class here is how in the parent Bear class the speak method returns "Grrrowlll!" and the child Panda class has it's own instance method called speak which overwrites its parent. When speak is called on the Panda class, "Grrr..." is returned instead of "Grrrowlll!". This illustrates how the subclass can overwrite methods of it's superclass.

Another example of this would be if we had a superclass of Human the greet instance method might say "Hello!" whereas a member of the Cowboy class might have a greet instance method overwriting that of it's parent Human class. The greet method of the Cowboy subclass might return "HOWDY!!" instead of "Hello!".

Conclusion

Because of ruby's object orientation and subsequent object inheritance, cleaner less repetitive code can be produced. Also, using the principles of inheritance, we can define generic Ruby classes which can have greater reusability. Smaller subclasses result in more detailed behaviors. With object inheritance we can also code multiple, non-repetitive methods, reducing the total amount of code needed, cleaning and optimizing the code that's useful to us.

Top comments (0)