<?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: IvanChen420</title>
    <description>The latest articles on DEV Community by IvanChen420 (@ivanchen420).</description>
    <link>https://dev.to/ivanchen420</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%2F396255%2Fdc927036-b6ee-43a0-a334-514cf91b11cc.png</url>
      <title>DEV Community: IvanChen420</title>
      <link>https://dev.to/ivanchen420</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ivanchen420"/>
    <language>en</language>
    <item>
      <title>Abstraction, encapsulation, and inheritance in C#</title>
      <dc:creator>IvanChen420</dc:creator>
      <pubDate>Wed, 27 May 2020 09:32:06 +0000</pubDate>
      <link>https://dev.to/ivanchen420/abstraction-encapsulation-and-inheritance-in-c-404b</link>
      <guid>https://dev.to/ivanchen420/abstraction-encapsulation-and-inheritance-in-c-404b</guid>
      <description>&lt;p&gt;In this post, I will introduce you abstraction, encapsulation, and inheritance in simple words. These programming techniques can help developers write clean code, save spaces and easily maintain programs.&lt;/p&gt;

&lt;h1&gt;
  
  
  Abstraction
&lt;/h1&gt;

&lt;p&gt;Data abstraction is an attribute that shows only basic details to users. Ordinary or non-essential units will not be shown to users. Data abstraction can also be defined as the process of ignoring irrelevant details and only recognizing the required features of an object. The attributes and behaviour of objects distinguish them from other similar types of objects and also help to classify/group objects.&lt;br&gt;
For example, real-world example, People who use ATMs to withdraw money will only know to insert the card and enter the password point to select the money and enter the amount to withdraw the money, but they cannot know the specific process of compiling the cash withdrawal program inside the ATM.&lt;br&gt;
In C#, we use abstract class to represent abstraction.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;// abstract class &lt;br&gt;
abstract class Operator { &lt;/p&gt;

&lt;p&gt;// abstract method &lt;br&gt;
  public abstract int sum(); &lt;br&gt;
} &lt;/p&gt;

&lt;p&gt;class Sum: Operator { &lt;/p&gt;

&lt;p&gt;private int x;&lt;br&gt;
      private int y; &lt;br&gt;
  public Sum(int x,int y) &lt;br&gt;
  { &lt;br&gt;
      this.x = x; &lt;br&gt;
       this.y = y;&lt;br&gt;
  } &lt;/p&gt;

&lt;p&gt;// overriding of the abstract method&lt;br&gt;
  // class using the override keyword &lt;br&gt;
  public override int sum() &lt;br&gt;
  { &lt;br&gt;
      Console.Write("Sum is: "); &lt;br&gt;
      return (x + y); &lt;br&gt;
  } &lt;br&gt;
} &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above codes show for calculate summary of 2 integers. By using the abstract class we can simply do this to call the function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;// Driver Class &lt;br&gt;
class Test {&lt;br&gt;&lt;br&gt;
  // Main Method &lt;br&gt;
  static void Main(string[] args) &lt;br&gt;
  { &lt;br&gt;
      // creating reference of Shape class &lt;br&gt;
      // which refer to Sum class instance &lt;br&gt;
      Operator sh = new Sum(4,5); &lt;br&gt;
      // calling the method &lt;br&gt;
      double result = sh.sum(); &lt;br&gt;
      Console.Write("{0}", result); &lt;/p&gt;

&lt;p&gt;} &lt;br&gt;
} &lt;br&gt;
}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Encapsulation
&lt;/h1&gt;

&lt;p&gt;In an Object-Oriented programming language, encapsulation is a key language feature. Encapsulation is the procedure of encapsulating data and functions into a class. The need for encapsulation is to protect or prevent accidental damage to code due to small mistakes that we make. In object-oriented programming, data is regarded as a key element in program development, and the data is tightly packed with the function on which it is operated, and protected from accidentally modified by external functions.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;using system;&lt;br&gt;&lt;br&gt;
public class customer {&lt;br&gt;&lt;br&gt;
   private string name; &lt;br&gt;
   // Accessor.&lt;br&gt;&lt;br&gt;
   public string Getname() {&lt;br&gt;&lt;br&gt;
       return name;&lt;br&gt;&lt;br&gt;
   }&lt;br&gt;&lt;br&gt;
   // Mutator.&lt;br&gt;&lt;br&gt;
   public void Setname(string a) {&lt;br&gt;&lt;br&gt;
       name = a;&lt;br&gt;&lt;br&gt;
   }&lt;br&gt;&lt;br&gt;
} &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the above code, once the user set up the name for one customer. There is not any way to change that customer's name unless you set his name again.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inheritance
&lt;/h1&gt;

&lt;p&gt;Inheritance is an important feature of object-oriented programming. In C #, through which a class can inherit the functions (fields and methods) of another class. A class inherited from another class is a subclass or a derived class, and a class inherited from another class is a parent class. Inheritance helps derived classes extend their functions and types, and create a "yes" relationship between parent and child classes.&lt;/p&gt;

&lt;p&gt;For example :&lt;/p&gt;

&lt;p&gt;// C# program to illustrate the &lt;br&gt;
// concept of inheritance &lt;br&gt;
using System; &lt;br&gt;
namespace program { &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;// Base class &lt;br&gt;
class base {&lt;br&gt;
  public string name; &lt;br&gt;
  // public method of base class &lt;br&gt;
  public void print(string name, string subject) &lt;br&gt;
  { &lt;br&gt;
      this.name = name; &lt;br&gt;
      Console.WriteLine("My name is: " + name);&lt;br&gt;&lt;br&gt;
  } &lt;br&gt;
} &lt;/p&gt;

&lt;p&gt;// inheriting the base class using : &lt;br&gt;
class child: base { &lt;/p&gt;

&lt;p&gt;// constructor of derived class &lt;br&gt;
  public child() &lt;br&gt;
  { &lt;br&gt;
      Console.WriteLine("This is child class."); &lt;br&gt;
  } &lt;br&gt;
} &lt;/p&gt;

&lt;p&gt;class test { &lt;/p&gt;

&lt;p&gt;// Main Method &lt;br&gt;
  static void Main(string[] args) &lt;br&gt;
  { &lt;br&gt;
      // creating object of the derived class &lt;br&gt;
      child c = new child(); &lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // calling the method of base class 
  // using the derived class object 
  c.print("Ivan"); 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;} &lt;br&gt;
} &lt;br&gt;
} &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Above code will output "This is child class." and "My name is: Ivan". Like that in inheritance class, we can call the method in parent class through child class.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This post introduced abstraction, encapsulation and inheritance. Also their usage in C#. If you have any feedback, please comment below. You are always welcomed.&lt;/p&gt;

</description>
      <category>css</category>
      <category>csharp</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
