Learn Ruby the Hard Way: What is Inheritance?

Inheritance is basically one class that takes features from a parent class like:

class Bear < Animals.

so every thing I do in the instance of the Bear class, would  also work as if they were done to an instance of Animals. It lets us do common functionalities in the Animals class and be more specific in the Bear class.

 

  1. Actions on the child imply an action on the parent.
  2. Actions on the child override the action on the parent.
  3. Actions on the child alter the action on the parent.

 

 

Implicit Inheritance

 

class Parent

def implicit()
 puts "Parent implicit()"
 end 
end

class Child < Parent
end

dad = Parent.new()
son = Child.new()

dad.implicit()
son.implicit()

allthough Child has no code in it’s function, it’s inherits all the functionality from it’s parent class, and therefor runs the def implicit() function.

 

class Parent

def override()
 puts "Parent override()"
 end 
end

class Child < Parent
 def override()
 puts "Child override()"
 end
end

dad = Parent.new()
son = Child.new()

dad.override()
son.override()

what this does is overriding the function from the parent class. Firstly we call the function that’s inside the parent class. But the second time we call the function inside the Child class. The old function gets overridden and the new one is called. Child is defining the def override() function new.

 

class Parent

def altered()
 puts "Parent altered()"
 end 
end

class Child < Parent
 def altered()
 puts "Child, before Parent altered()"
 super()
 puts "Child, after parent altered()"
 end
end

dad = Parent.new()
son = Child.new()

dad.altered()
son.altered()

The Child class gets a super() built-in Ruby function. What is does is jump back to the parent function again and skip the overridden function for a moment. So we get:

>Parent altered()
>Child, before Parent altered()
>Parent altered()
>Child, after parent altered()

 

 

Composition

 

class Other

def override()
 puts "Other override()"
end

def implicit()
 puts "Other implicit()"
end

def altered()
 puts "Other altered()"
 end
end


class Child

def initialize()
 @other = Other.new()
end

def implicit()
 @other.implicit()
end

def override()
 puts "Child override()"
end

def altered()
 puts "Child, Before Other altered()"
 @other.altered()
 puts "Child, After Other altered()"
 end
end

son = Child.new()

son.implicit()
son.override()
son.altered()

 

To do what I did above with Inheritance, I can also use other classes and modules, rather then rely on implicit Ihneritance. You can call functions that are inside of other functions.

 

 

module Other

def override()
 puts "Other override()"
end

def implicit()
 puts "Other implicit()"
end

def altered()
 puts "Other altered()"
 end
end


class Child 
 include Other


 def override()
 puts "Child override()"
 end

def altered()
 puts "Child, Before Other altered()"
 @other.altered()
 puts "Child, After Other altered()"
 end
end

son = Child.new()

son.implicit()
son.override()
son.altered()

Using modules and a require/include function that are doing the same thing as above.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s