<?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: Nam Nguyen</title>
    <description>The latest articles on DEV Community by Nam Nguyen (@namvnngu).</description>
    <link>https://dev.to/namvnngu</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%2F340356%2Ff40c7a2d-3ba2-4419-9e7a-9d27f0dab221.png</url>
      <title>DEV Community: Nam Nguyen</title>
      <link>https://dev.to/namvnngu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/namvnngu"/>
    <language>en</language>
    <item>
      <title>How To Solve Algorithm And Data Structure Problems [Tips]</title>
      <dc:creator>Nam Nguyen</dc:creator>
      <pubDate>Thu, 10 Sep 2020 04:45:29 +0000</pubDate>
      <link>https://dev.to/namvnngu/how-to-solve-algorithm-and-data-structure-problems-tips-loi</link>
      <guid>https://dev.to/namvnngu/how-to-solve-algorithm-and-data-structure-problems-tips-loi</guid>
      <description>&lt;p&gt;Are you struggling to solve coding problems? I’d like to share my own method to approach the solution. This is a reference on which I can build your own.&lt;/p&gt;

&lt;p&gt;Table of Content&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A. 5 Steps To Solve A Problem&lt;/li&gt;
&lt;li&gt;B. Example&lt;/li&gt;
&lt;li&gt;C. Summary&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A. 5 Steps To Solve A Problem &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Comprehend problem&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read the problem carefully (and underline keywords)&lt;/li&gt;
&lt;li&gt;Extract rules from problem&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Analyze Test Cases/Examples&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identify and evaluate Input/Output&lt;/li&gt;
&lt;li&gt;Understand how the output is produced for each input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Define Data Structure&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose the appropriate representations of data in problem to achieve output
If you don’t know data structure yet, you learn data structure through my favourite video, &lt;a href="https://www.youtube.com/watch?v=zg9ih6SVACc" rel="noopener noreferrer"&gt;Data Structures - Computer Science Course for Beginners&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Design Algorithm&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write instructions to gain an output with a given corresponding input&lt;/li&gt;
&lt;li&gt;Read books, articles, and other code to get some useful techniques.&lt;/li&gt;
&lt;li&gt;Recommendations:

&lt;ul&gt;
&lt;li&gt;Article: &lt;a href="https://dev.to/iuliagroza/complete-introduction-to-the-30-most-essential-data-structures-algorithms-43kd"&gt;Complete Introduction to the 30 Most Essential Data Structures &amp;amp; Algorithms&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Book: Grokking Algorithms - Aditya Bhargava, Jed Limke&lt;/li&gt;
&lt;li&gt;More books: &lt;a href="https://dev.to/javinpaul/10-best-books-to-learn-data-structure-and-algorithms-in-java-python-c-and-c-5743"&gt;10 Best Books to Learn Data Structure and Algorithms in Java, Python, C, and C++&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Github code examples: &lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/williamfiset" rel="noopener noreferrer"&gt;
        williamfiset
      &lt;/a&gt; / &lt;a href="https://github.com/williamfiset/Algorithms" rel="noopener noreferrer"&gt;
        Algorithms
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A collection of algorithms and data structures
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Implement Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write algorithm in code &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  B. Example &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Given an array &lt;code&gt;A&lt;/code&gt; of integers of size &lt;code&gt;n&lt;/code&gt;. Commute the maxim sum of &lt;code&gt;s&lt;/code&gt; contiguous elements in the array. Return &lt;code&gt;-1&lt;/code&gt; if there is no valid output.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input : A = [5,3,1,2], s = 2
Output: 8

Input : A = [1], s = 3
Output: -1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1. Comprehend Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Information extraction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An array of integer&lt;/li&gt;
&lt;li&gt;The maximum sum&lt;/li&gt;
&lt;li&gt;Contiguous elements&lt;/li&gt;
&lt;li&gt;Return -1 if no valid input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you read the problem carefully, you can extract crucial information and make it explicit to solve the problem. If you don’t highlight out important terms, you will not be able to come to the correct output, which is time-consuming. For example, if you miss “contiguous” (consecutive), you will try to find a maximum combination from a set of combinations of the array, which is absolutely wrong.&lt;/p&gt;

