<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sujith </title>
    <description>The latest articles on DEV Community by Sujith  (@gamersuji).</description>
    <link>https://dev.to/gamersuji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F146391%2F7b5fcd0d-c6ca-4457-a4ee-c9accda41cac.PNG</url>
      <title>DEV Community: Sujith </title>
      <link>https://dev.to/gamersuji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gamersuji"/>
    <language>en</language>
    <item>
      <title>C# Intro to the Abstract classes</title>
      <dc:creator>Sujith </dc:creator>
      <pubDate>Wed, 17 Jul 2019 08:50:50 +0000</pubDate>
      <link>https://dev.to/gamersuji/c-intro-to-the-abstract-classes-4m0</link>
      <guid>https://dev.to/gamersuji/c-intro-to-the-abstract-classes-4m0</guid>
      <description>&lt;h2&gt;
  
  
  What are Abstract classes?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Abstract classes are a combination of both a normal class (default class) and an Interface.
&lt;/h3&gt;

&lt;p&gt;&lt;b&gt;Abstract class as an Interface:&lt;/b&gt;  Like an interface you're not allowed to have an instance of an abstract class anywhere and also you're allowed to create "declaration only" methods like the methods inside interfaces and can later demand the inheritor of the abstract class to write the definitions. 
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Abstract class as a normal class:&lt;/b&gt; Like normal classes, you're allowed to have member variables, methods with definition and all the other advantages which will be covered in this post.
&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's jump into an example to see how to create an abstract class.
&lt;/h3&gt;

