DEV Community

IvanChen420
IvanChen420

Posted on

Abstraction, encapsulation, and inheritance in C#

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.

Abstraction

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.
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.
In C#, we use abstract class to represent abstraction.

// abstract class
abstract class Operator {

// abstract method
public abstract int sum();
}

class Sum: Operator {

private int x;
private int y;
public Sum(int x,int y)
{
this.x = x;
this.y = y;
}

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

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

// Driver Class
class Test {

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

}
}
}

Encapsulation

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.

For example:

using system;

public class customer {

private string name;
// Accessor.

public string Getname() {

return name;

}

// Mutator.

public void Setname(string a) {

name = a;

}

}

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.

Inheritance

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.

For example :

// C# program to illustrate the
// concept of inheritance
using System;
namespace program {

// Base class
class base {
public string name;
// public method of base class
public void print(string name, string subject)
{
this.name = name;
Console.WriteLine("My name is: " + name);

}
}

// inheriting the base class using :
class child: base {

// constructor of derived class
public child()
{
Console.WriteLine("This is child class.");
}
}

class test {

// Main Method
static void Main(string[] args)
{
// creating object of the derived class
child c = new child();

  // calling the method of base class 
  // using the derived class object 
  c.print("Ivan"); 

}
}
}

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.

Conclusion

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

Top comments (0)