What Is Abstraction In C#? - Tutorialspoint

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who
What is abstraction in C#? CsharpProgrammingServer Side Programming

Abstraction and encapsulation are related features in object-oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.

Abstraction can be achieved using abstract classes in C#. C# allows you to create abstract classes that are used to provide a partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.

The following are some of the key points −

  • You cannot create an instance of an abstract class

  • You cannot declare an abstract method outside an abstract class

  • When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.

Example

 Live Demo

using System; namespace Demo {    abstract class Shape {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int b = 0) {          length = a;          width = b;       }       public override int area () {          Console.WriteLine("Rectangle class area :");          return (width * length);       }    }    class RectangleTester {       static void Main(string[] args) {          Rectangle r = new Rectangle(20, 15);          double a = r.area();          Console.WriteLine("Area: {0}",a);          Console.ReadKey();       }    } }

Output

Rectangle class area : Area: 300 Samual Sam Samual Sam Updated on: 2020-06-22T09:29:18+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » What Is Abstraction In C#