0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】抽象クラスを作る

Posted at

##環境
Ruby 3.0.2
Rails 6.1.4.1

##抽象クラスとは

抽象クラスは、その特徴としてクラス自身はオブジェクトの実体、インスタンスを生成できない。
抽象クラスは主にそのクラスを親クラス(基本クラス)とした子クラス(派生クラス)を定義(継承)する前提で作製されるクラス。

Rubyではサポートされてないけどそれっぽいものを作れる。

class Parent
  def method_1
    raise 'implement this method in subclass'
  end
end

class Child < Parent
  def method_1
    'hogehoge'
  end
end
parent_class = Parent.new
parent_class.method_1
# => RuntimeError: implement this method in subclass

child_class = Child.new
child_class.method_1
# => 'hogehoge'

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?