Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Abstraction?
3.
Abstraction in C#
3.1.
Abstract Classes
3.2.
Abstract Methods
3.3.
Syntax
4.
Example
5.
Advantages of Abstraction
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

C# Abstractions

Author Rhythm Jain
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hello Ninjas, today we will learn about Abstractions in C#. C# is a Microsoft object-oriented programming language that runs on the.NET Framework. C# has origins in the C family and is related to other popular languages such as C++ and Java.

What is Abstraction?

Abstraction is an object-oriented programming paradigm that "shows" the essential features while "hiding" redundant data. The primary goal of abstraction is to shield people from irrelevant information.

Now let us look at how to go about abstraction in C# language.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

  1. 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.
     
  2. 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.
     
  3. 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!

Previous article
Multiple Inheritance in C#
Next article
Access Modifiers in C#
Live masterclass