&lt;p&gt;
Let's write a scenario where we put a human to sleep at night. We can easily pull this off by creating a function &lt;b&gt;Sleep&lt;/b&gt; in a separate class, Writing a functionality in a separate class enables the class to be independent and most importantly it will be really easier to be used in more than a single recipient class. 
&lt;br&gt;
So, we create the &lt;b&gt;Sleep&lt;/b&gt; as an &lt;b&gt;abstract&lt;/b&gt; class.( There is a reason behind using an abstract class instead of a normal class. We will explain that in a bit but before that let's just see how to create an abstract class first.)
&lt;/p&gt;



&lt;p&gt;
&lt;b&gt;Creating an abstract class&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
To make a class into an &lt;b&gt;"abstract"&lt;/b&gt; class just put an &lt;b&gt;abstract&lt;/b&gt; keyword right in front of the class keyword.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="c1"&gt;//the inherited organisms will sleep whence this method is called&lt;/span&gt;
          &lt;span class="n"&gt;sleep&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
 The &lt;b&gt;Sleep&lt;/b&gt; abstract class has a method named &lt;b&gt;GetSleep()&lt;/b&gt; which switches the &lt;b&gt;sleep&lt;/b&gt; boolean to &lt;b&gt;true&lt;/b&gt;. Upon making the sleep to &lt;b&gt;true&lt;/b&gt; it will put the instance of a class which inherits the &lt;b&gt;Sleep&lt;/b&gt; class to sleep.(we will expand on the "the inherited organism will sleep" comment that has been written inside the sleep method after writing the rest of the structure required for a human to sleep).
&lt;/p&gt;


&lt;h3&gt;
  
  
  Let's start writing the class that inherits the &lt;b&gt;Sleep&lt;/b&gt; class.
&lt;/h3&gt;



&lt;p&gt;
As planned this inheriting class will be the class &lt;b&gt;Human&lt;/b&gt;. Since we've already wrote the functionality for the &lt;b&gt;sleep&lt;/b&gt; inside the &lt;b&gt;Sleep&lt;/b&gt; class. Thus we will pretty much leave the whole human class empty and inherit the sleep functionality from the &lt;b&gt;Sleep&lt;/b&gt; abstract class. One of the main reasons to inherit this behavior instead of just writing this as part of the &lt;b&gt;Human&lt;/b&gt; class is that the &lt;b&gt;Human&lt;/b&gt; class does not have to know what sleep is.
&lt;/p&gt;

&lt;h3&gt;
  
  
  This is one of the main reasons an abstract class is called an abstract class.
&lt;/h3&gt;

&lt;p&gt; 
The inherited class doesn't need to know the structure of the class that is inside the class that it inherits. It is just like an actual human, A human like you who wishes to sleep. You close your eyes and expect yourself to be put to sleep, but you as a conscious self have no control over the process of sleep, there is no &lt;b&gt;"sleep"&lt;/b&gt; button in your head that puts you to sleep. For you, the entire concept of sleep is &lt;b&gt;abstract&lt;/b&gt;. You know how sleep can be triggered but cannot directly put yourself to sleep.
&lt;br&gt;
 The best thing you can do is to close your eyes and wait till your brain puts you to sleep. So like a human who doesn't need to know the internal structure of sleep, this &lt;b&gt;Human&lt;/b&gt; class doesn't need to know the internal structure of a &lt;b&gt;Sleep&lt;/b&gt; class and how it does the &lt;b&gt;sleep process&lt;/b&gt;. For the &lt;b&gt;Human&lt;/b&gt; class the internal structure of a &lt;b&gt;Sleep&lt;/b&gt; class is &lt;b&gt;abstract&lt;/b&gt;.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sleep&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;
Now the &lt;b&gt;Human&lt;/b&gt; class is capable of sleeping whenever &lt;b&gt;GetSleep()&lt;/b&gt; is called over any human subject, Let's do that in a class called &lt;b&gt;Night&lt;/b&gt;. Because the night cycle is one of the pottential triggers for Humans or any living organisms to sleep.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Night&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;human&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PutEverybodyToSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Sleep&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;human&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
Here we have a &lt;b&gt;GameObject&lt;/b&gt;(An &lt;b&gt;Unity&lt;/b&gt; centric object, It serves as just a reference that holds the class &lt;b&gt;Human&lt;/b&gt; over it). Now we do a &lt;b&gt;GetComponent&lt;/b&gt; to get an instance of the &lt;b&gt;Sleep&lt;/b&gt; from the &lt;b&gt;Human GameObject&lt;/b&gt; and if it actually has an instance of &lt;b&gt;Sleep&lt;/b&gt;(we confirm it by doing a a null check) we call the &lt;b&gt;GetSleep()&lt;/b&gt; method to put the human subject to sleep. Since the &lt;b&gt;Human&lt;/b&gt; class directly inherits the &lt;b&gt;Sleep&lt;/b&gt; the &lt;b&gt;GetSleep&lt;/b&gt; method had become part of the &lt;b&gt;Human&lt;/b&gt; class.
&lt;br&gt;
As you have seen the &lt;b&gt;Human&lt;/b&gt; class has nothing written in it, All it did was inherit a &lt;b&gt;Sleep&lt;/b&gt; class into it and all of &lt;b&gt;sleep&lt;/b&gt; functionality came on to it for the &lt;b&gt;Night&lt;/b&gt; class to call it. 


&lt;/p&gt;



&lt;h3&gt;
  
  
  I promised right through the read that I will answer the following two questions in no chronological order.
&lt;/h3&gt;

&lt;p&gt;1) The comment of &lt;b&gt;GetSleep()&lt;/b&gt; inside the &lt;b&gt;sleep&lt;/b&gt; class saying, "the inherited organisms will sleep whence this method is called"  &lt;br&gt;
2) Why abstract intstead of a normal class ?&lt;/p&gt;

