In the subclass, we can modify the object behavior by reloading the parent class method.
Ruby> class Human
| Def identify
| Print "I'm a person. \ n"
| End
| Def train_toll (age)
| If age <12
| Print "CED fare. \ n ";
| Else
| Print "Normal fare. \ n ";
| End
| End
| End
Nil
Ruby> Human. new. identify
I'm a person.
Nil
Ruby> class Student1 <Human
| Def identify
| Print "I'm a student. \ n"
| End
| End
Nil
Ruby> Student1.new. identify
I'm a student.
Nil
If we only want to enhance the identify method of the parent class instead of Completely replacing it, we can use super.
Ruby> class Student2 <Human
| Def identify
| Super
| Print "I'm a student too. \ n"
| End
| End
Nil
Ruby> Student2.new. identify
I'm a human.
I'm a student too.
Nil
Super can also let us pass parameters to the original method. There are sometimes two types of people here...
Ruby> class Dishonest <Human
| Def train_toll (age)
| Super (11) # we want a cheap fare.
| End
| End
Nil
Ruby> Dishonest. new. train_toll (25)
Reduced fare.
Nil
Ruby> class Honest <Human
| Def train_toll (age)
| Super (age) # pass the argument we were given
| End
| End
Nil
Ruby> Honest. new. train_toll (25)
Normal fare.
Nil