oop - What is polymorphism, what is it for, and how is it used? -


i watching google tech talks video, , referred polymorphism.

what polymorphism, for, , how used?

if think greek roots of term, should become obvious.

  • poly = many: polygon = many-sided, polystyrene = many styrenes (a), polyglot = many languages, , on.
  • morph = change or form: morphology = study of biological form, morpheus = greek god of dreams able take form.

so polymorphism ability (in programming) present same interface differing underlying forms (data types).

for example, in many languages, integers , floats implicitly polymorphic since can add, subtract, multiply , on, irrespective of fact types different. they're considered objects in usual term.

but, in same way, class bigdecimal or rational or imaginary can provide operations, though operate on different data types.

the classic example shape class , classes can inherit (square, circle, dodecahedron, irregular polygon, splat , on).

with polymorphism, each of these classes have different underlying data. point shape needs 2 co-ordinates (assuming it's in two-dimensional space of course). circle needs center , radius. square or rectangle needs 2 co-ordinates top left , bottom right corners , (possibly) rotation. irregular polygon needs series of lines.

by making class responsible code data, can achieve polymorphism. in example, every class have own draw() function , client code do:

shape.draw() 

to correct behavior shape.

this in contrast old way of doing things in code separate data, , have had functions such drawsquare() , drawcircle().

object orientation, polymorphism , inheritance closely-related concepts , they're vital know. there have been many "silver bullets" during long career fizzled out oo paradigm has turned out one. learn it, understand it, love - you'll glad did :-)


(a) wrote joke turned out correct and, therefore, not funny. momomer styrene happens made carbon , hydrogen, c8h8, , polystyrene made groups of that, (c8h8)n.

perhaps should have stated polyp many occurrences of letter p although, i've had explain joke, doesn't seem funny either.

sometimes, should quit while you're behind :-)


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -