Advertisement



< Prev
Next >



C# Abstract Class





An abstract class is declared with the abstract keyword. Unlike a regular class in C#, an abstract class may not only contain the regular methods, defined with curly braces { } but may also contain the abstract methods, ending with a semicolon; or a mix of regular and abstract methods.






What are abstract methods?


Abstract methods are not implemented in the abstract class and are declared with -: Please remember, the internal logic of an abstract method is not provided, which means that an abstract method is not defined within a pair of curly braces {} and it ends with a semicolon ;






An example of an abstract method -
abstract public void add();

In the example, we have declared a method add with an abstract keyword, which makes it an abstract method. T his method has a public access modifier and a void return-type, which means that this abstract method will not return any value. And as it should be, this abstract method ends with a semicolon ;




Please Note:





Advertisement




Some very important points about an abstract class.



//C# Example of an abstract class inheriting another abstract class

using System;


//An interface 
interface A
{
//The method of interface method is abstract and public by default 
void Message();
}


//An abstract class B inheriting an interface A
abstract class B:A
{
//Providing the implementation of the inherited method of interface
//Which is a MUST in this case
public void Message()
{
	Console.WriteLine("Hello!");
}
}


//A concrete class Ex inheriting abstract class B(which has inherited interface A)
class Ex:B
{
public static void Main(String[]ar)
{
	//Creating an object of concrete class Ex
	Ex ob = new Ex();

	//Calling the inherited and implemented method of abstract class
	ob.Message();
}
}

Output


Hello!

An abstract class B has implemented an interface A and has also implemented its method Message(). Not implementing the abstract method of an interface by the inheriting abstract class, would've issued a compile error.



Please share this article -





< Prev
Next >
< C# Interface
C# Method Return Types >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement