Abstraction in C#
In C#, abstraction is achieved with the help of Abstract classes and Abstract methods.
Abstract Classes
- Abstract classes and methods are defined by prefixing the class and method names with the "abstract" keyword.
- An object of an abstract class cannot be created.
- Abstract and non-abstract methods can both be found in an abstract class.
- A sealed class cannot be declared for an abstract class.
- We are not permitted to declare the abstract methods outside of the abstract class.
Abstract Methods
- Abstract Methods are only applicable in abstract classes.
- It lacks a body; the derived class (inherited from) gives it the body.
Syntax
//abstract class
abstract class <class_name>
{
//abstract method
public abstract <return_type> method_name();
}
Example
// C# program to calculate the area
// of a square using the concept of
// data abstraction
using System;
namespace abstraction_demo {
// abstract class
abstract class Shape {
public abstract int Area();
}
// Rectangle class inheriting
// the Shape class
class Rectangle : Shape{
// private data member
private int length;
private int breadth;
// abstract method
public Rectangle(int x , int y)
{
length=x;
breadth=y;
}
// overriding of the abstract method of Shape
// class using the override keyword
public override int Area()
{
return (length * breadth);
}
}
// Driver Class
class CodingNinja {
// Main Method
static void Main(string[] args)
{
// creating reference of Shape class
// which refer to Square class instance
Shape s = new Rectangle(4,2);
// calling the method
double result = s.Area();
Console.Write("Area of Rectangle: {0}", result);
}
}
}
Output
Area of Rectangle: 8
Advantages of Abstraction
- It simplifies the process of perceiving things.
- Code duplication is avoided, and reusability is increased.
-
As only necessary facts are shown to the user, it helps enhance the security of an application or software.
Also see, Ienumerable vs Iqueryable
FAQs
-
What is a Pure Abstract Base Class?
A Pure Abstract Base Class is a class that contains the abstract keyword and all of its methods.
-
Can we declare abstract methods outside the abstract class?
No, we cannot declare abstract methods outside abstract class. They can only be declared in abstract class.
-
State true or false. A higher level of abstraction requires a higher level of details?
False. The higher the level of abstraction, the lower are the details.
Key Takeaways
In this article, we have discussed C# Abstractions and their implementation in C# Language. We briefly introduced the topic and gave the basic definition of the topic. We also discussed Abstract class and methods with the help of syntax and examples. Abstraction is one of the most critical concepts in Object-oriented Programming.
If you wish to learn more about OOPs, you can visit Object-Oriented Programming.
Recommended Readings:
We hope that this blog has helped you enhance your knowledge regarding C# Abstractions and if you would like to learn more, check out our articles on Abstraction in Java.
Do upvote our blog to help other ninjas grow. Happy Coding!