C Abstraction
C Abstraction
To create an abstract class in C#, you use the “abstract” keyword before the class definition. Here is an
example:
• C#
using System;
Console.WriteLine("Moving...");
}
public class Dog : Animal
class Program
animal.Move();
Output
Example:
Abstract Method: A method that is declared abstract, has no “body” and is declared inside the abstract
class only. An abstract method must be implemented in all non-abstract classes using the override
keyword. After overriding, the abstract method is in the non-Abstract class. We can derive this class in
another class, and again we can override the same abstract method with it.
Syntax:
Abstract Class: This is the way to achieve the abstraction in C#. An Abstract class is never intended to be
instantiated directly. An abstract class can also be created without any abstract methods, We can mark a
class abstract even if doesn’t have any abstract method. The Abstract classes are typically used to define
a base class in the class hierarchy. Or in other words, an abstract class is an incomplete class or a special
class we can’t be instantiated. The purpose of an abstract class is to provide a blueprint for derived
classes and set some rules that the derived classes must implement when they inherit an abstract class.
We can use an abstract class as a base class and all derived classes must implement abstract definitions.
Syntax:
Important Points:
• A user must use the override keyword before the method is declared as abstract in the child
class, the abstract class is used to inherit in the child class.
• It can’t be static.
• C#
using System;
// 'override' keyword
{
Console.WriteLine("class Geek1");
Console.WriteLine("class Geek2");
// Driver Class
// Main Method
// GeeksForGeeks' cannot
// be instantiate
GeeksForGeeks g;
g = new Geek1();
g.gfg();
g = new Geek2();
g.gfg();
Output
class Geek1
class Geek2
Example 2: Program to calculate the area of a square using abstract class and abstract method
• C#
using System;
// declare class 'AreaClass'
// as abstract
// declare method
// 'Area' as abstract
int side = 0;
// constructor
public Square(int n)
side = n;
}
}
class gfg {
// Main Method
Output
Area = 36
Syntax:
Example:
• C#
// abstract class
using System;
// method 'MultiplyTwoNumbers'
// Driver Class
class geek {
// Main Method
d.AddTwoNumbers(4, 6),
d.MultiplyTwoNumbers(6, 4));
Output
Addition : 10
Multiplication :24
2) Abstract class can also work with get and set accessors.
Example:
• C#
using System;
get;
set;
get
return myNumber;
set
myNumber = value;
}
}
// Driver Class
class geek {
// Main Method
d.numbers = 5;
Console.WriteLine(d.numbers);
Output
There are several advantages and disadvantages to using abstract classes in C#. Here are some of the
main ones:
Advantages:
1. Encapsulation: Abstract classes allow you to define a common set of behaviors or properties that
derived classes should have, without
2. exposing the implementation details of those behaviors or properties to the outside world. This
can help you create more maintainable and flexible code.
3. Code reuse: Abstract classes can be used as a base class for multiple derived classes, which can
help reduce code duplication and improve code reuse.
4. Polymorphism: Abstract classes can be used to achieve polymorphism, which allows you to write
code that works with objects of different derived classes, as long as they all inherit from the
same abstract base class.
Disadvantages:
1. Tight coupling: Abstract classes can create tight coupling between the base class and derived
classes, which can make it harder to modify the base class without affecting the derived classes.
2. Limited inheritance: C# only allows a class to inherit from a single base class, so if you use an
abstract class as a base class, you limit the ability of derived classes to inherit from other classes.
3. Difficulty in testing: Because abstract classes cannot be instantiated directly, they can be more
difficult to test than regular classes. In order to test a derived class, you may need to create a
mock or stub of the abstract base class.
4. Overall, abstract classes can be a powerful tool for creating flexible and maintainable code, but
they should be used judiciously, taking into account the specific requirements of your
application.