Can we create an object for Abstract Class in C#
Can we create an object for Abstract Class in C#
- Abstraction lacks a complete implementation, which prevents the formation of an instance and an object of that class. An abstract class's role as a base for subclasses is to serve this objective.
- It functions as a template or an empty or largely empties a structure before using it, you must expand upon it and build upon it.
- Interfaces and abstract classes have many similarities. They are classes that cannot be instantiated and typically have either incomplete or no implementation.
- An important distinction between abstract classes and interfaces is that whereas a class may implement an infinite number of interfaces, it may only inherited from one abstract class (or any other form of class).
- Interfaces may still be implemented by classes that have derived from abstract classes.
ABSTRACT CLASSES:
When building components, abstract classes are helpful because they let you declare a constant level of capability in some methods while leaving the development of other methods until a particular implementation of that class is required.
They also support versioning well because new functionality may be introduced to the base class without causing code to fail if it is required for derived classes.
We are unable to construct objects of an abstract class in C#. The abstract keyword is used in order to create abstract classes. As an example
// Creating a class that is abstract.
abstract class Language_abstract {
// include the required fields and methods
}
.......
// create an object for the Language_abstract
// it also throws an error
Language_abstract obj = new Language_abstract();
Both abstract methods (methods without a body) and non-abstract methods (methods with a body) are possible in an abstract class. For instance
abstract class Language_abstract {
// An abstract method creation
public abstract void display_1();
// A non-abstract method creation
public void display_2() {
Console.WriteLine("Non abstract method");
}
}
Inheriting an Abstract Class:
We must make a derived class from the abstract class as we cannot make objects of abstract classes. In order to access members of the abstract class, we can utilise an object from the derived class. As an example
using System;
namespace AbstractClass {
abstract class Language_abstract1 {
// It is a non-abstract technique.
public void display_function() {
Console.WriteLine("Implements Non abstract method");
}
}
// implementing the inheritance from the abstract class
class Program_abc : Language_abstract1 {
static void Main (string [] args) {
// creation of an object for the class Program_abc
Program_abc obj = new Program_abc();
// access a method for a particular abstract class i.e., display_function
obj.display_function();
Console.ReadLine();
}
}
}
OUTPUT:
![Can we create an object for Abstract Class in C#](https://static.tutorialandexample.com/csharp/can-we-create-an-object-for-abstract-class-in-csharp1.png)
Explanation:
In the example above, we made an abstract class called Language. A non-abstract method called display_function() is present in the class.
The abstract class is inherited by the new Program class that we constructed. Take note of the phrase,
obj.display_function();
Obj is the object of the derived class Program in this instance. Through the use of the object obj, we are invoking the abstract class method.