×

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

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 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.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 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# 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# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

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

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

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

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# 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# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 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.

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.

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