C# .NET - abstract ,interface - Asked By sudula on 19-Nov-10 11:34 PM

what is main difference between interface and abstract class please give me one example
Anoop S replied to sudula on 19-Nov-10 11:50 PM
Abstract :
=========================================
1. Abstract class cannot be instantiated.
2. Abstract class may contain abstract methods and accessors.
3. Abstract modifier can be used with classes, methods and properties.
4. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.
5. Abstract method declarations are only permitted in abstract classes.
6. You can inherit abstract classes to other class.

abstract class MyBaseC   // Abstract class
{
   protected int x = 100;
   protected int y = 150;
   public abstract void MyMethod();   // Abstract method
 
   public abstract int GetX   // Abstract property
   {
    get;
   }
 
   public abstract int GetY   // Abstract property
   {
    get;
   }
}
 
class MyDerivedC: MyBaseC
{
   public override void MyMethod()
   {
    x++;
    y++;  
   }  
 
   public override int GetX   // overriding property
   {
    get
    {
     return x+10;
    }
   }
 
   public override int GetY   // overriding property
   {
    get
    {
     return y+10;
    }
   }
 
   public static void Main()
   {
    MyDerivedC mC = new MyDerivedC();
    mC.MyMethod();
    Console.WriteLine("x = {0}, y = {1}", mC.GetX, mC.GetY);   
   }
}


Interface :
=========================================
1.Interfaces are similar to classes.
2.They can have member properties and methods.
3.All the properties and methods of interfaces are abstract.
4.They have no body, just the declaration.
5.Public, Protected, Private, Friend, Shared, Overrides, MustOverride, NotOverridable are permitted inside an interface.
6. You cannot inherit an interface to a class.

interface IEquatable<T>
{
  bool Equals(T obj);
}
 
public class Car : IEquatable<Car>
{
  public string Make {get; set;}
  public string Model { get; set; }
  public string Year { get; set; }
 
  // Implementation of IEquatable<T> interface
  public bool Equals(Car car)
  {
    if (this.Make == car.Make &&
      this.Model == car.Model &&
      this.Year == car.Year)
    {
      return true;
    }
    else
      return false;
  }
}
Sreekumar P replied to sudula on 19-Nov-10 11:54 PM
A good way to distinguish between a case for the one or the other for me has always been the following:

1. Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class)
Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).

2. What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs.
Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.


As said by someone else's reply: the main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface.
By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.




http://en.csharp-online.net/Interfaces_and_Abstract_Classes

http://www.codeproject.com/csharp/abstractsvsinterfaces.asp

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1118027&SiteID=1

kausumi nanavati replied to sudula on 20-Nov-10 12:07 AM
hello mem!

Main difference of abstract class and interface is

    Interfaces are limited to public methods and constants with no implementation. when
    Abstract classes can have a partial implementation, protected parts, static methods, etc.
kausumi nanavati replied to kausumi nanavati on 20-Nov-10 12:47 AM
 example of abstract class:

 

using System;

abstract public class Window

{

// constructor takes two integers to

// fix location on the console

public Window(int top, int left)

{

this.top = top;

this.left = left;

}

// simulates drawing the window

// notice: no implementation

abstract public void DrawWindow( );

// these members are private and thus invisible

// to derived class methods. We'll examine this

// later in the chapter

protected int top;

protected int left;

}

// ListBox derives from Window

public class ListBox : Window

{

// constructor adds a parameter

public ListBox(

int top,

int left,

string contents):

base(top, left) // call base constructor

{

listBoxContents = contents;

}

// an overridden version implementing the

// abstract method

public override void DrawWindow( )

{

 

Console.WriteLine ("Writing string to the listbox: {0}",

listBoxContents);

}

private string listBoxContents; // new member variable

}

public class Button : Window

{

public Button(

int top,

int left):

base(top, left)

{

}

// implement the abstract method

public override void DrawWindow( )

{

Console.WriteLine("Drawing a button at {0}, {1}\n",

top, left);

}

}

public class Tester

{

static void Main( )

{

Window[] winArray = new Window[3];

winArray[0] = new ListBox(1,2,"First List Box");

winArray[1] = new ListBox(3,4,"Second List Box");

winArray[2] = new Button(5,6);

for (int i = 0;i < 3; i++)

{

winArray[i].DrawWindow( );

}

}

}

kausumi nanavati replied to kausumi nanavati on 20-Nov-10 12:51 AM
example of iterface:

using System;

// declare the interface

interface IStorable

{

// no access modifiers, methods are public

// no implmentation

void Read( );

void Write(object obj);

int Status { get; set; }

}

// create a class which implements the IStorable interface

public class Document : IStorable

{

public Document(string s)

{

Console.WriteLine("Creating document with: {0}", s);

}

// implement the Read method

public void Read( )

{

Console.WriteLine(

"Implementing the Read Method for IStorable");

}

// implement the Write method

public void Write(object o)

{

Console.WriteLine(

"Implementing the Write Method for IStorable");

}

// implement the property

public int Status

{

get

{

return status;

}

set

{

status = value;

}

}

// store the value for the property

private int status = 0;

}

// Take our interface out for a spin

public class Tester

{

static void Main( )

{

// access the methods in the Document object

Document doc = new Document("Test Document");

doc.Status = -1;

doc.Read( );

Console.WriteLine("Document Status: {0}", doc.Status);

// cast to an interface and use the interface

IStorable isDoc = (IStorable) doc;

isDoc.Status = 0;

isDoc.Read( );

Console.WriteLine("IStorable Status: {0}", isDoc.Status);

}

}

Output:

Creating document with: Test Document

Implementing the Read Method for IStorable

Document Status: -1

Implementing the Read Method for IStorable