&lt;p&gt;I will answer the &lt;b&gt;first question&lt;/b&gt; first, The reason to say that the inherited organisms instead of an organism because we were only talking about a human at that point of time and this &lt;b&gt;GetSleep()&lt;/b&gt; was not just built for humans. The &lt;b&gt;GetSleep()&lt;/b&gt; method inside the &lt;b&gt;Sleep&lt;/b&gt; class upon inheriting will be a generic method that puts any organism to sleep as "sleep" is a common behaviour for all living organisms. So we can just inherit this class on any class types that require sleep behavior.&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Before answering my &lt;b&gt;second question&lt;/b&gt; I would like to remind you again that an abstract class is very similar to a normal class. Both are capable of handling a &lt;b&gt;constructor&lt;/b&gt;, both are capable of creating &lt;b&gt;local/global&lt;/b&gt; variables inside (just like the &lt;b&gt;sleep&lt;/b&gt; boolean inside the &lt;b&gt;Sleep&lt;/b&gt; class), both are capable of &lt;b&gt;encapsulating&lt;/b&gt;(hiding data from children classes or other unrelated classes) both member variables and methods inside a class. The last and the most important similarity in both types of classes are, they are capable of handling &lt;b&gt;virtual methods&lt;/b&gt;.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Wait! what are virtual methods?
&lt;/h3&gt;

&lt;p&gt;
Virtual methods are like any other method where they will have a method definition and will execute that definition upon calling that method. As an added advantage, If these methods are being inherited from a &lt;b&gt;parent&lt;/b&gt; class these classes can be modified in the &lt;b&gt;children's&lt;/b&gt; class by adding new definitions along with the existing definition or one can even over-write the entire definition provided by the parent class with a new set of definitions by using the &lt;b&gt;"override"&lt;/b&gt; keyword.
&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's write a short virtual method inside our sleep class
&lt;/h3&gt;

&lt;p&gt;
To make any method a virtual method add the &lt;b&gt;"virtual"&lt;/b&gt; keyword after an access specifier and before the return type of a method. Here in our example, there is a &lt;b&gt;Dream&lt;/b&gt; method that generates a basic not so nightmarish, non-eccentric dream upon called by any of the children's class instance. 
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sleep&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//the inherited organisms will sleep whence this method is called&lt;/span&gt;
            &lt;span class="n"&gt;sleep&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;BasicDream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BasicDream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//A basic not so nightmarish, non-eccentric dream  &lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
It is not mandatory for the children class to override the virtual method.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sleep&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
This should not throw you any compile time or run time errors, but if you'd like to change the definition, You're given a free hand to do so. Let's assume that our human subject is suffering from some sort of sleep disorder and has a hard time sleeping. We cannot expect our subject to be having a pleasant dream during the course of his slumber. So we override the existing &lt;b&gt;Dream&lt;/b&gt; method to give him a nightmare.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sleep&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//bring him some ghosts, clowns and zombies to wreck his sleep&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
Now if you call the &lt;b&gt;Dream()&lt;/b&gt; for an instance of a &lt;b&gt;Human&lt;/b&gt; class, This human instead of having a non-nightmarish dream, he will see ghosts, clowns, and zombies in his dreams.
&lt;br&gt;
Here we have overridden the previous definition and changed it to give our human subject a taste of hell. But don't you feel like we have been too hard on our test subject? I definitely feel like it. So let's give him a normal non-nightmarish dream after all of that nightmare. As you know this "non-nightmarish dream" functionality is already part of the base &lt;b&gt;Dream()&lt;/b&gt; method definition situated inside the parent class &lt;b&gt;Sleep&lt;/b&gt;. Thus we do not have to rewrite it. To call the base definition inside an overridden method just use the keyword &lt;b&gt;"base.MethodName()"&lt;/b&gt; (in our case it is base.Dream()) to use the base definition written inside the parent class.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//bring him some ghosts, clowns and zombies to wreck his sleep&lt;/span&gt;
            &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
The base class can be called inside an overridden method in no particular order. It can be either called before the overridden definitions or in-between the overridden definitions or even after the overridden definitions as we did above.
&lt;/p&gt;



&lt;h3&gt;
  
  
  Now that you've learned virtual methods, we can now continue with answering the second question which is "Why abstract class instead of a normal class"?
&lt;/h3&gt;

