Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Polymorphism is what you get when the same method applies to multiple classes. For example, both a String and a List might have "Reverse" methods. Both methods have the same name ("Reverse"). Both methods do something very similar (reverse all the characters or reverse the order of the elements in the list). But the implementation of each "Reverse" method is different and specific to its class. (In other words, the String reverses itself like a string, and the List reverses itself like a list.)</p> <p>To use a metaphor, you could say "Make Dinner" to a French chef or to a Japanese chef. Each would perform "make dinner" in their own characteristic way.</p> <p>The practical result is that you could create a "Reversing Engine" that accepts an object and calls "Reverse" on it. As long as the object has a Reverse method, your Reversing Engine will work.</p> <p>To extend the chef analogy, you could build a "Waiterbot" that tells chefs to "Make Dinner". The Waiterbot doesn't have to know what type of dinner is going to be made. It doesn't even have to make sure it's talking to a chef. All that matters is that the "chef" (or fireman, or vending machine, or pet food dispenser) knows what to do when it's told to "Make Dinner".</p> <p>What this buys you as a programmer is fewer lines of code and either type-safety or late binding. For example here's an example with type safety and early binding (in a c-like language that I'm making up as I go):</p> <pre><code>class BankAccount { void SubtractMonthlyFee } class CheckingAccount : BankAccount {} class SavingsAccount : BankAccount {} AssessFee(BankAccount acct) { // This will work for any class derived from // BankAccount; even classes that don't exist yet acct.SubtractMonthlyFee } main() { CheckingAccount chkAcct; SavingsAccount saveAcct; // both lines will compile, because both accounts // derive from "BankAccount". If you try to pass in // an object that doesn't, it won't compile, EVEN // if the object has a "SubtractMonthlyFee" method. AssessFee(chkAcct); AssessFee(saveAcct); } </code></pre> <p>Here's an example with no type safety but with late binding:</p> <pre><code>class DatabaseConnection { void ReleaseResources } class FileHandle { void ReleaseResources } FreeMemory(Object obj) { // This will work for any class that has a // "ReleaseResources" method (assuming all // classes are ultimately derived from Object. obj.ReleaseResources } main() { DatabaseConnection dbConn; FileHandle fh; // You can pass in anything at all and it will // compile just fine. But if you pass in an // object that doesn't have a "ReleaseResources" // method you'll get a run-time error. FreeMemory(dbConn); FreeMemory(fh); FreeMemory(acct); //FAIL! (but not until run-time) } </code></pre> <p>For an excellent example, look at the .NET ToString() method. All classes have it because all classes are derived from the Object class. But each class can implement ToString() in a way that makes sense for itself.</p> <p>EDIT: Simple != short, IMHO</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload