advertisement
Abstract Class and Abstract Methods in C# Programming
Abstract Classes
C# allows designing a class specifically to be used as a base class by declaring it an abstract class. Such a class can be referred to as an incomplete base class, as it cannot be instantiated, but it can only be implemented or derived.
An abstract class is a class declared using the abstract keyword and which may or may not contain one or more of the following: normal data member(s), normal method(s), and abstract method(s).
Purpose
Consider the base class, Animal, that defines methods such as Eat (), Habitat (0, and Animal Sound (). The Animal class is inherited by different subclasses such as Dog, Cat, Lion, and Tiger. The dogs, cats, lions, and tigers neither share the same food nor habitat nor do they make similar sounds. Hence, the Eat(), Habitat (), and Animal Sound () methods must be different for different animals even though they inherit the same base class. These differences can be incorporated using abstract classes.
Definition
An abstract class can implement methods that are similar for all the subclasses. Additionally, it can declare methods that are different for different subclasses without implementing them. Such methods are referred to as abstract methods.
A class that is defined using the abstract keyword and that contains at least one method that is not implemented in the class itself is referred to as an abstract class. Without the abstract keyword, the class cannot be compiled. Since the abstract class contains at least one method without a body, the class cannot be instantiated using the new keyword.
Implementation
An abstract class can be implemented in a way similar to implementing a normal base class. The subclass inheriting the abstract class has to override and implement the abstract methods. In addition, the subclass can implement the methods implemented in the abstract class with the same name and arguments.
If the subclass fails to implement the abstract methods, the subclass cannot be instantiated as the C# compiler considers it as abstract.
Implement Abstract Base Class Using IntelliSense
IntelliSense provides access to member variables, functions, and methods of an object or a class. Thus, it helps the programmer to easily develop the software by reducing the amount of input typed in, since IntelliSense performs the required typing. IntelliSense can be used to implement system-defined abstract classes.
The steps performed to implement an abstract class using IntelliSense are as follows:
- Place the cursor after the class IntelliSenseDemo statement.
- Type: TimeZone. Now the class declaration becomes class IntelliSenseDemo: TimeZone. (The TimeZone class is a system-defined class that represents the time zone where the standard time is being used.)
- Click the smart tag that appears below the TimeZone class.
- Click Implement abstract class System.TimeZone. IntelliSense provides four override methods from the system-defined TimeZone class to the user-defined IntelliSenseDemo class.
Abstract Methods
The methods in the abstract class that are declared without a body are termed abstract methods. These methods are implemented in the inheriting class.
Similar to a regular method, an abstract method is declared with access modifiers, a return type, and a signature. However, an abstract method does not have a body and the method declaration ends with a semicolon.
Abstract methods provide a common functionality for the classes inheriting the abstract class. The subclass of the abstract class can override and implement the abstract methods.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0