Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to know your expert advice on interface/abstract class? A small real time example, Where you think interface/Abstract class makes sense. Please don't suggest me any link i want your views/ideas...

Please help :)
Posted

See
My answer
 
Share this answer
 
Comments
Sushil Mate 19-Mar-12 0:56am    
:)

Suppose your parents gives you a list of items to purchase, that list is an Interface that you will implement at time of purchasing..... Means Before implementing any thing , you list out what you have to done that will be interface......
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.)

1. Use an abstract class to define a common base class for a family of types.
2. Use an abstract class to provide default behavior.
3. 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.

1. Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
2. Use an interface to design a polymorphic hierarchy for value types.
3. 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.

for more refer:http://mycodelines.wordpress.com/2009/09/01/in-which-scenario-we-use-abstract-classes-and-interfaces/[^]
 
Share this answer
 
v4
SQL
Concider the following example.

    If you want to draw some figures by selecting the drawing tool. Then you can keep this drawing methods and data on abstract class. Then you can over ride the drawing method according to your tool selected in different classes. Because all the figures need the drawing function.
 
Share this answer
 
Interface and abstract class both are used to serve two purpose

1. Having the facility for objects to work in polymorphic fashion.
2. Some kind of contract is enforced on the objects( the interface primarily defines the contract)

Now the decision of using which is based on

If you have only contract that should be enforced you should use interfaces.

But if you have some default behavior along with the contract that the derived classes should/could use then you should use abstract classes.

But I will not suggest going for either/or, My suggestion would be

1. Have an interface defining only the contract.
2. define an abstract class that implements this interface and then defines the default behavior functions.
3. Then have the concrete classes use the abstract classes. (they may choose to override some methods).

more like

Interface <------AbstactClass <---------------ConcreteClass

Gurus, please correct me if i said something wrong here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900