Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p> Let's take your example of a Dog and a Cat class, and let's illustrate using C#:</p> <p>Both a dog and a cat are animals, specifically, quadruped mammals (animals are waaay too general). Let us assume that you have an abstract class Mammal, for both of them:</p> <pre class="lang-cs prettyprint-override"><code>public abstract class Mammal </code></pre> <p>This base class will probably have default methods such as:</p> <ul> <li>Feed</li> <li>Mate</li> </ul> <p>All of which are behavior that have more or less the same implementation between either species. To define this you will have:</p> <pre class="lang-cs prettyprint-override"><code>public class Dog : Mammal public class Cat : Mammal </code></pre> <p>Now let's suppose there are other mammals, which we will usually see in a zoo:</p> <pre class="lang-cs prettyprint-override"><code>public class Giraffe : Mammal public class Rhinoceros : Mammal public class Hippopotamus : Mammal </code></pre> <p>This will still be valid because at the core of the functionality <code>Feed()</code> and <code>Mate()</code> will still be the same.</p> <p>However, giraffes, rhinoceros, and hippos are not exactly animals that you can make pets out of. That's where an interface will be useful:</p> <pre class="lang-cs prettyprint-override"><code>public interface IPettable { IList&lt;Trick&gt; Tricks{get; set;} void Bathe(); void Train(Trick t); } </code></pre> <p>The implementation for the above contract will not be the same between a cat and dog; putting their implementations in an abstract class to inherit will be a bad idea. </p> <p>Your Dog and Cat definitions should now look like:</p> <pre class="lang-cs prettyprint-override"><code>public class Dog : Mammal, IPettable public class Cat : Mammal, IPettable </code></pre> <p>Theoretically you can override them from a higher base class, but essentially an interface allows you to add on only the things you need into a class without the need for inheritance.</p> <p>Consequently, because you can usually only inherit from one abstract class (in most statically typed OO languages that is... exceptions include C++) but be able to implement multiple interfaces, it allows you to construct objects in a strictly <em>as required</em> basis.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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