&lt;p&gt;However, sometimes you face strange terminology which the problem’s author uses to describe the problem, I recommend you check out the list of common terms, &lt;a href="https://en.wikipedia.org/wiki/List_of_terms_relating_to_algorithms_and_data_structures" rel="noopener noreferrer"&gt;List of terms relating to algorithms and data structures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Analyze Test Cases/Examples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You utilize the above information extraction to apply on the input and return the correct output, so that you evaluate your understanding on the given problem. After that, you should try to think beyond on the given examples for edge cases. Edge cases are unexpected inputs which drive your algorithm to be incorrect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Given A = [5,3,1,2], s = 2
Output: 5 + 3 = 8  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Define Data Structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this problem, you can directly use the given array as a representation of data. In other problems, you should think of all possible data structures, list them out on paper, and choose the most efficient one. For example, if you want to map one item to another item (key-value relationship), a hash table is the best choice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Design Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make the game start easily, you can come up with a brute-force solution, which is the inefficient algorithm. Then you try to optimize it by thinking beyond it. You use and modify techniques or good algorithms to fit your certain problem. Therefore, reading articles, books and other codes is useful to improve your knowledge of algorithms. &lt;/p&gt;

&lt;p&gt;In this example, I would solve it in the brute-force way, and then optimize it with a technique called “Window Sliding”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Brute-force solution

Initialize max_sum as INT_MIN
For every element `index` from 0 to (n-s+1)
    Set current_sum as 0
    For every `number` from 0 to s
        current_sum = current_sum + arr[index+number]
    max_sum = max(max_sum, current_sum)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above solution, you need to use one loop and one inner loop to iterate through s elements to find the maximum sum. It takes &lt;code&gt;O(ns)&lt;/code&gt; times to gain the correct output. But can we eliminate s to reduce only &lt;code&gt;O(n)&lt;/code&gt;. That’s where the “Window Sliding” technique comes to our playground.&lt;/p&gt;

&lt;p&gt;Window Sliding is a technique of using a “window” with a specific length and run operation in this window. More specifically, consider a sub-array of length s, and move the sub-array by one element. Following the below figures to understand more:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Iteration 1&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff30zjeiz5lbi7o8qyf98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff30zjeiz5lbi7o8qyf98.png" alt="Iteration 1" width="241" height="91"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;Iteration 2&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh4bl89ruot71zbmntjkk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh4bl89ruot71zbmntjkk.png" alt="Iteration 2" width="241" height="91"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Optimal solution:
Initialize max_sum as INT_MIN
Initialize window_sum as 0

For every element `index` from 0 to s:
    window_sum += array[index]

max_sum = window_sum
For every element `index` from s to n:
    # Move to next window, and subtract the first
    # element in the previous window
    window_sum += arr[index] - arr[index - s]
    max_sum = max(max_sum, window_sum)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Implement Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s is a C# implementation of the above algorithm:&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;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;MaxContiguousSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;arr&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;n&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;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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;n&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;return&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;max_sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Int32&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&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;window_sum&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="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;s&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;window_sum&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;arr&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;max_sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;window_sum&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="n"&gt;s&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;n&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;window_sum&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;arr&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;arr&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;s&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
     &lt;span class="n"&gt;max_sum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;window_sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_sum&lt;/span&gt;&lt;span class="p"&gt;);&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;max_sum&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;h2&gt;
  
  
  C. Summary &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I hope that my step-by-step instructions can help you to form your own method of solving future problems. Remember, here is only a tip of solving a problem, and it does not try to show this is the only technique you should use to cope with any problem. Therefore, the more important tip I want to give you is &lt;code&gt;PRACTICE, PRACTICE, AND PRACTICE&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you find any mistakes in my post, you can comment below to correct me. Thanks for reading.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>datastructure</category>
    </item>
    <item>
      <title>Inheritance, Composition and Aggregation in C#</title>
      <dc:creator>Nam Nguyen</dc:creator>
      <pubDate>Thu, 14 May 2020 13:14:05 +0000</pubDate>
      <link>https://dev.to/namvnngu/inheritance-composition-and-aggregation-in-c-3dj8</link>
      <guid>https://dev.to/namvnngu/inheritance-composition-and-aggregation-in-c-3dj8</guid>
      <description>&lt;p&gt;In this post, I briefly introduce three main object-oriented programming terms for beginners in simple words. They are Inheritance, Composition and Aggregation. These programming techniques help developers write clean code, save many lines of code, refactor as well as maintain programs easily, where the developers have full control of the programs. After definition, I will list some main differences between composition and aggregation since they are quite similar.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Inheritance
