We have a center which is running 3 types of courses: Cooking, Sewing and Writing. Now we want to make sure that our class hierarchy will forbid a user from creating a “generic” type of
course (i.e. not a cooking, or sewing or writing course).
Can anybody suggest that how should we implement this?
Thanks for the solution. But as far as I know (and I am sure I don't know much), that abstract classes are a way of creating 'generic' classes. Then how is it going to forbid a user of the API from creating one itself? I am a little confused here so can you explain what you meant there?
@David: What if the user here is the user of the application? Then how should we proceed?
An abstract class is a class that cannot be instantiated....
So the abstract superclass in your case might be 'Course' and the concrete subclasses might be 'Sewing' , 'Cooking' , et cetera.
This way, no one can instantiate a 'Course' , but all the classes ('Sewing' 'Cooking' 'Fishing' 'Basketweaving') can be members of the hierarchy and have similar qualities, and be kept in Collections together.
A very simple version is also the 'Animal' example. What do you get when you instantiate an Animal??
This is why you make 'Animal' abstract, and 'Dog', 'Cat' and 'Zebra' are concrete subclasses.
Joined: May 01, 2010
Thanks David and Janeice. That solves my problem now.