Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Everyone,
I would like to know, what is the advantage of implementing these elements in your classes,viz,interface and abstract classes.
Im trying to find out a concrete reason why should i used them instead of just go straight and implement my classes..
Mninawa
Posted

Mninawa,

Generally, interfaces define the interface of a set of classes, but provide no detail of how that implementation is realised. This purpose is commonly emphasised by name, such as IComparable and IEnumerable.

A class implements an interface by providing methods that implement all the methods in the interface. A class may implement multiple interfaces.

By contrast, an abstract class is a base class of another class - it may have realised members as well as abstract members. They are commonly used as part of a framework, where they provide a pattern of behaviour that is expected to be realised by a concrete class deriving from it.

For example, a class may provide the basic support for putting a window on the screen, but provide abstract or virtual methods such as Draw, that should be overridden by derived classes to customise the behaviour to a particular situation.

A class may only inherit from one base class however (in C#, Java, etc.).

Like Nishant, however, I'd strongly recommend learning more about this - the GOF Design Patterns book for example gives plenty of examples of when each may be appropriate. Design Patterns[^]
 
Share this answer
 
Comments
Mninawa 21-Feb-11 12:29pm    
atleast you didnt critise,you just recomended a solution,thus there was a gap in my skills that needed to be fixed.most of the guys who mentored me as a junior had never used these elements,may be it was the level or the kind of project they were working on,it never required to implement interface and abstract.. hence now i will have to go and learn design patterns..
If you have no need to, then you should not use interfaces or abstract base classes merely because you want to. They have specific uses and unless your scenario fits those usage patterns, using them would actually be bad design.

Also read http://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx[^]
 
Share this answer
 
v2
Comments
Mninawa 21-Feb-11 11:33am    
Nishant, im doing entity framework and i came across this book and most examples are done using interface first. Also at work i have been maintaining this app written in .Net 1.1,they have interface for each layer.. thus your BLL,DAL and Previously most of the senior designer i have worked with never uses interface. This has bothered me and soon i will have to design system,hence i need to know the do and donts..
Nish Nishant 21-Feb-11 11:35am    
Your best approach would be to read up on common design patterns and approaches, if possible from a .NET perspective. You'll quickly be able to see some actual practical uses of interfaces.
#realJSOP 21-Feb-11 11:55am    
That's scary - you're doing EF and you don't have a grasp as to the basic differences between elemental aspects of .Net programming?
Mninawa 21-Feb-11 12:15pm    
Hi John, please dont get me wrong,i know the difference between the two and i have used them before.My question is, is it a mendate to have them? For example in sql there was a GOTO and most programmers dont use that.At work most of the senior developer i know dont use these elements above and its only those application that were written in .Net 1.1 that have..

I would always implement them when i do my code,now i have a power to decide how code in a team,but still i dont want to outshine my senior architect....
Yusuf 21-Feb-11 11:38am    
Good point. propose as 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