&lt;/h1&gt;

&lt;p&gt;In real life, inheritance is the action of passing property, debts or rights of parents to their children. Similarly, in programming, inheritance is the ability of a class to “copy” or acquire all properties and methods from another class. The class inheriting from another is a child class or derived class, and the class inherited by another class is the parent class. The inheritance helps derived classes extend their functionality as well as type, and create an “is-a” relationship between parent and child classes.&lt;/p&gt;

&lt;p&gt;For example:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Fighter&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;_life&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;_attack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Fighter&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;life&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;attack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;_life&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;life&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="n"&gt;_attack&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attack&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="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;GetLife&lt;/span&gt;&lt;span class="p"&gt;()&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;_life&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;class&lt;/span&gt; &lt;span class="nc"&gt;Soldier&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Fighter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Soldier&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;life&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;attack&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;life&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attack&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 above example illustrates that &lt;em&gt;Fighter&lt;/em&gt; is a parent class and &lt;em&gt;Soldier&lt;/em&gt; is a child class. In the child class, you do not need to repeat all attributes and methods defined in the parent class. Apparently, the &lt;em&gt;Soldier&lt;/em&gt; “&lt;strong&gt;is-a&lt;/strong&gt;” Fighter. Let me create instances of them and make a call to &lt;em&gt;GetLife()&lt;/em&gt; method.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Soldier&lt;/span&gt; &lt;span class="n"&gt;soldier&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;Soldier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;life&lt;/span&gt; &lt;span class="n"&gt;soldier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetLife&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;life&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10&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;h1&gt;
  
  
  2. Composition and Aggregation
&lt;/h1&gt;

&lt;p&gt;Composition and aggregation are programming techniques which allow a class to “contain” one or more objects of other classes to form a big object doing some specific functionalities. The container is the superclass and the classes contained by the superclass are subclasses. Therefore, the superclass and subclasses have a “has-a” relationship.&lt;/p&gt;

&lt;p&gt;However, composition and aggregation must have some differences to distinguish between them. Let’s see the following examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composition:&lt;/strong&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;class&lt;/span&gt; &lt;span class="nc"&gt;ChairLeg&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;Material&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wood"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Chair&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;ChairLeg&lt;/span&gt; &lt;span class="n"&gt;_leg&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Chair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ChairLeg&lt;/span&gt; &lt;span class="n"&gt;leg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_leg&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leg&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;&lt;strong&gt;Aggregation&lt;/strong&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;class&lt;/span&gt; &lt;span class="nc"&gt;Chair&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;Material&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wood"&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;class&lt;/span&gt; &lt;span class="nc"&gt;DinnerTable&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;Chair&lt;/span&gt; &lt;span class="n"&gt;_chair&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Chair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Chair&lt;/span&gt; &lt;span class="n"&gt;chair&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_chair&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chair&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 two examples above show “has-a” relationship, DinnerTable “has-a” Chair around and Chair “has-a” ChairLeg (obviously, a chair has 4 four legs, but here is just an example for you to understand the concept). Therefore, DinnerTable has a loose relationship with Chair because DinnerTable can not be useless or destroyed if it does not have a Chair object. In other words, DinnerTable exists independently from Chair. However, oppositely, Chair has a solid relationship with ChairLeg, which means that Chair cannot exist independently without ChairLeg. &lt;/p&gt;

