Abstract Method

Updated : Oct 24, 2019 in Articles

What is abstract class and abstract method in c#

In this article, we will discuss completely from the beginning what is abstract class and abstract method in c#

What is an Abstract Class?

If a class can have abstract members ( one or more ) that class should be marked as an abstract, abstract is a keyword. it is not possible to create an object for abstract class.

An abstract class behavior is a prototype( incomplete ) (it means no implementation inside the class ) if any class can inherit abstract class must override all the abstract members of the parent class ( parent class nothing but an abstract class ).

an abstract class can have some concrete data and abstract members, concrete data means like constructors, field members, static members, etc

abstract class and abstract method in c#
abstract class and abstract method in c#

you see the following snippet c# code for Abstract class and abstract methods

abstract class Figure
 {
    //non abstract memebers
    Figure()
    {
    }
    public int Dimension;
    public int MyProperty { get; set; }
    //Abstract members
    public abstract int AbstractProperty { get; set; }
    public abstract double Area();
    public abstract double Perimeter();
 }

Concrete class means normal class it is a just keyword but concrete class doesn’t have abstract members

Abstract Class and Abstract method
Abstract Class and Abstract method

Create class name in your application name like someclass try to add Abstract members inside the class you will get compile-time errors

class someclass
  {
     //abstrat memebers
     public int MyProperty { get; set; }
     public abstract double Area();
     public abstract double perimeter();
 }

we can create an abstract class without abstract members also but that class can’t be instantiated its means it not possible to create an object.

Abstract Class and Abstract method
Abstract Class and Abstract method

What is abstract method?

An abstract method is a method in which declaration is provided but implementation is not. by using override keyword we will override the abstract methods

Abstract Method
Abstract Method

Example of Abstract Class

Create a New Console Application and add the following code to the various files added to that project.

Figure abstract class can have two abstract methods one is Area() and another one is Perimeter()

the normal concrete class Square, Circle also will have common functionality like Area, perimeters

square, circle class can be inherited from Figure abstract class its means must and should be you will implement abstract methods of Figure class with the help of override keyword

namespace AbstractClassDemo
{
    using System;
    abstract class Figure
    {
        public int Dimension;
        public abstract double Area();
        public abstract double Perimeter();
    }
    class Square : Figure
    {
        public override double Area()
        {
            return Dimension * Dimension;
        }
        public override double Perimeter()
        {
            return 4 * Dimension;
        }
    }
    class Circle : Figure
    {
        public override double Area()
        {
            return Math.PI * Dimension * Dimension;
        }
        public override double Perimeter()
        {
            return 2 * Math.PI * Dimension;
        }
    }
    class Program
    {
        static void Main()
        {
            Figure fig = new Square(); // or Circle(); 
            fig.Dimension = 10;
            Console.WriteLine(fig.Area());
            Console.WriteLine(fig.Perimeter());
        }
    }
}

the above example if you are trying to create an object(Figure fig = new Figure ) for figure class it is not possible because of figure class is incomplete behavior

the reference variable of an abstract class(fig) can store the object of Square, Circle 1. Figure fig = new Square(); 2.Figure fig = new Circle();

Output: If Object is of type Square, then the Area and Perimeter of Square is printed. Similarly, if the object is of type Circle then Area and Perimeter of Circle is printed.

Summary

In this article, we learned about completely about abstract class and abstract method Basic

For realtime example, you will go through the next article abstract class implementation in encapsulation

Latest posts by DuttaluruVijayakumar (see all)
Like
Like Love Haha Wow Sad Angry
101

Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Crave Freebies
February 12, 2020 9:03 pm

whoah this blog is wonderful i love reading your posts. Keep up the great work! You know, lots of people are looking around for this information, you can help them greatly.

Pedro Bojanowski
April 7, 2020 1:59 am

Bookmarked!, I enjoy your web site!

10B
April 19, 2020 3:27 pm

I have found the guidance interesting in the article. You are totally a wise person

3
0
Would love your thoughts, please comment.x
()
x