&lt;p&gt;
As you could see normal classes and abstract classes are very similar and there is only one reason that should probably make you use an abstract class instead of a normal class. It is the need for an Interface like methods. In an Interface, methods are just declared and not defined. The ability to define these methods will be made mandatory for the classes that inherit the interface. The same way we can create such methods in abstract classes by preceding the method with the &lt;b&gt;"abstract"&lt;/b&gt; keyword. Just add &lt;b&gt;"abstract"&lt;/b&gt; keyword after an access specifier and before the return type of a method to make a method an abstract method.
&lt;br&gt;
&lt;br&gt;
We now use the same old &lt;b&gt;Sleep&lt;/b&gt; class to create an abstract method called &lt;b&gt;EstablishSleepTime()&lt;/b&gt;. As you could see that the sleep time can change in between species to species, every species should be able to establish their own definition of managing their sleep time. 
&lt;br&gt;
Remember these abstract methods will not have definitions inside and the compiler will throw an error even if you try to give one. 
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sleep&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//the inherited organisms will sleep whence this method is called&lt;/span&gt;
            &lt;span class="n"&gt;sleep&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;BasicDream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BasicDream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//A basic non nightmar-ish, non eccentric dream  &lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EstablishSleepTime&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
And also remember that the &lt;b&gt;abstract methods&lt;/b&gt; can only be written in an &lt;b&gt;abstract class&lt;/b&gt;.
&lt;/p&gt;

&lt;p&gt;
Whence this class is inherited, the inherited class (in our case the &lt;b&gt;Human&lt;/b&gt; class) will have to establish the amount of he sleep any human should be getting. 
&lt;/p&gt;

&lt;p&gt;
Writing definitions for an abstract method can only be done by overriding the abstract method inside the inherited class just like overriding a virtual method inside an inherited class
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sleep&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GetSleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//bring him some ghosts, clowns and zombies to wreck his sleep&lt;/span&gt;
            &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EstablishSleepTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//get 8 hours of sleep for this human&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
Here inside the &lt;b&gt;EstablishSleepTime()&lt;/b&gt; method we define the amount of time a human can sleep. Since this &lt;b&gt;EstablishSleepTime()&lt;/b&gt; is made as an abstract method in the parent class this method is made as a mandatory class to be defined in the children's class. 
&lt;/p&gt;

&lt;h3&gt;
  
  
  This is the main reason why we use an abstract class instead of a normal class.
&lt;/h3&gt;

&lt;p&gt;
&lt;b&gt;The other main use of an abstract class:&lt;/b&gt;
&lt;br&gt;
If you could see the abstract class &lt;b&gt;Sleep&lt;/b&gt; you can witness that it is a very common behavior of every living thing. Even non-living things like computers sleep when they're tired. So this functionality can be inherited by any class that requires sleep. If you had written the sleep functionality inside every single class it would only be a duplicate of the same functionality inside every single class that requires sleep. Since abstract is capable of being a replacement of a normal class, We write this once in a class and we directly inherit it and use it anywhere like how we would do with a normal default class.
&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;
Abstract classes are a combination of both a normal class and an interface. A normal method cannot handle an abstract method and Interfaces are incapable of using constructors, encapsulation, member variables, and method definitions. But the abstract class is capable of doing what both of these classes cannot do. If your class structure demands for such a class to be inherited then you should use an &lt;b&gt;abstract&lt;/b&gt; class.
&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>interface</category>
      <category>unity3d</category>
      <category>csharp</category>
    </item>
    <item>
      <title>C# Intro to the Interface</title>
      <dc:creator>Sujith </dc:creator>
      <pubDate>Thu, 11 Jul 2019 02:11:58 +0000</pubDate>
      <link>https://dev.to/gamersuji/c-intro-to-interface-3b3f</link>
      <guid>https://dev.to/gamersuji/c-intro-to-interface-3b3f</guid>
      <description>&lt;h1&gt;
  
  
  Prelude
&lt;/h1&gt;

&lt;p&gt;Just so you know I'm not an Interface wizard who knows every nook and cranny that exists with an interface. I am just gonna explain how one can effectively use an interface and make their day/code less cumbersome and a lot cleaner. Also, a few elements of the examples will have some unity keywords in it, But that will not bog you from learning the fundamental concept that I'm trying to explain. Without further ado, let's jump into the topic. &lt;/p&gt;

