ASP.NET - abstract class and interface

Asked By Vimal Sharma on 30-Nov-11 02:42 AM
What is abstract class and interface ? What is diffrence between them ? And in what instance we use abstract class and interaface . Please give some live exmple
Reena Jain replied to Vimal Sharma on 30-Nov-11 02:47 AM
Hi,

The most real time example of abstract class and interface is bulding a house ,you create a class say House and then another say Shop both extend Building but you decide to make Building as an abstact class as when you create an object of the building class you don't know which kin of object it will be; will it be house or shop so the program fails and you say ah it is good that this class is made an abstract as we shouldn't create an object of this class.

Interface is a 100 % pure Abstract class means it is a class which is 100 % abstract in nature i.e 100 % unknown what it does.

  • concrete methods are explained with completed house.
  • abstract classes are explained with completed house but a little bit of work left
  • interfaces are explained with taking a building plan
refer=>http://www.geekinterview.com/talk/5254-real-time-example-abstract-class-interface.html

Hope this will help you
dipa ahuja replied to Vimal Sharma on 30-Nov-11 02:50 AM
Abstract classes
An abstract method is implicitly a virtual method.
Abstract method declarations are only permitted in abstract classes.
Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ })
 
Interface:
Interfaces describe a group of related functionalities that can belong to any class or struct . You define an interface by using the interface keyword.
Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.
When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation. The derived class is said to implement the interface implicitly.
 
Difference :
 
(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 cannot implement methods.
(2)  An abstract class can contain fields, constructors, or destructors and implement properties. An interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.
(3)  An abstract class cannot support multiple inheritances, but an interface can support multiple inheritances. 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.
(6) Abstract classes are faster than interfaces.
 
 
 
s j replied to Vimal Sharma on 30-Nov-11 02:50 AM


Hi,
Try this . it is helpful for u.

http://forums.asp.net/t/1240708.aspx/1



Cheers,
Samantha.
Chintan Vaghela replied to Vimal Sharma on 30-Nov-11 02:51 AM

Description:

Abstract Classes: Classes which cannot be instantiated.

This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class.


Abstract classes contains have one or more abstarct methods, ie method body only no implementation.


Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation.


When to use wot depends on various reasons.

One being design choice. One reason for using abstarct classes is we can code common
functionality and force our developer to use it.

I can have a complete  class but I can still mark the class as abstract.

Developing by interface helps in object based communication.


Design View:


< form id="form1" runat="server" >
   < div >
     < h2 >Simple Partial Class Example:< /h2 >   
    < asp:Label ID="lblAbstract" runat="server" > </asp:Label >     
  < /div >   
 < /form >

Class Files

 public abstract class EmployeeSalary
 {
     public abstract decimal Salary
     {
     get;
     }
 
 }
 
 public class SalesClass1 : EmployeeSalary
 {
     public override decimal Salary
     {
     get
     {
       return 50000.23m;
     }
     }
 }

Code Behind

 protected void Page_Load(object sender, EventArgs e)
 {
    if (!IsPostBack)
    {    
    
     SalesClass1 objSalCls1 = new SalesClass1();
     lblAbstract.Text = objSalCls1.Salary.ToString();  
    }   
 }


Venkat K replied to Vimal Sharma on 30-Nov-11 02:51 AM
This is a common question which arises in many people,
here is an article which explains the difference, you will get a clear idea when to use both of them:
http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/


Hope this helps!
Jitendra Faye replied to Vimal Sharma on 30-Nov-11 03:27 AM

Interface:

–> If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.

For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.

–> If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.

For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .

Abstract Classes

–> When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.

–> The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

For example a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.

Use an abstract class

  • When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
  • 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.

Use an interface

  • When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
  • Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
  • Use an interface to design a polymorphic hierarchy for value types.
  • Use an interface when an immutable contract is really intended.
  • A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.
Suchit shah replied to Vimal Sharma on 30-Nov-11 04:15 AM

Abstract Class

These classes are either partially implemented, or not at all implemented.

  • This class can contain declarations and also implementations.
  • We cannot make instance of Abstract class. We must inherit to use this class.
  • A non-abstract class derived from an Abstract class must include implementations for all inherited abstract methods.
  • Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

Interface

Interface is a pure abstract entity:

  • It contains only the declaration.
  • We cannot make instance of an Interface. We must inherit to use Interface.
  • A class which implements the interface should implement all the members of an Interface.
  • Members of an Interface are always public.

Few Advantages

An advantage of Abstract class over Interface

If you want to have additional functionalities for the interface, you must implement the new ones in all of the classes that implement the changed interface. The code will not work until you make the change. But if you want to have additional functions to the Abstract class, you can have default implementation. Your code still might work without many changes.

An advantage of Interface over Abstract class

A class can inherit multiple interfaces but can inherit only one class.

Example of Abstract Class

abstract class WashingMachine
{
public WashingMachine()
{
// Code to initialize the class goes here.
}

abstract public void Wash();
abstract public void Rinse(int loadSize);
abstract public long Spin(int speed);
}

class MyWashingMachine : WashingMachine
{
public MyWashingMachine()
{
// Initialization code goes here.
}

override public void Wash()
{
// Wash code goes here.
}

override public void Rinse(int loadSize)
{
// Rinse code goes here.
}

override public long Spin(int speed)
{
// Spin code goes here.
}
}

Example of Interface

interface ISampleInterface
{
void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod() { // Method implementation.
}

static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();

// Call the member.
obj.SampleMethod();
}
}

