Chapter 6 Inheritance

Inheritance permits you to create a class that is a specialization of another class. Classes lower down the hierarchy can inherit the features and functionalities of those higher up. Also, lower classes can add features of their own.

Ruby offers simple single inheritance. A single inheritance implementation permits a Ruby class to have one direct parent or class.

Example:

class Bird
    def fly
        puts "i can fly." 
    end
end

class Hawk < Bird
    def eat
        puts "worms" 
    end
end

black = Hawk.new
black.fly

  • Currently 3.08/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
  Flag Inappropriate Content 0 comments