&lt;h1&gt;
  
  
  What is an Interface?
&lt;/h1&gt;

&lt;p&gt;
An interface is a base class with a bunch of method declarations with no definitions, This interface class cannot work on its own as all its methods are just declarations. It will expect some other class to inherit this interface and upon inheriting the interface will make it mandatory to the inherited class to define all the method declarations inside it. In essence, an interface tells a class about the methods it needs to have in order for the class to perform certain functions but the interface will not tell what the function does, It will expect the writer of that class to know it.

&lt;br&gt;

&lt;b&gt; I hope this much is clear, let's continue with an example ,&lt;/b&gt; 
A human will perform a function of eating without which a human will die. If the human were a class we interface the class human with an Interface &lt;b&gt;IEat&lt;/b&gt; 
(People always precede &lt;b&gt;Interface&lt;/b&gt; with an &lt;b&gt;"I"&lt;/b&gt; so let us follow the same rule, It's not necessary but I'd advise everybody to follow the same). 
This
 &lt;b&gt;IEat&lt;/b&gt; 
will only add a method to the class &lt;b&gt;Human&lt;/b&gt; called &lt;b&gt;Eat&lt;/b&gt; and this method will have a parameter called &lt;b&gt;consummate amount&lt;/b&gt;. Remember this method or the parameter from the interface does not tell us how to eat or how much to eat. They will only tell these things to exist in class and later the writer of the class should specify how to eat the food and how much/what food to eat &lt;b&gt;Human&lt;/b&gt;.
&lt;/p&gt;

&lt;h4&gt;
  
  
  This is how the code for the above-mentioned functionality will look like
&lt;/h4&gt;

&lt;p&gt;&lt;b&gt;IEat.cs &lt;/b&gt; Interface&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IEat&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;consumateamount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;b&gt;Human.cs&lt;/b&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IEat&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;consumateamount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;PutItInMouth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;GetFood&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;consumateamount&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Food&lt;/span&gt; &lt;span class="nf"&gt;GetFood&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;consumateamount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Food&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Food&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apple&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;consumateamount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;meat&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;consumateamount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PutItInMouth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Food&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//Human Eats&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Food&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;meat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  So now let's see how we could use this &lt;b&gt;Eat(int consumateamount)&lt;/b&gt; for a human test subject.
&lt;/h3&gt;

&lt;p&gt;
Let's consider that the human brain has a class called &lt;b&gt;BrainSchedule,&lt;/b&gt; Inside the class we have each time cycle of a day as a method and inside every method we call the &lt;b&gt;Eat(int consumateamount)&lt;/b&gt; method for an instance of a  human subject. Here the amount of food consumed is determined by the time cycle method. 
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BrainSchedule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Human&lt;/span&gt; &lt;span class="n"&gt;myHuman&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Morning&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myHuman&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Noon&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myHuman&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Evening&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myHuman&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Night&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;myHuman&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
      As you could see the &lt;b&gt;BrainSchedule&lt;/b&gt; class assumes that the &lt;b&gt;Human&lt;/b&gt; class has an eat method inside it and calls that method at different cycles of the day to keep the human well nourished.
&lt;/p&gt;


&lt;h3&gt;
  
  
  We've just learned what an &lt;b&gt;Interface&lt;/b&gt; is, But this is not the best application of it.
&lt;/h3&gt;
&lt;h1&gt;
  
  
  Thus we have a make-believe scenario here where  &lt;b&gt;Interface&lt;/b&gt; could be used at it's best.
&lt;/h1&gt;

&lt;p&gt;

       Let's say we are maintaining a zoo, and this zoo has a variety of animals. Each animal will have its own base class. We as the zookeepers our main job here is to feed them. Even though each animal has its own unique food preference we can segregate them based on their food habits into three different categories.&lt;b&gt;Herbivores, Carnivores and Omnivores&lt;/b&gt;&lt;/p&gt; 

&lt;b&gt;Writing the feeding system &lt;/b&gt;

