Algorithm


In C#, an abstract class is a class that cannot be instantiated on its own and is typically used as a base class for other classes. Abstract classes can contain abstract methods, which are methods that do not have a body and must be implemented by any non-abstract derived classes. Here's an example of an abstract class with an abstract method:

 

Code Examples

#1 Inheriting Abstract Class in C #

Code - C# Programming

using System;
namespace AbstractClass {

  abstract class Language {

    // non-abstract method
    public void display() {
      Console.WriteLine("Non abstract method");
    }
  }

  // inheriting from abstract class
  class Program : Language {

    static void Main (string [] args) {
      
      // object of Program class
      Program obj = new Program();

      // access method of an abstract class
      obj.display();

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Non abstract method

#2 Implementation of the abstract method in C#

Code - C# Programming

using System;
namespace AbstractClass {

  abstract class Animal {

    // abstract method
    public abstract void makeSound();
  }

  // inheriting from abstract class
  class Dog : Animal {

    // provide implementation of abstract method
    public override void makeSound() {

      Console.WriteLine("Bark Bark");

    }
  }
  class Program  {
    static void Main (string [] args) {
      // create an object of Dog class
      Dog obj = new Dog();
      obj.makeSound();    

      Console.ReadLine(); 
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Bark Bark

#3 Abstract class with get and set accessors in C#

Code - C# Programming

using System;
namespace AbstractClass {
  abstract class Animal {
    
    protected string name;
    // abstract method
    public abstract string Name {
      get;
      set;
    }
  }

  // inheriting from abstract class
  class Dog : Animal {

    // provide implementation of abstract method
    public override string Name {
      get {
        return name;
      }
      set {
        name = value; 
      }
    }
   
  }
  class Program  {
    static void Main (string [] args) {
      // create an object of Dog class
      Dog obj = new Dog();  
      obj.Name = "Tom";
      Console.WriteLine("Name: " + obj.Name); 

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Name: Tom

#4 Access Constructor of Abstract Classes in C#

Code - C# Programming

using System;
namespace AbstractClass {
  abstract class Animal {
    
    public Animal() {
      Console.WriteLine("Animal Constructor");
    }
  }

  class Dog : Animal {
    public Dog() {
      Console.WriteLine("Dog Constructor");
    }   
  }

  class Program  {
    static void Main (string [] args) {
      // create an object of Dog class
      Dog d1 = new Dog();  

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Animal Constructor
Dog Constructor

#5 C# Programming Abstraction

Code - C# Programming

using System;
namespace AbstractClass {
  abstract class MotorBike {
    
    public abstract void brake();
  }

  class SportsBike : MotorBike {

    // provide implementation of abstract method
    public override void brake() {
      Console.WriteLine("Sports Bike Brake");
    }
   
  }

  class MountainBike : MotorBike {

    // provide implementation of abstract method
    public override void brake() {      
      Console.WriteLine("Mountain Bike Brake");
    }
   
  }
  class Program  {
    static void Main (string [] args) {
      // create an object of SportsBike class
      SportsBike s1 = new SportsBike();  
      s1.brake();

      // create an object of MountainBike class
      MountainBike m1 = new MountainBike();
      m1.brake();

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Sports Bike Brake
Mountain Bike Brake
Advertisements

Demonstration


C# Programming abstract class and method