DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE

1.     An Abstract class doesn't provide full abstraction but an interface does provide full abstraction; i.e. both a declaration and a definition is given in an abstract class but not so in an interface.
2.     Using Abstract we can not achieve multiple inheritance but using an Interface we can achieve multiple inheritance.
3.     We can not declare a member field in an Interface.
4.     We can not use any access modifier i.e. public , private , protected , internal etc. because within an interface by default everything is public.
5.     An Interface member cannot be defined using the keyword static, virtual, abstract or sealed.

Abstract Class:-

We can not create an object of an abstract class and can call the method of abstract class with the help of class name only.
Example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{

   abstract
 class A
   
{
        
public int add(int a, int b)
        {
            
return (a + b);
        }
    }
    
class B :A
    {

        
public int Add (int a, int b)
        {
            
return a + b;
        }
    }
    
class test
    {

        
static void Main(string[] args)
        {
            
B ob = new ();
            
int result = ob.add(10, 20);
            
Console.WriteLine("the result is {0}", result);
            
Console.ReadLine();
        }
    }
}



Output:
The result is 30.

Interface:-
An Interface member can not contain code bodies.Properties are defined in an interface with the help of an access block get and set, which are permitted for the property.
      e.g.   Interface myInterface
                       {
                          int myint
                            {
                                 get;
                                 set;
                              }
                         }
Example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{

    interface
 MyInterface
    
{
      void
 myMethod();
    }

    class
 MyClass : MyInterface
    
{
      public
 static void Main()
      {

            MyClass
 cls = new MyClass();
            cls.myMethod();
      }

       public
 void myMethod()
      {
            
Console.WriteLine("welcome in neerajcodesolution.blogspot.in ");
            
Console.ReadLine();
       }
    }
}


Output:-
welcome in neerajcodesolution.blogspot.in
DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE DIFFERENCE BETWEEN AN ABSTRACT CLASS AND AN INTERFACE Reviewed by NEERAJ SRIVASTAVA on 3:54:00 PM Rating: 5

No comments:

Powered by Blogger.