File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class inheritance" Watch "Class inheritance" New topic
Author

Class inheritance

Purnima Agarwal
Greenhorn

Joined: May 01, 2010
Posts: 4
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

Joined: Sep 14, 2009
Posts: 1665
    
  11

Welcome to JavaRanch!

You should read about abstract classes and see if that would work for you.....


When you do things right, people won't be sure you've done anything at all.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A user of the app, or a user of the API?
Purnima Agarwal
Greenhorn

Joined: May 01, 2010
Posts: 4
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

Joined: Sep 29, 2008
Posts: 12617

Then Janeice's solution is one really easy way to accomplish this; factory methods and private constructors are another common way.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

How about using an enum?
Then you have four possible values: Course.Cooking, Course.Sewing, Course.Writing and null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Purnima Agarwal
Greenhorn

Joined: May 01, 2010
Posts: 4
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 14, 2009
Posts: 1665
    
  11

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

Joined: May 01, 2010
Posts: 4
Thanks David and Janeice. That solves my problem now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class inheritance