"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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users


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.

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

717 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