01.Blogs :
RobMiles  
Programming, gadgets and life as a lecturer in a UK university.

Interfaces and Abstract Classes. And why not?

Been teaching more programming. Today we did (amongst other things) interfaces and abstract classes. People have problems with these. Here my technique. There are just two things you need to remember here:

Product is a great name for an abstract class.

IDoEdit is a great name for an iterface.

If you can get these two lines into your head you can figure out everything else from scratch. Abstract classes serve as templates for the "real" classes underneath them. You'll never make an instance of a class called Product. You might make an instance of a class called ToothBrush, or BusStop, but you'll never make one called Product.

IDoEdit is something that any class can do. If they implement the IDoEdit interface they can be asked to do the edit thing. And a class which implements the IDoEdit interface can be thought of purely in terms of this ability and nothing else. The C# convention is to put an I in front of interface names.

So if you remember that the one that begins with I (and contains I too) is the interface and the other one is Product, you will do fine. I'm a great believer in getting the context of the thing sorted so that you can "figure out" what it does, rather than trying to remember a bunch of stuff.

posted on Friday, April 08, 2005 9:13 PM by RobMiles

# @ Saturday, April 09, 2005 8:17 AM

Great Stuff.. Especially defining the Product as abstract is just so perfect and so easy to get head around. Hope to see some more of these sort of lessons in future.

agurha

# @ Saturday, April 09, 2005 9:03 AM

I also use abstract for classes that only contain static variables. I don't know if this is the correct use but it makes it tidier by cutting out the constructor.

Pandilex

# @ Saturday, April 09, 2005 9:05 AM

Not sure how abstract cuts out the constructor. It makes sure that nobody ever makes an instance of it though. If the class just contains constants and settings it makes sense though. If you want some more of this kind of stuff take a look at www.csharpcourse.com where you can find my teaching wiki.

RobMiles

# @ Saturday, April 09, 2005 10:06 PM

Eh, I'm not wild about C# naming conventions. Give me CamelCase any day. And I prefer to name interfaces as adjectives and classes as nouns. So, Product is a good abstract class name, but how about Editable instead of IDoEdit? Interfaces describe what an object does, while classes describe what an object is. Since classes are nouns, having their descriptors be adjectives makes sense to me.

Porphyre

# @ Sunday, April 10, 2005 6:33 AM

Because the 'I' for interface is also used as a pronoun. I'm not sure about Rob's example but IDrawable and ICanReceiveNetworkClientMessages are two interfaces I use. And while one is very long, it states what it does. Perhaps that isn't recommended, but IDrawable just means "I am Drawable".

Pandilex


 
03.UPDATE CALENDAR :
<April 2005>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR