Creating an Abstract Class and Method : abstract class « Class « Ruby






Creating an Abstract Class and Method


class Shape2D
  def initialize
    raise NotImplementedError.
      new("#{self.class.name} is an abstract class.")
  end
  def area
    raise NotImplementedError.
      new("#{self.class.name}#area is an abstract method.")
  end
end


class Square < Shape2D
  def initialize(length)
    @length = length
  end

  def area
    @length ** 2
  end
end

Square.new(10).area                              # => 100

# NotImplementedError: Shape2D is an abstract class.

 








Related examples in the same category

1.Add method to class to create abstract method dynamically