Jump to content

[SOLVED] Converting an instance of a class into a child class


vigge

Recommended Posts

I'm currently working class representations of some different filesystem objects. Currently I've got these pretty much done:

class pFileSystemObject {}

class pFolder extends pFileSystemObject {}

class pFile extends pFileSystemObject {}

class pImage extends pFile {}

 

Anyway, whenever an instance of the pFile class is created, I'm planning on having the constructor check what the extension for the passed file is and then convert itself into for example a pImage instance if the extension is jpg/gif/png/etc. Is this possible in some simple way or will I have to create a new pImage instance inside the pFile class, re-set all the attributes for it and then destroy the pFile instance?

 

Would I be doing this the wrong way? Should I check the extension before instancing the class and then create an instance of the appropiate one (I'm thinking this should be avoided since as much as the work of figuring out what type of file should be done inside the class itself)? Maybe using a Factory class would be the best choice?

 

I'm having trouble deciding this alone as I've just started with OOP, so any advice on this would be great.

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.