Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - INHERITANCE


Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.


Story :

God has created human beings. So, he has prepared the 'Human' class with states and behaviours. So that human beings could be created based on the stated behaviour.

Now let's say there is a supreme God(more powerful than the God who created Humans). He had decided how all living beings should behave(Human, Animal, Fish).

So, he created LivingBeing class.


class LivingBeing

	def breathe()
		puts "Breathes oxygen from air."
	end
end

Story :

Now, the Supreme God(i.e. The creator of the above 'LivingBeing' class) made this rule that all living beings, be it Human or Animal everyone should breathe oxygen from air.

So, the God who has created Human is not going to define the breathe() behaviour again. And he decided to reuse it.

Now, LivingBeing is the parent class and Human class is going to be the child class.


Let us see in the below example.


Example :



class LivingBeing  

  	def breathe()  
    	puts "Breathes oxygen from air."
  	end  
end  
  
class Human < LivingBeing  

	def initialize(name, food, language)
		@name = name
		@food = food
		@language = language
	end	

	def eat()
		puts "#{@name} eats #{@food}"
	end	

	def speak()
		puts "#{@name} speaks #{@language}"
	end	
  
end  
  
human1 = Human.new("John", "Burger", "English")

human1.eat()
human1.speak()

human1.breathe()


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from air.

So, in the above example, we have created a LivingBeing class that just has a Behaviour/Method i.e. breathe().

java_Collections

Then we have created the Human and the Human class should have the breathe() method from the LivingBeing class.


And since, the breathe() method is defined in the LivingBeing class. There is no point in redefining the breathe() method again.


Rather the Human class reuses it by inheriting all the members of the LivingBeing class.


class Human < LivingBeing

The syntax is quite simple. We just place the class to be inherited(i.e. LivingBeing) followed by <.

java_Collections

And what happens is, breathe() method of LivingBeing class becomes a part of the Humanclass.

java_Collections

And along with eat() and speak() method, breathe() method also becomes a part of Human class.


Now, after creating the human1 object,


human1 = Human.new("John", "Burger", "English")
java_Collections


Now when we call the breathe() method from human1 object.


human1.breathe()

We get the below output.


Breathes oxygen from air

Use of super() Method


The super() Method is used in the child class and is used to call a method of its Parent class.


We have seen the above example. Let us say the Parent class, LivingBeing has an initialize() method and we want to initialise its source attribute there.


Sounds Complex?


Let's simplify with the below example.


Example :



class LivingBeing
	def initialize(source)
		@source = source
	end	

	def breathe()
		puts "Breathes oxygen from #{@source}"
	end
end		

class Human < LivingBeing
	def initialize(name, food, language, source)
		super(source)
		@name = name
		@food = food
		@language = language
	end	

	def eat()
		puts "#{@name} eats #{@food}"
	end	

	def speak()
		puts "#{@name} speaks #{@language}"
	end	  
end  

human1 = Human.new("John", "Burger", "English", "Air")

human1.eat()
human1.speak()

human1.breathe()


Output :



  John eats Burger
  John speaks English
  Breathes oxygen from Air

So in the above example, we have an attribute named source and a method breathe() in the Parent class, LivingBeing.

java_Collections

class LivingBeing
	def initialize(source)
		@source = source
	end

	def breathe()
		puts "Breathes oxygen from #{@source}"
	end
end

And we wanted to initialise the source attribute in the initialize() constructor of LivingBeing.


But as we know, the initialize() constructor is only called at the time of Object creation. And we won't be creating any objects of LivingBeing.


And this is where super() Method comes to picture.


Let us see, how can we use the super() Method to call the initialize() constructor.


Then we have declared the Human class inheriting all the contents of the LivingBeing class.


class Human < LivingBeing
	def initialize(name, food, language, source)
		super(source)
		@name = name
		@food = food
		@language = language
	end

	def eat()
		puts "#{@name} eats #{@food}"
	end

	def speak()
		puts "#{@name} speaks #{@language}"
	end
end

Now, in the initialize() constructor of the Human class,


def initialize(name, food, language, source)
	super(source)
	@name = name
	@food = food
	@language = language
end

We have called the initialize() constructor of the HumanBeing class using the super() Method.


super(source)

When we write the super(), internally the initialize() constructor of the HumanBeing class gets called.


And the value of the attribute source gets set in the initialize() constructor of the Parent class HumanBeing.


def initialize(source)
	@source = source
end