Linux

C# Abstract Class

An abstract class deals with the process of abstraction. It is a phenomenon in which the details of any method are hidden, and only the functionality is shown. It is not a complete implementation. We cannot instantiate an abstract class directly; it can be created without using an abstract method. The elementary purpose of an abstract class is to offer a guide for all the derived classes inherited by it.

The syntax for abstract class:

An ‘abstract’ keyword is utilized to generate an abstract class. Whereas in the case of an abstract function, it contains nobody only declared inside the class. An abstract method is used inside the derived, or you can say in all non-abstract classes by using a keyword ‘override’. Using this keyword, an abstract method is not in the state of abstraction; it can be used as a regular function. A derived class that is the child of the abstract class can override the same abstract methods.

The syntax for an abstract method:

Public abstract void smp() ;

Structure of an abstract class

An abstract class is used mostly in the inheritance process. Structures cannot override any abstract class. An abstract class is not in favor of multiple inheritances. Similarly, an abstract class cannot be declared as a static class, as a static class cannot be derived.

Example 1

To implement the concept of an abstract class, we have created a sample program. In which we used a class sample with the keyword abstract. Inside the class, an abstract method is stated. The abstract class always has an abstract method.

Public abstract class sample() {

Public abstract void smp]();

As we only define the functionality of the abstract methods inside the abstract class, instead of explaining the working of the functions, so only the header of the function is declared here.

An abstract class cannot be accessed directly because we do not create an object of it; it is said to be a dummy class or a non-active class until it becomes active by creating an object of the class derived from it. So we will drive the class sample1 from the base abstract class sample. This class is not abstract. Sample class is inherited in the child class sample1. The way of representing an inherent class from the base class is to use the parent class name with a colon sign in front of the derived class. Here sample1 is a derived class.

# Public class sample1: sample

When we inherit a class from the abstract class, it can use the features, and the methods present inside the abstract class. For example, the smp() method can be declared in the sample1 class, but not with the name of abstract because it is not an abstract class anymore. It is written with the keyword ‘Override’ to depict that it is inherited from an abstract class. A simple statement is displayed inside the program.

# Public override void smp ()

Similarly, another class sample2 is inherited from the class sample. And it also uses the override method smp(). To initialize the abstract class, we will not create an object for it. But the instance of the derived or the child class will be instantiated. The abstract class will automatically become active and can be accessed easily by creating the object.

‘s’ is the object of the sample class; it is created, but it is not instantiated.

This dynamical creation will instantiate class sample1 by using a ‘new’ operator. We will now access the function declared inside the abstract class through this object.

Similarly, the same object for the sample2 class will be created. And then, we will call the function from the parent class. When we instantiate the object for the class, we will use it for the function call in the next step, immediately making it specified for the function used inside the class.

Save the code; we will execute it in the Ubuntu terminal, use the MCS compiler to compile the code, and mono to execute it.

$ MCS file.cs

$ mono file.exe

On execution, you can see that both the values inside the functions are displayed.

Example 2

By using an abstract class, we will now calculate the area. So an abstract class for the area is created, in which an abstract method for the area will be defined.

Abstract class AreaClass {

Abstract public int Area ();

}

A derived class square will be inherited from the base class. This will calculate the area by using the abstract method inside it. First, a variable is declared to get the number from the main program.

# Class square: AreaClass

A constructor is created to assign the sent value from the main program. Then the function will be overridden from the abstract class. This method will compute the area of the square by multiplying both sides.

Create another class to declare the main program. Here, the object for the child class is created and will be instantiated as we need to send the number to the constructor of the square class to create the object.

# Square s = new square (15);

By using the same object, the function will be called.

The console will print the area calculated by the function.

Example 3

The abstract class doesn’t need to contain all the abstract methods. But also, non-abstract methods can be stated in an abstract class. We have used the below example to elaborate on this concept. Create an abstract class. A simple function is created and is in a fully functioned state, as it accepts the parameters sent to it from the function call and returns the sum of both the numbers.

# Public int sum (int num1, int num2)

After the declaration of the regular function, an abstract method is defined, but its body is not declared here as it is an abstract function.

# Public abstract int multiply (int num1, int num2)

After the abstract class, we will create a derived class that will inherit the abstract function of multiplication.

Class Derived: AbstractClass {

Public override int multiply (int num1, int num2)

This function will multiply both values and then return the answer.

Now we will create a main program to create the object.

Derived d = new Derived();

d.sum(20, 8);

d.multiply (20, 8);

Conclusion

An abstract class in C sharp is created to hide the information by only defining the function’s header. This abstract class is non-functional until another child class is derived through it. An abstract class is used in inheritance; otherwise, the declaration of an abstract class is useless. Because of inheritance, we access it by creating and instantiating the object for the child class. It is not mandatory that an abstract class must have all the abstract methods within it. We have explained the working and declaration of an abstract class by using some examples and implementing them in Ubuntu.

Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button