&lt;p&gt;As a result, the main difference is independence. In composition, subclasses' life cycle depends directly on superclass’s life cycle and vice versa. In aggregation, the subclasses and superclass have their own life cycle.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Conclusion
&lt;/h1&gt;

&lt;p&gt;This post is a brief explanation of inheritance, composition and aggregation in object-oriented programming for beginners. If you find any mistakes in my post or give my feedback, please comment below. I am welcome to your responses.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>oop</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction To Method Overloading and Method Overriding in C#</title>
      <dc:creator>Nam Nguyen</dc:creator>
      <pubDate>Sun, 10 May 2020 09:11:54 +0000</pubDate>
      <link>https://dev.to/namvnngu/introduction-to-method-overloading-and-method-overriding-in-c-388b</link>
      <guid>https://dev.to/namvnngu/introduction-to-method-overloading-and-method-overriding-in-c-388b</guid>
      <description>&lt;p&gt;In this post, I would introduce definition of method overloading and method overriding in C# (be similar in most programming languages such as Java and C++). Also, I would discuss the differences between these methods for you to find out which one is the most suitable in a specific situation. Sometimes, while you are building your programs, you inadvertently use them to save many lines of code. Therefore, I am here to give you a quick definition and help you identify them clearly.&lt;/p&gt;

&lt;p&gt;Before I jump into their definition and examples, I have one thing to let you know that method overloading and method overriding are two types of polymorphism. Polymorphism is a magical ability of objects to transform into multiple appearances, which allows the derived classes to have their own implementation (or their own playground in which you must conform to their rules). Let’s get started!&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Method Overloading
&lt;/h1&gt;

&lt;p&gt;Method Overloading is a special programming technique that gives you permission to define one or more one methods arming the same names with different types or numbers of arguments. At first place, beginners think that it is impossible to create two methods, for example, having the same name, and it would return a bunch of errors. Yes. Some errors occur if you do not follow rules of defining a method overloading. But no worries, I guarantee that it is easy and simple to use when it comes to see some below examples&lt;/p&gt;

&lt;p&gt;There are two ways of overloading a method:&lt;/p&gt;

&lt;h4&gt;
  
  
  a. Define different numbers of parameters:
&lt;/h4&gt;

&lt;p&gt;Oops. Just a quick definition here. You may see that sometimes I use “parameter” or “argument”, so what is the difference between them? “Parameter” is a hole defined in a method while “Argument” is an actual value passed to the method to make the method usable at runtime. Get back to examples to understand this way of overloading a method.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&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;RetrieveStudentInfo&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;studentID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt; &lt;span class="n"&gt;Method&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;// Or&lt;/span&gt;
   &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RetrieveStudentInfo&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;studentID&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt; &lt;span class="n"&gt;Method&lt;/span&gt;&lt;span class="err"&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 two methods above are a valid case of method overloading (they are not perfect methods but a good example). As can be seen from the above lines of code, the “RetrieveStudentInfo” method has two definition, but they have different numbers of parameters. Therefore, when you pass the  “RetrieveStudentInfo” method one argument (studentID), you are using the first method. And when you pass the method two arguments (studentID  and age), you are taking the second method. It’s awesome, isn’t it?&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;student&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;Student&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RetrieveStudentInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;98247&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="c1"&gt;// Print “First Method”&lt;/span&gt;

      &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RetrieveStudentInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;98247&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="c1"&gt;// Print “Second Method”&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;h4&gt;
  
  
  b. Define different types of parameters:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&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;RetrieveStudentInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt; &lt;span class="n"&gt;Method&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c1"&gt;// Or&lt;/span&gt;
   &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RetrieveStudentInfo&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;studentID&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt; &lt;span class="n"&gt;Method&lt;/span&gt;&lt;span class="err"&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 two methods above are also a typical usage of method overloading (again, they are not perfect methods but a good example). The first method takes &lt;em&gt;String&lt;/em&gt; as the first argument type and int as the second one. But, the below method accepts two arguments as &lt;em&gt;int&lt;/em&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Student&lt;/span&gt; &lt;span class="n"&gt;student&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;Student&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RetrieveStudentInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Nam&lt;/span&gt; &lt;span class="n"&gt;Nguyen&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="c1"&gt;// Print “First Method”&lt;/span&gt;

      &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RetrieveStudentInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;98247&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="c1"&gt;// Print “Second Method”&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;h1&gt;
  
  
  2. Method Overriding
