×

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#, Abstraction is achieved by two ways:
  1. By abstract class
  2. By Interface
Abstract class and interface both can have abstract methods which are necessary to achieve abstraction.
  1. Abstract Methods:
A Method which has only declaration and no definition is called Abstract Method. An abstract method can only be declared inside abstract class. It must be overridden else there is no meaning of declaring abstract method. Syntax:
abstract <return type> <method name>(<parameter list>);
An abstract method can’t be sealed. We can’t declare it as static. It’s internally a virtual method.
  1. Abstract Class:
Abstract class is the class which can have abstract and non abstract methods inside it. Abstract class is used to define a blueprint of a derived class. It can’t be instantiated. This needs to be derived if you want to use its properties. We have to give implementation of all the abstract methods of abstract class. If you are deriving an abstract class into your class then you must override all the abstract methods present in that class or declare your class as abstract. Let's see an example of Abstract class.
using System;
public abstract class Shape
{
                        public abstract void draw();
}
public class Triangle:Shape
{
                        public override void draw()
                        {
                                    Console.WriteLine("drawing traingles...");
                        }
}
public class TestAbstractClass
{
                        public static void Main (string[] args)
                        {
                                    Shape s=new Triangle();
                                    s.draw();
                        }
}
Output
drawing traingles...
C# abstract class example: having Constructors and methods in abstract class
 using System;
public abstract class Car
{
                        public Car()
                        {
                                    Console.WriteLine("Car is created");
                        }
            public abstract void show();
            public void speed()
            {
                        Console.WriteLine("speading...");
            }
}
public class Hundai:Car
{
                        public override void show()
                        {
                                    Console.WriteLine("showing brand new Hyundai i20....");
                        }
}
public class AbstractClassExample
{
public static void Main (string[] args)
{
                                    Hundai h=new Hundai();
                                    h.show();
                                    h.speed();
}
}
Output
Car is created
showing brand new Hyundai i20....
speading...

Related Topics

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.