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
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Abstract Class in C#
WhatsApp
Aniket Narvankar
8y
6.7
k
0
1
25
Blog
Look the following points:
An Abstract Class Contains Abstract as well as Non Abstract Methods.
An Abstract Method is the one which has only declaration, its implementation is left to the class which derives the abstract class.
To Declare a Class as Abstract we must use a keyword abstract.
Example:
abstract class A
Abstract members can not be private.
For example:
abstract
class
P1
{
abstract
void
Display();
}
This will give a error abstract members can not be private,you can see the below screen shot
So,we must use a public access specifier for abstract method as by default methods are private.
abstract
class
P1
{
public
abstract
void
Display();
}
Class which derives the abstract class needs to implement abstract method of abstract class.
abstract
class
P1
{
public
abstract
void
Display();
}
class
P2: P1
{}
This will generate an error P2 does not implement inherited abstract member P1.Display() .
Look at the below image:
So the class which inherits abstract class needs to implement abstract method of abstract class.
abstract
class
P1
{
public
abstract
void
Display();
}
class
P2: P1
{
public
override
void
Display()
{
Console.WriteLine(
"Hi"
);
}
}
We can see i have implemented abstract method Display in class P2 which inherits abstract class P1.
Keyword static can not be used with abstract method.
Abstract class does not support multiple inheritance.Inheritance from abstract to abstract class is possible.
For example look at following code:
abstract
class
P1
{
public
abstract
void
Display();
public
void
Test()
{
Console.WriteLine(
"Welcome"
);
}
}
abstract
class
P3: P1
{
abstract
public
void
Test1();
}
abstract
class
p4: P3
{
abstract
public
void
Test2();
}
class
P2: p4
{
public
override
void
Display()
{
Console.WriteLine(
"Hi"
);
}
public
override
void
Test1()
{
Console.WriteLine(
"Hello"
);
}
public
override
void
Test2()
{
Console.WriteLine(
"Welcome"
);
}
}
We can not create a object of abstract class look at the below image, it will raise an error can not create instance of an abstract class.
We can not create a object of abstract class, but we can create reference of it. Below is the practical example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
ConsoleApplication5
{
abstract
class
P1
{
public
abstract
void
Display();
public
void
Test()
{
Console.WriteLine(
"Welcome"
);
}
}
abstract
class
P3: P1
{
abstract
public
void
Test1();
}
abstract
class
p4: P3
{
abstract
public
void
Test2();
}
class
P2: p4
{
public
override
void
Display()
{
Console.WriteLine(
"Hi"
);
}
public
override
void
Test1()
{
Console.WriteLine(
"Hello"
);
}
public
override
void
Test2()
{
Console.WriteLine(
"Welcome"
);
}
}
class
Program
{
static
void
Main(
string
[] args)
{
p4 objP4 =
new
P2();
objP4.Display();
objP4.Test();
objP4.Test1();
objP4.Test2();
P2 obj3 =
new
P2();
obj3.Test2();
obj3.Test1();
obj3.Test();
obj3.Display();
}
}
}
Abstract Class
C#
Up Next
Class, Inheritance And Abstract Class With Real Time Examples In C#
Ebook Download
View all
Object Oriented Programming Using C#
Read by 47.2k people
Download Now!
Learn
View all
Membership not found