Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class inheritance

 
Purnima Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following scenario:

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?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

You should read about abstract classes and see if that would work for you.....
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A user of the app, or a user of the API?
 
Purnima Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:A user of the app, or a user of the API?


Hi David, I mean a user of the API.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then Janeice's solution is one really easy way to accomplish this; factory methods and private constructors are another common way.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using an enum?
Then you have four possible values: Course.Cooking, Course.Sewing, Course.Writing and null.
 
Purnima Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Janeice,

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?

@Rob: Thanks for your reply.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes cannot be instantiated.

A user of the app itself can do only what the app allows them to--just don't let them create an invalid type.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Purnima Agarwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David and Janeice. That solves my problem now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic