Saturday, 20 December 2014

Difference between Interface and abstract class

 

 Difference between Interface and abstract class

 

  • Declaration and a definition is given in an abstract class but interface contain only declaration.
  • Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
  • We can not declare a member field,constructor in an Interface but we can do this in abstraction.
  • We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.But we can use this in abstract class.
  • An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.But abstract class can.

Abstract Class :

  • Object of the abstract class cannot be created it is used only for inheritance.
  • We create a abstract class library which will be widely distributed or reused
  • Use an abstract class to define a common base class for a family of types.
  • Use an abstract class to provide default behavior.
  • Subclass only a base class in a hierarchy to which the class logically belongs.

When to use is following :
  •  Need to create multiple versions of your component since versioning is not a problem with abstract class.You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.
  •  Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

Example of Abstract Class


using System; 
using System.Collections.Generic;
usingSystem.Linq;
using System.Text; 
 
namespace ConsoleApplication4
{

abstract class M1 //declaration of abstract class
{   
   public int add(int a,int b)
    {
      return(a +b);
    }
}

class M2 : M1//inheriting abstract class
{
   public int mul(int a,int b)
      {
         return a * b;
      }
}


class test
{

   static void Main(string[] args)
    {
      M2 ob = new M2();
      int result = ob.add(10,20);//calling method of abstract class
      Console.WriteLine("the result is {0}",result);
      Console.ReadLine();
    }
}

}



Abstract method :

An abstract method is used in the abstract class and it is also used for declaration and
we cannot give definition to the abstract method in the abstract class.We have to give 
definition of the abstract method in the class in which the abstract class is inherited.
We give defintion to abstract method by using override keyword.

Example of abstract method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
   abstract class M1
   {
        public int add(int a, int b)
        {
            return (a + b);
        }

        public abstract int getClassName(); //Abstract method
    }
    class M2 :M1
    {
        public int mul(int a, int b)
        {
            return a * b;
        }

       public override int getClassName() //Giving definition to abstract method
        {
           return 1;
        }
    }
    class test
    {
        static void Main(string[] args)
        {
            M2 ob = new M2();
            int result = ob.add(10, 20);
            int abstractMethodResult = ob.getClassName(); //Calling abstract method
            Console.WriteLine("the result is {0}", result);
            Console.ReadLine();
        }
    }
}



Abstract Property :

  An abstract property does not provide implementation. It declares that the class supports properties, but leaves the accessor implementation to derived classes.

 Example of Abstract property

public abstract class Shape
{
  public abstract double Area
{
get;
}
} 
public class Square : Shape
{
private int side;

public Square(int side)
{
this.side = side;
}

public override double Area
{
get
{

// Given the side, return the area of a square: 

return side * side;
}
}
}
 
 
An Interface :

An Interface cannot contain definition it is used only for declaration.We cannot contain member variables and constructors in an interface. When we implement interface on any class then it enforces to give definition of the functions which are declared in the interface.

Example of Interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication3
{

  interface MyInterface //Interface declaration
   {
     void myMethod();
   }

 
class MyClass:MyInterface //Implementing interfac

{
 
   public static void Main()
    {
      MyClass cls = new MyClass();
      cls.myMethod();
    }

   public void myMethod() //Giving definition to interface function
   {
      Console.WriteLine("welcome to MCN IT SOLUTION");
      Console.ReadLine();
   }
}

}


 


Live Example of Interface is following :

In market different types of cars available where all cars have the same type of prototypes i.e parts. According to Interface definition it contains Function Prototypes so we can define a Interface called Car so that all Brands of classes can implement that.


namespace SecondProject 

     class Tata:car 
    { 
         
    public   int doors { get; set; } 
    public   int Wheels { get; set; } 
    public   ConsoleColor Color { get; set; } 
    public   int TopSpeed { get; set; } 
    public   string DisplayTopSpeed() 
    { 
        return "Top Speed is" + TopSpeed.ToString(); 
    } 
 
    public Tata(int doors, int wheels, ConsoleColor Color, int topspeed) 
    { 
        this.TopSpeed = topspeed; 
        this.doors = doors; 
        this.Wheels = wheels; 
 
    } 
    } 
 
    interface car 
    { 
        int doors {get; set; } 
        int Wheels { get; set; } 
        ConsoleColor Color { get; set; } 
        int TopSpeed { get; set; } 
         
        string DisplayTopSpeed(); 
    } 

1 comment:

  1. Hi Guys,

    If you have any questions regarding the blog please write to me.

    ReplyDelete