"Is A" "Has A"

Posted on 2004-10-07
Last Modified: 2013-12-16
Just trying to get some hierachy sorted out, but could use some help.

Not sure where to use inheritance or composition.

I'm basically writing a wrapper for the COM object "Scripting.FileSystem" in ColdFusion (sorry there is no OO forum for ColdFusion ;-) but whatever I am writing it in does really not matter anyway.

File System Has A Drive
Drive Has A Folder
Sub-folder Is A Folder
Folder Has A File

Any thoughts on this?
Question by:Tacobell777
LVL 92

Expert Comment

ID: 12255740
> Sub-folder Is A Folder

why do you need special treatment for a sub-folder?
LVL 17

Author Comment

ID: 12255746
Sub-folder Is A Folder
File Has A Folder
Folder Has A Drive
File System Has A Drive
Drive Has A Folder

I'm lost... ...
LVL 17

Author Comment

ID: 12255748
I guess its one and the same object... hmmm

Expert Comment

ID: 12255752
It depends on who you ask, and when you ask them ;)

I don't think there is one definite answer to this.  An example:

public class A {
  public void someMethod() {
    // Some code here...

public class B extends A {
  public void anotherMethod() {
    // More code here..

Now, you could do this:

  A b = new B();

But, since b was created as an object of class A, you can't do this:


But, you can however do this:


Now, lets say you add this:

public class C {
  private A a = new A();

  public void someMethod() {

  public void yetAnotherMethod() {
    // More code here..

So, creating a C object goes like this:

  C c = new C();

If you do this:

  A c = new C(); // DOES NOT COMPILE

It simply won't compile!!  But, with an instance of c, you can still use both methods, like this:

  C c = new C();
  c.someMethod();  // This seems to work like before..

All of that would work.  The thing composition can NOT enable you to do is to pass your instances as an object of the type they contain and use, C is just NOT an A.  Another problem is that for every method A has you wish to expose and use in C, you have to write a method to invoke the composed method.

I once created an interesting project, I tried to see if I could use composition to create an object that actually inherited from TWO parent-objects.  It worked, but whenever I wanted to pass the child object as either one of its parent objects, I had to cast it myself!

So, in conclusion, use either where ever you need it, just remember that composition isn't inheritance, so casting your object won't even work (my project was a different issue, I did some tricks to get casting to work :)
LVL 17

Author Comment

ID: 12255753
However there is a lot of stuff that the sub folder object does and most of the time is not required by the Folder object..
LVL 17

Author Comment

ID: 12255819
but I need some ideas ;-)) I know what you are saying, but surely someone else must have something similiar before.
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.


Accepted Solution

RuadRauFlessa earned 500 total points
ID: 12256136
Why not ahve a handler class that represents your Filesystem and then keep everything else as simple as possilbe by going the unix routhe and call all of the rest a file? You can store everything you need to know about a folder in a file as well since a file hass all the same properties as a folder with the exception of having on disk data connected to it. For example user rights to the file or folder, size of file or folder and parent of file or folder. They all have everything in common. Remember that inherently a folder is actually a type of file that contains more files within it rather than an on  disk storage area.
LVL 21

Expert Comment

ID: 12257420
>File System Has A Drive
>Drive Has A Folder
>Sub-folder Is A Folder
>Folder Has A File

class FileSystem
    public Drive getDrive(String driveLetter){...}
    public Collection getDrives(){...}

public Drive
     *Return collection of folders on root of drive
   public Collection getFolders() {...}

public Folder extends File
   public Collection getSubFolders() {...}

public File /*Note this is a DIFFERENT object than*/

ColdFusions FileSystemObject is the same object used in VBScript.  Just go to any reference site (e.g. and copy the specification into the java classes.
All of the VB properties will have to be replaced by java get/set methods.  (e.g VB FileSystem object has Drives property your Java FileSystem will have a getDrives() method [no setDrives() because this is a readonly property]).  Each method would mostly just be a wrapper to the corresponding method in File object.  For Example:
public Collection getDrives()

     ArrayList drives=new ArrayList(roots.length);
     for(int i=0;i<roots.length;i++) {
         Drive drive=new Drive(roots[i]);
     return drives;

Expert Comment

ID: 12264424
>Not sure where to use inheritance or composition.

> File System Has A Drive
> Drive Has A Folder
> Sub-folder Is A Folder
> Folder Has A File

Well to me: All has-a are "Composition implementations" and All is-a are "Inheritance implementations"

Think of it in terms of set presentation:
                        |FileSystem                                                       |
                        |  ____________________________________      |
                        | |Drive(s)                                                   |     |
                        | |  _______________________________      |     |
                        | | |FOLDER                                          |     |     |
                        | | |  _____________                            |     |     |
                        | | | |Folder(s)  ____|__________         |     |     |
                        | | | |              |      |                  |        |     |     |
                        | | | |  file(s)    |      |    file(s)      |        |     |     |
                        | | | |              | X   |                  |        |     |     |
                        | | | |_____________|                 |       |     |     |
                        | | |                |     Subfolder(s)    |       |     |     |
                        | | |                |_______________|       |     |     |
                        | | |______________________________|     |     |
                        | |__________________________________ |     |

X = Common functionalities

From this diagram, we can deduct that:
_ a Filesystem has 1..n Drives
_ each drive has EXACTLY 1 FOLDER, interestingly if I've drawn Subfolder box within Folder box then we get Folder has 0..n subfolders which is TRUE. But such drawing would losing the meaning of subfolder is-a folder. To avoid this, I divided FOLDER into Folder(s) and Subfolder(s). So FOLDER is a special one-off instance of Folder like root of a tree for example.
_ FOLDER has 0..n Folders and 0..n Subfolders
_ Subfolders and Folders have common functionalities denoted by X, hence Subfolder is-a Folder.
_ Subfolders and Folders have 0..n files.

So what am I talking about? Your implementation must obey the above deduction.
LVL 14

Expert Comment

by:Tommy Braas
ID: 12264644
Let's simplify (and think Unix):

File System Has A Drive
Drive Has A File
Folder Is A File
Folder Has A File
File Is A File

Expert Comment

ID: 12274035
Now isn't that what I was saying orangehead911 and that whould be the correct way of handling things since Java does it that way as well. If you havent noticed there isn't such a thing as a Folder class in Java simply cuz the File class has everything you need for a folder pluss the extras that  makes up a file. So you see you can even represend a drive as a file sinc eyou would still reference it by it's name [Windows(C: or D: or E:)] NetWare[(SYS: DATA:)] [Linux(there is only one drive or filesystem on a unix machine and that is root / since all pysical drives get mounted to a logical naming structure and if you look at the source of Linux you would notice that even that logical system is mapped as a file)]

========== Off topic ==========
By the way orangehead911 do you by any chance own a orange 911 Porche? Just asking mate.

Expert Comment

ID: 12274868
Glad we could help.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
solarwind tftp server 2 44
eclipse formatting 6 72
servlet  URL Rewriting 1 27
MySQL  on Tomcat 8 27
In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now