when to use interface and abstract class in c# code example

Example 1: abstract class vs interface

Interfaces specify what a class must do and not how. 
It is the blueprint of the class.
It is used to achieve total abstraction. 

We are using implements keyword for interface.

Abstract=
Sometimes we may come across a situation
where we cannot provide implementation to
all the methods in a class. We want to leave the 
implementation to a class that extends it.
  In that case we declare a class
as abstract by using abstract keyword on method
signature.In my framework I have created my
PageBase class as super
class of the all page classes. 
I have collected all common elements
and functions into PageBase class and
all other page classes extent PageBase class.
By doing so, I don't have to locate very
common WebElements and it provides
reusability in my framework.
Also
1)Abstract classes cannot be instantiated
2)An abstarct classes contains abstract method,
concrete methods or both.
3)Any class which extends abstarct class must
  override all methods of abstract class
4)An abstarct class can contain either
  0 or more abstract method.

Example 2: 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.
    } 
}

Tags:

Misc Example