what is c# abstract class code example

Example 1: c# abstract class and interfaces

// ------------------- ABSTRACT vs INTERFACES ------------------------ //
// Both are used to define the architecture of the application


// ---------- ABSTRACT CLASS --------- //
// -> The methods can have an implementation (be defined)
// -> It's elements are private by default
// -> It can contain fields
// -> It can inherit from another abstract class or interface 
// -> It can have abstract and non-abstract methods. The abstract classes have to be implemented in the child class. 

public abstract class AbsParent { 
  
  	private int count = 0;
  
    public void foo1()    
    {
    	Console.WriteLine("Hi there, I'm a normal method that will be inherited!");
    }
  
    public abstract void foo2();  // You can't have an implementation for this abstract method 
  
    public virtual void foo3()    
    {
    	Console.WriteLine("Hi there, I'm a virtual method!");
    }  
} 


// --------- INTERFACES --------- //
// -> It's not a class, it's an entity
// -> The methods can't have an implementation
// -> It's elements are public by default and can't have acesso modifiers 
// -> It can't contain fields like for example, "private int count", but it can contain properties 
// -> It can only inherit from another interface 
// -> A class can inherit from multiple interfaces
// -> A class inheriting from an interface, has to implement all it's methods 

public interface IParent { 
  
  string myProterty{get; set;} 
  
  void foo1(); // You can't have an implementation for these methods
  void foo2(); 
  
} 



// -------- INHERITANCES --------- // 

// class 'AbsParent' inherit in child class 'Child1' 
public class Child1 : AbsParent { 
  
    public override void foo2()    // The implementation of the AbsParent abstract methods is mandatory 
    { 
        Console.WriteLine("Only in the child/derived class, can I be defined!"); 
    } 
    
  
  	public override void foo3() 
    { 
        Console.WriteLine("Class name is Child1"); 
    } 
} 
  

// class 'IParent' inherit in another child class 'Child2' 
public class Child2 : IParent { 
 
    public void foo1()    // The implementation of all the IParent methods is mandatory 
    { 
        Console.WriteLine("Only in the child/derived class, can I be defined!"); 
    } 
    
  
  	public void foo2() 
    { 
        Console.WriteLine("Only in the child/derived class, can I be defined!"); 
    } 

} 



// ---------- MAIN --------- //
public class main_method { 
  
    // Main Method 
    public static void Main() 
    { 
        AbsParent obj = new AbsParent();  // ERROR!! You can't create an object of an abstract class
                                          // it is used only for inheritance purposes. The same logic 
                                          // goes to interfaces.
    } 
}

Example 2: abstract class c#

abstract class Shape
{
    public abstract int GetArea();
}

class Square : Shape
{
    int side;

    public Square(int n) => side = n;

    // GetArea method is required to avoid a compile-time error.
    public override int GetArea() => side * side;

    static void Main() 
    {
        var sq = new Square(12);
        Console.WriteLine($"Area of the square = {sq.GetArea()}");
    }
}
// Output: Area of the square = 144

Tags:

Java Example