&lt;/h1&gt;

&lt;p&gt;Method overriding is a special technique in object-oriented programming that allows you to override child-class methods defined in the parent class earlier. Before overriding, the child classes have to inherit the parent class. A quick definition, inheritance is a “copying” ability of children classes that they can “steal” all public properties and methods of the parent class. Please look at the below example to get understanding.&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="c1"&gt;// Parent class&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Fighter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Overridden method&lt;/span&gt;
   &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="nf"&gt;AttackDamage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt; &lt;span class="n"&gt;loses&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="err"&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="c1"&gt;// Child class&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Soldier&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Fighter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Overriding method&lt;/span&gt;
   &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="nf"&gt;AttackDamage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt; &lt;span class="n"&gt;loses&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="err"&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 “AttackDamage” method in “Soldier” class rewriting the one in “Fighter” is called overriding method, and the one in the parent class is the overridden method. Therefore, when you instantiate a “Soldier” object and call “AttackDamage” method, the console prints out “The enemy loses 10 points”&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;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;Fighter&lt;/span&gt; &lt;span class="n"&gt;soldier&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;Soldier&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;soldier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AttackDamage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// The enemy loses 10 points&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;h1&gt;
  
  
  3. Differences Between Method Overloading And Method Overriding
&lt;/h1&gt;

&lt;p&gt;Here is a list of main differences for beginners:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Method overriding requires forcefully to rewrite the method with the same parameters in terms of size and type. In meanwhile, method overloading is more flexible by allowing multiple types and different sizes which are its speciality and outstanding ability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Method overloading cannot perform outside the class that the first method is defined in, which means all overloading should be defined within the same class. Method overriding has a totally different story. Because method overriding is a representation of relationship between classes, specifically parent and child classes, overriding methods must be re-defined in other classes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Method overloading executes at compile-time where all overloaded methods are formed their argument types, size and definition early before executing the program. Method overriding is as stubborn as a mule, and it prefers to define itself in runtime, which means during execution, it shows “information” as well as actual “characteristics”.  &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  4. Conclusion
&lt;/h1&gt;

&lt;p&gt;This post is a quick introduction to Method Overloading and Method Overriding in C# as well as object-oriented programming. Also, this is my first time I’ve written a tech post. I want to force myself to review my own knowledge about programming concepts and to see how solid I know about OOp as well as how well I could explain it to my reader by my own words.&lt;/p&gt;

&lt;p&gt;Therefore, if you find any mistake in my post or give me feedback, please comment below. I am welcome to your responses.&lt;/p&gt;

&lt;h5&gt;
  
  
  Reference: &lt;a href="https://beginnersbook.com/2014/01/difference-between-method-overloading-and-overriding-in-java/" rel="noopener noreferrer"&gt;Difference between method Overloading and Overriding in java&lt;/a&gt;
&lt;/h5&gt;

</description>
      <category>csharp</category>
      <category>oop</category>
      <category>programmingconcepts</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
