Click here to Skip to main content
16,013,747 members
Home / Discussions / C#
   

C#

 
AnswerRe: Mdi Form Pin
tgurlevik29-Oct-11 2:43
tgurlevik29-Oct-11 2:43 
AnswerRe: Mdi Form Pin
Dave Kreskowiak29-Oct-11 3:15
mveDave Kreskowiak29-Oct-11 3:15 
QuestionCasting to Abstract 'parent' rather than casting to Interface ? Pin
BillWoodruff28-Oct-11 22:31
professionalBillWoodruff28-Oct-11 22:31 
AnswerRe: Casting to Abstract 'parent' rather than casting to Interface ? PinPopular
Luc Pattyn29-Oct-11 1:37
sitebuilderLuc Pattyn29-Oct-11 1:37 
GeneralRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
Not Active29-Oct-11 5:17
mentorNot Active29-Oct-11 5:17 
GeneralRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
Luc Pattyn29-Oct-11 5:45
sitebuilderLuc Pattyn29-Oct-11 5:45 
GeneralRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
BillWoodruff29-Oct-11 16:26
professionalBillWoodruff29-Oct-11 16:26 
AnswerRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
Luc Pattyn29-Oct-11 17:40
sitebuilderLuc Pattyn29-Oct-11 17:40 
Hi Bill,

I agree with what you said. And I'll phrase things differently once more:

an interface indeed is a contract: IWalk could be defined as "offers a public Walk() method". And that is all it is, there is no data defined, and no executable code whatsoever: no static methods, no instance methods. So it is just a spec for the implementer. And it can hide (or ignore) a lot; one of the nicest examples is IEnumerable (generic or not), as is required by foreach. Without knowing anything about your collection, foreach can enumerate all elements of your collection, whether an array, an ArrayList, a List<T>, a string of characters, etc.

A base class, abstract or not, can hold both data members and executable code; so it can provide real functionality (actual methods) as well as helper methods, etc.

An abstract class is a more restricted base class, in that the abstract methods must be replaced (overridden) by its inheritors.

So if you want Monkey and Lion classes, they could inherit from Animal. If you don't ever want to see a mere instance of Animal, then make that class abstract; if you want, or don't mind, a new Animal(), then don't make it abstract.

If Monkey and Lion were to be able to Hunt(), while deriving from an abstract Animal class, you would have to have each of them provide their own Hunt() method, which could rely on some shared methods in the base class.


In summary:

1. a class that inherits from a base class emphasizes the "is a" relationship, and therefore was willing to devote the single inheritance right it has to express that; it may or may not re-implement known behavior.

2. s class that inherits from an abstract class emphasizes the "is a" relationship, but also prevents the creation of non-specialized objects; the price to pay is you need to provide the specialization code in each of the descendants.

3. a class that implements an interface emphasizes the "knows how to" relationship, it does not exhaust the inheritance rights, however it must implement everything it promises it knows how to do.


More comments:

1. the set of available choices may not be perfect, but it is adequate. One could even live without abstract, leaving it out of the language spec (or the app) just opens the door to possibly unwanted situations (e.g. unspecified Animals); .NET would not be viable without interface.

2. every class can implement any number of interfaces, see e.g. the List<T> class which implements a lot of them.

3. typically the API of an interface is rather small, whereas the API of a class could be very big. There is an OO guideline that says something like: try to describe your interfacing specs in many, small interfaces, rather than in few, big interfaces; obviously nobody wants them so small that they become useless on their own, e.g. with collection-like functionality Add() and Remove() would normally be part of the same interface.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

AnswerRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
PIEBALDconsult29-Oct-11 4:27
mvePIEBALDconsult29-Oct-11 4:27 
GeneralRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
BillWoodruff29-Oct-11 16:30
professionalBillWoodruff29-Oct-11 16:30 
GeneralRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
PIEBALDconsult29-Oct-11 17:28
mvePIEBALDconsult29-Oct-11 17:28 
AnswerRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
Abhinav S29-Oct-11 17:46
Abhinav S29-Oct-11 17:46 
AnswerRe: Casting to Abstract 'parent' rather than casting to Interface ? Pin
BobJanova30-Oct-11 23:36
BobJanova30-Oct-11 23:36 
QuestionC# linq working with list controls Pin
classy_dog28-Oct-11 14:33
classy_dog28-Oct-11 14:33 
AnswerRe: C# linq working with list controls Pin
BillWoodruff28-Oct-11 14:59
professionalBillWoodruff28-Oct-11 14:59 
GeneralRe: C# linq working with list controls Pin
classy_dog28-Oct-11 17:02
classy_dog28-Oct-11 17:02 
GeneralRe: C# linq working with list controls Pin
BillWoodruff28-Oct-11 18:57
professionalBillWoodruff28-Oct-11 18:57 
QuestionWhat does this code segment mean?? Pin
Goalie3528-Oct-11 11:01
Goalie3528-Oct-11 11:01 
AnswerRe: What does this code segment mean?? Pin
harold aptroot28-Oct-11 11:17
harold aptroot28-Oct-11 11:17 
QuestionBitconverter.GetBytes() issue Pin
Blubbo28-Oct-11 8:16
Blubbo28-Oct-11 8:16 
AnswerRe: Bitconverter.GetBytes() issue Pin
Mark Salsbery28-Oct-11 8:36
Mark Salsbery28-Oct-11 8:36 
AnswerRe: Bitconverter.GetBytes() issue Pin
Dennis E White28-Oct-11 9:40
professionalDennis E White28-Oct-11 9:40 
AnswerRe: Bitconverter.GetBytes() issue Pin
harold aptroot28-Oct-11 9:42
harold aptroot28-Oct-11 9:42 
QuestionWriting plugin for IE Pin
johnsson7727-Oct-11 19:44
johnsson7727-Oct-11 19:44 
AnswerRe: Writing plugin for IE Pin
Richard MacCutchan27-Oct-11 23:23
mveRichard MacCutchan27-Oct-11 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.