C# .NET - interface,abstract class

Asked By sudula on 19-Nov-10 11:19 PM
what is difference between abstract class and interface ,please give one realtime example
Nowshad M replied to sudula on 19-Nov-10 11:21 PM
Hi,
Interfaces are closely related to abstract classes that have all members abstract.
 For an abstract class, at least one method of the class must be an abstract method that means it may have concrete methods.
 For an interface, all the methods must be abstract
 Class that implements an interface much provide concrete implementation of all the methods definition in an interface or else must be declare an abstract class
 In C#, multiple inheritance is possible only through implementation of multiple interfaces. Abstract class can only be derived once.
 An interface defines a contract and can only contains four entities viz methods, properties, events and indexes. An interface thus cannot contain constants, fields, operators, constructors, destructors, static constructors, or types.
 Also an interface cannot contain static members of any kind. The modifiers abstract, public, protected, internal, private, virtual, override is disallowed, as they make no sense in this context.
 Class members that implement the interface members must be publicly accessible.
For further reading check 
http://www.c-sharpcorner.com/Code/2003/Dec/OOPS%20In%20CSharp%201.0.asp
Nowshad M replied to sudula on 19-Nov-10 11:43 PM
Hi,
Abstract Class | Interfaces | 1. Starts with a class keyword | Starts with interface | keyword 2. abstract modifier has to be | No need to provide abstract provided | modifier (100 % Abstract) 3. Not necessary to contain | All methods are by default abstract methods, but if a | public and abstract, and class has abstract method | all attributes are public then class has 2 be abstract| static, and final (FIELDS) 4. Says what a class should be | Says how a class must like, behaves like a model | behaves and what it is | able to do 5. Therefore, Generally given | Thus, given names as class name as Nouns | Adjectives 6. Used for group of related | Any class can implement classes only | interfaces 7. Single inheritance is | Multiple inheritance can allowed only | be achieved 8. All class behaviours are | Cannot have constructors, allowed, except creating | should initialise its objects of abstract classes | Fields

Please refer to the following link for an example

http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx
Sirisha Reddy replied to sudula on 19-Nov-10 11:47 PM
Hii

1 An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
2 An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
3  An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
4  A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
5 Various access modifiers such as abstract, Protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces. 
6Abstract classes are faster than interfaces
EX:  //Abstarct Class

public abstract class Vehicles

      {

      private int noOfWheel;

      private string color;

      public abstract string Engine

      {  

        get;

        set;

      }

      public abstract void Accelerator();

      }

    //Interface

public interface Vehicles

      {

      string Engine

      {  

        get;

        set;

      }

      void Accelerator();

      }

 

 
Anoop S replied to sudula on 19-Nov-10 11:55 PM
Interfaces are essentially having all method prototypes no definition but Abstract class can contain method definations also.

In short Interface is a abstract class having all methods abstract.

Both abstract classes and interfaces are used when there is a difference in behaviour among the sub-types extending the abstract class or implementing the interface.

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.

If you create a abstract class writing the abstract keyword in the declaration part then You only can inherit the class. You can not create an instance of this abstract class but can inherit the class and with creating the instance of the derived class you can access the method of the abstract class.

If you use a virtual keyword in a method then you can override this method in the subclass if you wish..

If you create a abstract method then you must override this method in the subclass other wise it shows error in the program.


the most real time example of abstract class and interface is bulding a house
1)concrete methods are explained with completed house.
2)abstract classes are explained with completed house but a little bit of work left
3)interfaces are explained with taking a building plan