Following are the differences between abstract and interfaces :-
√ Abstract classes can have concrete methods while interfaces have no methods implemented.
√ Interfaces do not come in inheriting chain, while abstract classes come in inheritance.
Hope it helps... and clear the understanding :)

Suchit shah replied to Vimal Sharma on 30-Nov-11 04:16 AM

Abstract Class

These classes are either partially implemented, or not at all implemented.

  • This class can contain declarations and also implementations.
  • We cannot make instance of Abstract class. We must inherit to use this class.
  • A non-abstract class derived from an Abstract class must include implementations for all inherited abstract methods.
  • Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

Interface

Interface is a pure abstract entity:

  • It contains only the declaration.
  • We cannot make instance of an Interface. We must inherit to use Interface.
  • A class which implements the interface should implement all the members of an Interface.
  • Members of an Interface are always public.

Few Advantages

An advantage of Abstract class over Interface

If you want to have additional functionalities for the interface, you must implement the new ones in all of the classes that implement the changed interface. The code will not work until you make the change. But if you want to have additional functions to the Abstract class, you can have default implementation. Your code still might work without many changes.

An advantage of Interface over Abstract class

A class can inherit multiple interfaces but can inherit only one class.

Example of Abstract Class

abstract class WashingMachine
{
public WashingMachine()
{
// Code to initialize the class goes here.
}

abstract public void Wash();
abstract public void Rinse(int loadSize);
abstract public long Spin(int speed);
}

class MyWashingMachine : WashingMachine
{
public MyWashingMachine()
{
// Initialization code goes here.
}

override public void Wash()
{
// Wash code goes here.
}

override public void Rinse(int loadSize)
{
// Rinse code goes here.
}

override public long Spin(int speed)
{
// Spin code goes here.
}
}

Example of Interface

interface ISampleInterface
{
void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod() { // Method implementation.
}

static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();

// Call the member.
obj.SampleMethod();
}
}

Following are the differences between abstract and interfaces :-
√ Abstract classes can have concrete methods while interfaces have no methods implemented.
√ Interfaces do not come in inheriting chain, while abstract classes come in inheritance.
Hope it helps... and clear the understanding :)

Riley K replied to Vimal Sharma on 30-Nov-11 04:33 AM

The choice of whether to design your functionality as an interface or an abstract class (a MustInherit class in Visual Basic) can sometimes be a difficult one.


 An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes.

An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.

Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument.
http://

Regards