Know how to inherit
Published: 2022-08-02 21:45:35

Objects know how to inherit

Back end consists of a main language being Ruby. There are many different elements but one of the main ones is inheritance. With that being said it is as it sounds in the real world to inherit from a parent in this case in Ruby a class can inherit the behaviors or other classes. 

To give you some insight let’s start from the beginning, A class is a blueprint from which objects are created. The object is also called as an instance of a class. For Example, the animal is a class and mammals, birds, fish, reptiles, and amphibians are the instances of the class.
one can easily create classes and objects. Simply write class keyword followed by the name of the class. The first letter of the class name should be in capital letter.

As we know an object is what inherits from a class which is the default root of all Ruby objects. Object inherits from Basic Object which allows creating alternate object hierarchies. Methods on Object are available to all classes unless explicitly overridden. This means they adopt all of the attributes and behaviors of the parent also known as the Super class. 



Syntax:  
house       <       estate

class House

  attr_accessor :rooms, :bathrooms

  def initialize(rooms, :bathrooms)
    @rooms = rooms
    @bathrooms = bathrooms
  end

  def water
    "pay water bill"
  end

  def laundry
    "fold laundry
  end

end

as seen in the image above the objects being inherited are being called with first the inherit symbol as they are in the above code you can see that the @ symbol are instance variables that are being inherited from a different class. 

Inheritance can be a great way to remove duplication in your code base. With inheratnce we get Super Ruby provides us with the super keyword to call methods earlier in the method lookup path. When you call super from within a method, it searches the method lookup path for a method with the same name, then invokes it. An example of how this works is shown below. 

class Animal
  def speak
    "Hello!"
  end
end

class GoodDog < Animal
  def speak
    super + " from GoodDog class"
  end
end

sparky = GoodDog.new
sparky.speak        # => "Hello! from GoodDog class"

We use super to invoke the speak method from the superclass, Animal, and then we extend the functionality by appending some text to the return value.

Another more common way of using super is with initialize.

class Animal
  attr_accessor :name

  def initialize(name)
    @name = name
  end
end

class GoodDog < Animal
  def initialize(color)
    super
    @color = color
  end
end
bruno = GoodDog.new("brown")

But something to know is Ruby itself uses inheritance to enable methods like:

1.	puts
2.	class
3.	super - The main benefit of using super is being able to use similar actions within a method with many classes while still adding unique actions to the sub-class method.

Other types of inheritance are 

•	Single inheritance.
•	Multi-level inheritance.
•	Multiple inheritance.
•	Multipath inheritance.
•	Hierarchical Inheritance.
•	Hybrid Inheritance.

Single inheritance –

In this inheritance, a derived class is created from a single base class.
In the given example, Class A is the parent class and Class B is the child class since Class B inherits the features and behavior of the parent class A.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66we80qydhh4ywhbshsz.png)
  

Multi-level inheritance
In this inheritance, a derived class is created from another derived class.
In the given example, class c inherits the properties and behavior of class B and class B inherits the properties and behavior of class B. So, here A is the parent class of B and class B is the parent class of C. So, here class C implicitly inherits the properties and behavior of class A along with Class B i.e there is a multilevel of inheritance.
 


Multiple inheritance
In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F#, etc., and Java Language.
In the given example, class c inherits the properties and behavior of class B and class A at the same level. So, here A and Class B both are the parent classes for Class C.
 

Multipath inheritance
In this inheritance, a derived class is created from other derived classes and the same base class of other derived classes. This inheritance is not supported by .NET Languages like C#, F#, etc.
In the given example, class D inherits the properties and behavior of class C and class B as well as Class A. Both class C and class B inherit the Class A. So, Class A is the parent for Class B and Class C as well as Class D. So it's making it a Multipath inheritance.


Hierarchical Inheritance
In this inheritance, more than one derived class is created from a single base class and further child classes act as parent classes for more than one child class.
In the given example, class A has two children class B and class D. Further, class B and class C both are having two children - class D and E; class F and G respectively.
 





Hybrid inheritance
This is a combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance Hierarchical and Multipath inheritance, or Hierarchical, Multilevel and Multiple inheritances.

Since .NET Languages like C#, F#, etc. do not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritances is not supported by .NET Languages.
 


This is because all Ruby objects inherit from the Object class by default. With that being said I hope that you are able to understand the importance and basics of inheritance and how it is used and why. 

 In conclusion the advantage of inheriting is 

1.	Reduce code redundancy.
2.	Provides better code reusability’s.
3.	Reduces source code size and improves code readability
4.	The code is easy to manage and divided into parent and child classes.
5.	Supports code extensibility by overriding the base class functionality within child classes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4u2aj95hk3okhs20elib.png)