&lt;p&gt; 
  We will have a common class called Feeding System which allocates the amount of food and feeds it to the respective animals based on the category. The important point to be noted here is each category will have different subcategories. &lt;b&gt;For example,&lt;/b&gt; Herbivores eat foods ranging from hay to fruits. Some animals like &lt;b&gt;Koala&lt;/b&gt; will eat the only &lt;b&gt;eucalyptus&lt;/b&gt;. According to our design, the feeding system will only segregate foods based on the main categories, If Koala does not want to eat berries and only eat eucalyptus then it is up to the Koala to ignore the berries and consume eucalyptus, and will not be spoon-fed by the feeding system. (This in the real life would be a very irresponsible feeding system but for the sake of learning the Interface we are gonna bear with it).
&lt;br&gt;
&lt;br&gt;
Let's define what sort of foods these categories gonna have
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;//Just so I could view this over the inspector&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HerbivoreFood&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hay&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;strawberry&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;eucalyptus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CarnivoreFood&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;salmon&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;steak&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lamb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OmnivoreFood&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;CarnivoreFood&lt;/span&gt; &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;HerbivoreFood&lt;/span&gt; &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
Let's create three interfaces based on the three different categories, Each of this category based interfaces will impose one method that takes in the food type of its category type. &lt;b&gt;For example&lt;/b&gt;, Iherbivore interface should have an &lt;b&gt;EatGreens&lt;/b&gt; method that takes &lt;b&gt;HerbivoreFood&lt;/b&gt; instance as an argument. The code sample is provided below 
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IHerbivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatGreens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HerbivoreFood&lt;/span&gt; &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ICarnivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CarnivoreFood&lt;/span&gt; &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IOmnivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatGreensAndMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OmnivoreFood&lt;/span&gt; &lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
And let's create a base class for every animal in the zoo and we will make each animal inherit an interface that suits with their food habits. &lt;b&gt;For example,&lt;/b&gt; If an animal likes &lt;b&gt;berries&lt;/b&gt;, It will have access to those berries only if the feeding system recognizes that animal is an &lt;b&gt;herbivore&lt;/b&gt; and sends it herbivore based food. For the feeding system to recognize a given animal is herbivore or not we need to inherit the base class of an animal from the &lt;b&gt;IHerbivore&lt;/b&gt; interface. (If the animal only wants to eat meat then it should inherit from the &lt;b&gt;ICarnivore&lt;/b&gt; interface or else if it wants both then it should inherit from the &lt;b&gt;IOmnivore&lt;/b&gt; interface.) The code for the base classes are given below.

&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Koala&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IHerbivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatGreens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HerbivoreFood&lt;/span&gt; &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;EatEucalyptus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eucalyptus&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatEucalyptus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;eucalyptus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;eucalyptus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
As mentioned earlier due to &lt;b&gt;Koala's&lt;/b&gt; inheritance from the &lt;b&gt;IHerbivore&lt;/b&gt; class, It will be sent with all of the Herbivore based food but if you look properly at the class you'll realize that after receiving all of the herbivore based food it eats only what it wants to eat which is the &lt;b&gt;eucalyptus&lt;/b&gt; and leaves the rest. 
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Panther&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ICarnivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CarnivoreFood&lt;/span&gt; &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;EatSteak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steak&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatSteak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;steak&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;steak&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
Same way the panther eats only the &lt;b&gt;steak&lt;/b&gt; even if it has access to &lt;b&gt;lamb&lt;/b&gt; inside the &lt;b&gt;CarnivoreFood&lt;/b&gt; Class.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bear&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;IOmnivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatGreensAndMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OmnivoreFood&lt;/span&gt; &lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;EatBerries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strawberry&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;EatFish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salmon&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatBerries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;berries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;berries&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatFish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;salmon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;salmon&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
And &lt;b&gt;bear&lt;/b&gt; as usual being an omnivore eats both &lt;b&gt;berries&lt;/b&gt; and &lt;b&gt;salmon&lt;/b&gt;.
&lt;/p&gt;


&lt;h3&gt;
  
  
  Now let's write the feeding system
&lt;/h3&gt;

