Inheritance is to reuse the Behaviors/methods and properties/state of a Parent class in child class.
So, he created LivingBeing class.
class LivingBeing def breathe() puts "Breathes oxygen from air." end end
Now, LivingBeing is the parent class and Human class is going to be the child class.
Let us see in the below 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()
So, in the above example, we have created a LivingBeing class that just has a Behaviour/Method i.e. breathe().
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 <.
And what happens is, breathe() method of LivingBeing class becomes a part of the Humanclass.
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")
Now when we call the breathe() method from human1 object.
human1.breathe()
We get the below output.
Breathes oxygen from air
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.
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()
So in the above example, we have an attribute named source and a method breathe() in the Parent class, LivingBeing.
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