BETA

About the concept of Polymorphism

投稿日:2019-07-10
最終更新:2019-07-13

Polymorphism is one of the core concepts of object-oriented programing(OOP). It might sounds daunting, but nothing to worry. it's just a long word for a very simple concept.

First of all, Polymorphism means “many shapes” in Greek. Please keep it in mind. It could be of help later.

Kindly ignore the syntax and pay no attention to precise technical points. It's about a concept.

When to use

We use Polymorphism when you want each instance to behave in its own way though they call a common method.

How to define

Just put a method which contains nothing in a class so that you can make every instance generated from the class act in its own way.

Example

class Car  
  name =   

  def run  

  end  
end  

Ferrari instance which is generated form the Car class run 200km/h.
Toyota Corolla instance which is also generated from the Car class run 60km/h.

To simply put, the concept of Polymorphism is that "same name, different befaviour" .

Another example

class Animal  
  name =  

  def makeSounds  

  end  
end  

Cat instance which is generated from the Animal class makeSounds ”meow”.
Bird instance which is also generated from the Animal class makeSounds “chirp".

So polymorphism is the ability of an object to take on many shapes and to call a common method which differs depending on its own shape.

I think now you got it.

Hope this helps and makes it clear.
Thanks.

技術ブログをはじめよう Qrunch(クランチ)は、プログラマの技術アプトプットに特化したブログサービスです
駆け出しエンジニアからエキスパートまで全ての方々のアウトプットを歓迎しております!
or 外部アカウントで 登録 / ログイン する
クランチについてもっと詳しく

この記事が掲載されているブログ

@fGeIKkJv88kaARfaの技術ブログ

よく一緒に読まれる記事

0件のコメント

ブログ開設 or ログイン してコメントを送ってみよう
目次をみる
技術ブログをはじめよう Qrunch(クランチ)は、プログラマの技術アプトプットに特化したブログサービスです
or 外部アカウントではじめる
10秒で技術ブログが作れます!