&lt;p&gt;
The Feeding system should first have access to all the animals in the zoo, So we have all the animals inside the &lt;b&gt;allZooAnimals&lt;/b&gt; array which is the type of a &lt;b&gt;GameObject&lt;/b&gt;. (It is a &lt;b&gt;Unity&lt;/b&gt; specific type don't pay much attention to it, just consider it as a common reference type). So far we have three animals scripts made so we will have three animals in the array and each animal &lt;b&gt;GameObject&lt;/b&gt; will have one of the three animal scripts attached to them. As we have all of our animals now we create objects for each food type and initialize it inside the &lt;b&gt;Start()&lt;/b&gt; method. After that, we call the &lt;b&gt;FeedAllAnimals()&lt;/b&gt; method, which will recognize the type of each animal script attached to each &lt;b&gt;GameObject&lt;/b&gt; in the array and call the appropriate method and pass the appropriate food type based on its category.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FeedingSystem&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;GameObject&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;allZooAnimals&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;HerbivoreFood&lt;/span&gt; &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;CarnivoreFood&lt;/span&gt; &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;OmnivoreFood&lt;/span&gt; &lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;///Get food ready&lt;/span&gt;
        &lt;span class="n"&gt;herbivoreFood&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HerbivoreFood&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hay&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eucalyptus&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strawberry&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


        &lt;span class="n"&gt;carnivoreFood&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CarnivoreFood&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;steak&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;60&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salmon&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lamb&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

       &lt;span class="n"&gt;omnivoreFood&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;OmnivoreFood&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;herbivoreFood&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;carnivoreFood&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nf"&gt;FeedAllAnimals&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;FeedAllAnimals&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;allZooAnimals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

            &lt;span class="n"&gt;IHerbivore&lt;/span&gt; &lt;span class="n"&gt;herbivore&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allZooAnimals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IHerbivore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;herbivore&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;herbivore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EatGreens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;ICarnivore&lt;/span&gt; &lt;span class="n"&gt;carnivore&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allZooAnimals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ICarnivore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;carnivore&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;carnivore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EatMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;IOmnivore&lt;/span&gt; &lt;span class="n"&gt;omnivore&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allZooAnimals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IOmnivore&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;omnivore&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;omnivore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EatGreensAndMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;omnivoreFood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
The &lt;b&gt;GetComponent &lt;/b&gt; inside the &lt;b&gt;FeedAllAnimals()&lt;/b&gt; method will get the instance of the animal as the type of the interface, We do this and not the base class of the animals because, the base class might have a hell lot functionalities which the feeding class does not need to know about, By accessing the instance as an interface we know that these methods inside the interface will compulsorily be written inside the class that inherits it, so we can pass the data with confidence also we use this to type check what sort of food that particular instance of the animal eats without having to know anything about that animal apart from its food habit just through the interface. 
&lt;/p&gt;

&lt;p&gt;
If you're buying a new animal to the zoo, Fret not! Just add an interface based on their diet to the animal and they will be ready to be fed, &lt;b&gt;For example,&lt;/b&gt; see this &lt;b&gt;Tiger&lt;/b&gt; and &lt;b&gt;Zebra&lt;/b&gt;.
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tiger&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ICarnivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatMeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CarnivoreFood&lt;/span&gt; &lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;EatLamb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;carnivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lamb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatLamb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lamb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;lamb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Zebra&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IHerbivore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatGreens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HerbivoreFood&lt;/span&gt; &lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;EatHay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;EatBerries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;herbivoreFood&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strawberry&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatHay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;hay&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;EatBerries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;berries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stomach&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;berries&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; So this marks the end of this tutorial or whatever you call this is, According to me this is the best use of an interface I've seen so far. Maybe there is more to the interface than meets the eye, if you know that is the case lets discuss it in the comments section below. 
&lt;br&gt;
&lt;br&gt;

P.S This is like the first blog that I've ever written in my entire life so if you see places where I could improve both in writing and programming please do let me know, Thanks XOXO 
&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>interface</category>
      <category>unity3d</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
