Inheritance In Ruby On Rails

Today, we are going to learn some interesting concepts about inheritance in Ruby on Rails.

Introduction

Today, we are going to learn some interesting concepts about inheritance. You have learned this concept in other languages also. Here, you can see how it will be used and performed in the Ruby language.

Contents

  • Super Method

Inheritance

  • Inheritance is when a class receives or inherits the attributes and behavior of another class.
  • The class that is inheriting the behavior is called the subclass (or derived class) and the class it inherits from is called the superclass (or base class).
  • Imagine several classes - Cat, Dog, Rabbit, and so on.
  • Although they may differ in some ways (like, only Dog might have the method bark), they are likely to be similar in others (all having color and name).
  • This similarity can be expressed by making them all inherit from a super class Animal, which contains the shared functionality.
  • The < symbol is used to inherit a class from another class.

Example

  1. Class Dog < Animal  
  2.     # some code  
  3. End  
  • In the code above, Dog is the subclass and Animal is the super class.
  • Now, let’s define the Animal and Dog classes.

Example

  1. Class Animal  
  2.     def initialize (name, color)  
  3.     @name = name  
  4.     @color = color  
  5.      End  
  6.      def speak  
  7.     Puts “Hi”  
  8.      End  
  9. End  
  10. Class Dog < Animal  
  11. End  
  • Dog is a subclass of Animal so it inherits Animal’s methods and attributes.

Example

  1. d = Dog.new (“Deva”, “Blue”)  
  2. d.speak  

Output

Hi

  • Now, Dog has all the methods and attributes of the Animal class, which is why we can initiate the object and all the speaking.

Example

  1. Class vehicle  
  2.     def make_sound  
  3.     Puts “Hi”  
  4.     End  
  5. End  
  6. Class car < vehicle  
  7. End  
  8. c = car.new  
  9. c.make_sound  
  • The subclass can also have its own methods and attributes.
  • Let’s define a Cat class and inherit it from the same Animal class.

Example

  1. Class Animal  
  2.     def initialize (name, color)  
  3.     @name = name  
  4.     @color = color  
  5.     End  
  6.     def speak  
  7.     Puts “Hi”  
  8.     End  
  9. End  
  10. Class Dog < Animal  
  11. End  
  12. Class cat < Animal  
  13.     attr_accessor:age  
  14.     def speak  
  15.     Puts “Meow”  
  16.     End  
  17. End  
  18. c = cat.new (“lucy”, “white”)  
  19. c.speak  

Output

Meow

  • In the code above, Cat inherits from Animal.
  • It has an instance variable age and also defines its own speak.
  • This is called method overloading because the speak method in Cat overrides or replaces the one in the Animal class.
  • When we called the speak method for our Dog object, its super class method was called because Dog did not override it.
  • The Cat object called its own speak method because it defined its own implementation.
  • Inheritance is a great way to remove duplication in your code by writing the shared and common functionality in the super class and then adding individual functionality in the sub class.
  • You can have multiple levels of inheritance, for example,

Example

  1. Class Animal  
  2. End  
  3. Class Mammal < Animal  
  4. End  
  5. Class Dog < Mammal  
  6. End  
  • Here, Dog inherits from Mammal, which inherits from Animal.
  • This can be described as an “is a” relationship because a Dog is a Mammal
  • This is an example of single inheritance with multiple levels of hierarchy.
  • However, Ruby does not support multiple inheritance, meaning you cannot inherit a class simultaneously from multiple classes (a class cannot have multiple super classes).

SUPER

  • Ruby has a built-in method called super, which is used to call methods from the super class.
  • When you call super in a method for the sub class, the method of the same name gets called from the super class.

Example

  1. Class Animal  
  2.     def speak  
  3.     Puts “Hi”  
  4.     End  
  5. End  
  6. Class cat < Animal  
  7.     def speak  
  8.     super  
  9.     Puts “Meow”  
  10.     End  
  11. End  
  • Super class the speak method of the Animal class.
  • Now, if we create an object of class cat and call its speak method, we will get the following:

Example

  1. c = cat.new  
  2. c.speak  

Output

Hi

Meow

  • The use of super allows us to remove duplicate code by using and extending the behavior of the super class in our super class.
  • Super is more commonly used in the initialize
  • For example, our super class has na initialize method that takes one argument and initializes an instance variable.

Example

  1. Class Animal  
  2.     def initialize (name)  
  3.     @name = name   
  4.     End  
  5. End  
  • Now, we need a sub class cat that also has an @age instance variable, and we need to define its own initialization
  • Instead of repeating ourselves, and setting the name instance variable in the cat class, we can use its super class with the super method as follows:

Example

  1. Class Cat < Animal  
  2.     def initialize (name, age)  
  3.     Super (name)  
  4.     @age = age   
  5.     End  
  6.     def to_s  
  7.     “#{@name} is #{@age} years old.”  
  8.     End  
  9. End  
  • We passed one of the arguments to the super method, which calls the initialize method of the Animal call and sets the @name instance variable.
  • Now we can instantiate an object and output its info:

Example

  1. c = Cat.new (“Bob”, 3)  
  2. Puts c  

Output

Bob is 3 years old

  • In the example we used super for a simple assignment.
  • Imagine having a complex program with complex calculations and operations being carried out.
  • That’s where the real benefits of “not repeating yourself” come in, and calling the super where applicable is one way of achieving it.

Example

  1. Class A  
  2.     def initialize (x)  
  3.     Puts x/2  
  4.         End  
  5. End  
  6. Class B < A  
  7.     def initialize (y)  
  8.     Super (y +2)  
  9.     End  
  10. End  
  11. ob = B.new (6)  

Output

4

CONCLUSION

I hope you understand. If you have any queryies please ask me anything. We'll see more in the future.