• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

composition and inheritance

 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folder
|
|-Subfolder
|
|-File

I think answer B is correct.

B says

Folder has-a Subfolder.
Folder has-a file.

It looks appropriate.What's wrong in it ?

Although answer C says

FolderItem (superclass)

Folder is-a FolderItem (ok it is-a folder type)
File is-a FolderItem (how a file is-a folder type?)
Folder has-a FolderItem ? (didn't get Folder is-a FolderItem or Folder has-a FolderItem ?)

Could you explain me answer C ?
New-Bitmap-Image.JPG
[Thumbnail for New-Bitmap-Image.JPG]
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What C in describing is an approach where you have a (probably abstract) class FolderItem, which represents those things that you can find in a folder. There are two types of FolderItem (think of it as two concrete subclasses) - folders, and files. So both are types of FolderItem. But a folder can contain FolderItems (that's what it means, after all), so a folder has-a FolderItem.

You might want to search for information about the Composite pattern - this is a specific example of that pattern.

The problem with B is that it considers Folders and Subfolders as distinct types of object, but they are really the same thing.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meeta,

Implementation of option (c) in code:

Implementation of option (b) in code:

Do you notice any difference here, or advantage of one over the other?

With respect,
Mala
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mala
 
Mala Gupta
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meeta,

If you answer this question, it would help you to implement this design pattern in your real life projects:

Do you notice any difference here, or advantage of one over the other?


With respect,
Mala
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mala Gupta wrote:Meeta,

If you answer this question, it would help you to implement this design pattern in your real life projects:

Do you notice any difference here, or advantage of one over the other?


With respect,
Mala



Thank you to give me opportunity to explain.

I will use composite design pattern if i observe there is any formation of tree structure, where i can to take benefit to perform common operations on entire hierarchy.

Hence a file system is a tree structure, so i will use here composite design pattern.

Answer B was not correct implementation of this design pattern, answer A was correct implementation of this pattern.

to implement composite design pattern, i need component(interface/abstract class) where i will declare common operations.Client will interact thru that component only without knowing anything about any composite or a leaf as composite design pattern encourage program to interface not to implementation.

each leaf and composite must have is-a relationship with component.
and each composite must have has-a relationship with component.

Thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic