ClanKiller.com
http://forums.clankiller.com/

Using OO Classes :: instantiate or inherit?
http://forums.clankiller.com/viewtopic.php?f=24&t=2384
Page 1 of 1

Author:  Satis [ Thu Jun 14, 2007 7:55 am ]
Post subject:  Using OO Classes :: instantiate or inherit?

So...just curious... for the people that OO here, do you prefer instantiating external classes to use them, or just inherit them? To be a little more specific, take a general tools class that just holds various utility methods, like database abstraction stuff, input sanitizing methods, stuff like that.

Building your actual page/application classes, would you just have your class extend the tool class, thereby giving direct access to it (well, protected and public methods/properties, at least), or rather instantiate it?

C# inherit
Code:
 public partial class myPageClass : toolsclass
{
    public myPageClass()
    {
        databaseaccess("SELECT * FROM table");
        while (dr.Read())
        {
            //blah, iterate through results, whatever
        }
    }
}


C# instantiated
Code:
public partial class myPageClass : System.Web.UI.Page
{
    protected void Page_Load()
    {
        tools mytools = new tools();
        mytools.databaseaccess("SELECT * FROM table");
        OleDbDataReader dr = mytools.dr;
        while (dr.Read())
        {
            //yea, iterate and all that
        }
    }
}



especially if you're using the tools alot, it seems like it would be less efficient to keep instantiating and destroying objects rather than just inheriting.

Author:  Peltz [ Thu Jun 14, 2007 9:08 am ]
Post subject: 

ooooook, right back at ya :D



(i have no idea what the hell you are talking about so ignore this post)

Author:  RB [ Thu Jun 14, 2007 10:16 am ]
Post subject: 

If you are extending functionality of some class, then you should inherit. For instance, you have class SimpleMath and you want to write class Analysis1 BUT you don't want to write SimpleMath inside again. Then you'll do:

Say, Java:
Code:
public class Analysis extends SimpleMath {
  // ...
}
or C++:
Code:
class Physics : SimpleMath, SI, WhateverElseNeeded {
  // ...
};


On the other hand, if the class you are writing rather just uses the functionality of the class(es) you have, you should instance. For instance:

Say C++:
Code:
class My3DEngine {
  private:
    MySoundDevice *pSoundDevice;
    MyDisplay *pDisplay;
    // ...

  public:
    My3DEngine()
    {
      // initialization
    }
 
    // ...
};

Author:  Pig [ Thu Jun 14, 2007 11:43 am ]
Post subject: 

You should only inherit if the consuming class is a derivation of the class you are inheriting. So if you have a class of "automobile", you could have an inherited class of "truck". You would not want to inherit a class of "database" into a class of "truck".

If the class is not a derivation or extension of a parent class, then you want to use the class directly. I don't know C#, but you don't always have to inherit. A database class would most likely require instantiating because of opening database connections, etc. Other classes you can call statically.

instantiated:
class db_class{...}
$db_object = new db_class();
$db_object -> method();

static:
class translate_class{...}
translate_class::method();

So for your database example, you would need to instantiate the object for querying and such, however you could still call certain methods statically that do not require a database connection (and thus instantiating). For example, query() would require instantiating an object, but escape_text() might not.

Author:  Satis [ Fri Jun 15, 2007 6:48 am ]
Post subject: 

looks like it's unanimous (not including Peltz's useful comments :P ). Instantiating it is. I know in PHP you can use a class statically... I don't think that option's available in c#. Thanks again for the input.

Long time no see Pig. Asking OO questions usually gets you out of the closet. :roll:

Author:  Pig [ Fri Jun 15, 2007 9:06 am ]
Post subject: 

I am a lurker.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/