Basing One Class on Another: a Demo : inheritance « Class « Ruby






Basing One Class on Another: a Demo


class Animal
  def initialize(color)
    @color = color
  end

  def get_color
    return @color
  end
end

class Dog < Animal
  def initialize(color, sound)
    super(color)
    @sound = sound
  end

  def get_sound

    return @sound
  end
end

dog = Dog.new("brown", "Bark")
puts "The new dog is " + dog.get_color
puts "The new dog says: " + dog.get_sound + "" + dog.get_sound

 








Related examples in the same category

1.Basing one class on another is called inheritance.
2.Extends class
3.how inheritance works in code form
4.different types of people
5.Structuring Your Pets Logically
6.If the class Name were in a different file, you would just require that file first
7.Add new constructor
8.Module and class hierarchy
9.Subclass Array class