Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Abstraction in C#

C# Abstract

Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only. Abstraction can be achieved by two ways:

  1. Abstract class
  2. Interface

Abstract class and interface both can have abstract methods which are necessary for abstraction.

 

Abstract Method

A method which is declared abstract and has no body is called abstract method. It can be declared inside the abstract class only. Its implementation must be provided by derived classes. For example:

 

public abstract void draw();

An abstract method in C# is internally a virtual method so it can be overridden by the derived class. You can’t use static and virtual modifiers in abstract method declaration.

 

C# Abstract class

In C#, abstract class is a class which is declared abstract. It can have abstract and non-abstract methods. It cannot be instantiated. Its implementation must be provided by derived classes. Here, derived class is forced to provide the implementation of all the abstract methods.

Let’s see an example of abstract class in C# which has one abstract method draw(). Its implementation is provided by derived classes: Rectangle and Circle. Both classes have different implementation.

 

using System;  
public abstract class Shape  {  
    public abstract void draw();  
}  
public class Rectangle : Shape  {  
    public override void draw()  {  
        Console.WriteLine("drawing rectangle...");  
    }  
}  
public class Circle : Shape  {  
    public override void draw()  {  
        Console.WriteLine("drawing circle...");  
    }  
}  
public class TestAbstract  {  
    public static void Main()  {  
        Shape s;  
        s = new Rectangle();  
        s.draw();  
        s = new Circle();  
        s.draw();  
    }  
}  

Output:

drawing ractangle...
drawing circle...

C# Interface

Interface in C# is a blueprint of a class. It is like abstract class because all the methods which are declared inside the interface are abstract methods. It cannot have method body and cannot be instantiated.

It is used to achieve multiple inheritance which can’t be achieved by class. It is used to achieve fully abstraction because it cannot have method body.

Its implementation must be provided by class or struct. The class or struct which implements the interface, must provide the implementation of all the methods declared inside the interface.

Let’s see the example of interface in C# which has draw() method. Its implementation is provided by two classes: Rectangle and Circle.

 

using System;  
public interface Drawable  {  
    void draw();  
}  
public class Rectangle : Drawable  {  
    public void draw()  {  
        Console.WriteLine("drawing rectangle...");  
    }     
}  

public class Circle : Drawable  {  
    public void draw()  {  
        Console.WriteLine("drawing circle...");  
    }  
}  
public class TestInterface  {  
    public static void Main()  {  
        Drawable d;  
        d = new Rectangle();  
        d.draw();  
        d = new Circle();  
        d.draw();  
    }  
}  

Output:

drawing ractangle...
drawing circle...

Note: Interface methods are public and abstract by default. You cannot explicitly use public and abstract keywords for an interface method.

Try the following example in the editor below.

Given an interface Math , inherit it and override the Operation method to return the sum of two numbers.

Start solving Abstraction in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test