Why we choose interface and abstract class in C#

Posted by krrishbiju-15589 under C# on 11/22/2013 | Points: 10 | Views : 1303 | Status : [Member] | Replies : 6
Hi,
I know the concept of Abstract class and interface in c#.
But I want to know the design criteria for choosing interface and abstract class in C#.

Thanks and Regards
Krrish




Responses

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
If you wish to leave implementation to third party vendors we should go ahead with interfaces...
For suppose take database connection..
you can make connections with different databases by using same method names (only thing is we should pass different driver name for each database connection such as SQL Server, MySQL, and Oracle so on)

For example,
//interface should provide by Java APT
interface DBConnection
{
public string connstr ;
void ConnString(string connstr);
}

third party vendors should provide implementation part.. ( implementation will be differed but not the naming conventions and method names


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Design criteria of interfaces:
http://stackoverflow.com/questions/2026054/why-do-we-use-interface-is-it-only-for-standardization

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: krrishbiju-15589 on: 11/22/2013 [Member] Starter | Points: 25

Up
0
Down
Ok Chandu...
In your example in interface contain a string
public string connstr.
can we declare with public modifier


krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
In .NET we can't do directly we should declare it as property...
I'm thinking about java while writing that post.. I haven't much concerned about syntax...
Just tried to explain the scenario...

NOTE: In JAVA we can declare constants (static final variables) in interface..

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suchitrab on: 11/27/2013 [Member] Starter | Points: 25

Up
0
Down
Abstract Class may have partial implementation.
Interface has no implementation at all. Its a contract.

Abstract Class may have access modifiers for its fields, properties and methods.
Interface has no access modifier for its properties and methods. Default they are PUBLIC.

A class can inherit from a single (Abstract) class but
can implement many interfaces.

Hope these differences clarify the need of use.

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: t5j9033387989 on: 11/28/2013 [Member] Starter | Points: 25

Up
0
Down

Abstract classes can have only one parent class, whereas a class can implement several interfaces.

Interfaces cannot contain any implementation, abstract classes can (they can have abstract methods in addition to not abstract methods).


mark this answer if it will really help you,

Thanks&Regards
ketan

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response