Overriding Methods Demo : Overriding Methods « Class « Ruby






Overriding Methods Demo


# When you define a method in a class, you can redefine that method in a derived class, 
# and that's what overriding is all about - redefining a method in a derived class.

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

  def get_color
    return @color
  end
end

class Dog < Animal
  def initialize(color)

    super(color)
  end

  def get_color
    return "blue"
  end
end

dog = Dog.new("brown")
puts "The new dog is " + dog.get_color

 








Related examples in the same category

1.Overriding Existing Methods
2.override your own methods
3.Override method from parent