Abstract Methods and Class in C#
We have to do an abstract class as well because, in this type of class, different types of methods are used to Declare, but they are not implemented or define. Rather, they are left to implement in those Derived Classes, which are inherited with this type of Abstract Classes.
A class declared abstract may or may not include abstract methods. They are created with the purpose of being a superclass.
An abstract class can have constructors—this is one major difference between an abstract class and an interface.
When we create a method without a body is known as an abstract method, what the method contains the only declaration without any implementation & should be declared by only the ‘abstract modifier’.
It is mandatory to override an abstract method in the derived class.
These abstract classes and methods are declared with the 'abstract' keyword. An abstract class can only be extended, and cannot be directly instantiated.
The abstract class provides a little more than interfaces. The Interfaces do not include fields and superclass methods that get inherited, whereas abstract classes do. This means that an abstract class is more closely related to a class which extends it than an interface is to a class that implements it.
The abstract methods are must be implemented in the non-abstract class using the override keyword.
Features:
- The abstract class can inherit from a class and one or more interfaces.
- The abstract class can implement code with non-Abstract methods.
- The Abstract class can have modifiers for methods, properties etc.
- The Abstract class can have constants and fields.
- The abstract class can implement a property.
- The abstract class can have constructors or destructors.
- The abstract class cannot be inherited from by structures.
- The abstract class cannot support multiple inheritances.
The syntax of using the abstract method is as follows : <access-modifier>abstract<return-type>method name (parameter) |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication24
{
class myclass
{
public static void Main(string[] args)
{
demo2 ob = new demo4();
int a = ob.mul(2, 4);
demo1 ob1 = new demo2();
int b = ob1.mul(4, 2);
demo1 ob2 = new demo3();
int c = ob2.mul(4, 2);
Console.Write("{0},{1},{2}", a, b, c);
Console.ReadLine();
}
}
abstract class demo1
{
public int add(int i, int j)
{
return i + j;
}
public abstract int mul(int i, int j);
}
class demo2 : demo1
{
public override int mul(int i, int j)
{
return i * j;
}
}
class demo3 : demo1
{
public override int mul(int i, int j)
{
return i - j;
}
}
class demo4 : demo2
{
public override int mul(int i, int j)
{
return i + j;
}
}
}
If you want to know more about to abstract classes and methods then follow these links :
1). https://www.mindstick.com/articles/12035/abstract-class-in-c-sharp
2) https://www.mindstick.com/articles/11956/difference-between-interface-vs-abstract-class-in-c-sharp
3). https://www.mindstick.com/articles/1430/abstract-class
4). https://www.mindstick.com/articles/952/abstraction-in-c-sharp-with-example
5). https://www.mindstick.com/articles/92/static-and-abstract-class
Thank You for reading this article, and if you have any suggestion, Please comment in the comment box.
Anonymous User
16-Nov-2018It's according to my own experience :
After reading your article, There is no doubt that your article is very good.
And you have done a great job for it.
Abstract Function:
Virtual Function:
Anonymous User
16-Nov-2018Following statements should give you more clarity:
I hope this helps.
Anonymous User
16-Nov-2018Keep it up
Anonymous User
16-Nov-2018nice article for me.