×

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# 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# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 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# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute 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.

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# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute 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# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.