Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Abstract Class in C#
WhatsApp
Ramesh Sivaperumal
13y
6.6
k
0
0
25
Blog
Abstract Class:
An abstract class cannot be instantiated.
An abstract class may contain abstract methods and also contain own concrete method.
We want use the abstract class must be inherited by the non abstract class.
A non-abstract class, which derived from an abstract class must include implementations for all inherited abstract methods
An abstract method is implicitly a virtual method.
In abstract method declaration provides no actual implementation, there is no method body; it has only the signature
The implementation is provided by an overriding method (using override keyword in c#), which is a work as a member of non-abstract class.
Code:
public
abstract
class
ClassBase
//abstract classes
{
//Abstract class constructor
public
ClassBase()
{
Console
.WriteLine(
"Abstract Constructor"
);
}
//Own completed method in Abstract Class
public
void
useinsub()
{
Console
.WriteLine(
"Call by sub-class/non abstract"
);
}
//Virtual method in abstract class
public
virtual
void
fill()
{
Console
.WriteLine(
"Abstract Virtual Function"
);
}
//Pure abstract method
public
abstract
void
fill2();
}
public
class
ClassSub
:
ClassBase
//Sub Class(Non Abstarct)
{
// Sub class constructor
public
ClassSub()
{
Console
.WriteLine(
"Sub Constructor"
);
}
// Over ride the virtual method in abstract class
public
override
void
fill()
{
Console
.WriteLine(
"Override the virtual Function"
);
}
// Implement the abstract method, which is in abstract class
public
override
void
fill2()
{
Console
.WriteLine(
"Implement the abstract Function"
);
//Call abstract class's own completed method
base
.useinsub();
}
}
class
Program
{
static
void
Main(
string
[] args)
{
ClassSub
cs =
new
ClassSub
();
cs.fill();
cs.fill2();
}
}
Abstract Class in C#
Up Next
Class, Inheritance And Abstract Class With Real Time Examples In C#
Ebook Download
View all
Introduction to C# 8
Read by 19.3k people
Download Now!
Learn
View all
Membership not found