It is currently Thu Mar 28, 2024 5:36 pm



Reply to topic  [ 6 posts ] 
Using OO Classes :: instantiate or inherit? 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 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.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu Jun 14, 2007 7:55 am
Profile WWW
Stranger
User avatar

Joined: Sat Apr 12, 2003 1:14 pm
Posts: 6295
Location: Estonia
Reply with quote
Post 
ooooook, right back at ya :D



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

_________________
When someone asks how rich you are, quote Rinox " I don't even have a rusty nail to scratch my butt with...!"

Be well or Get Help!!


Thu Jun 14, 2007 9:08 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
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
    }
 
    // ...
};

_________________
++


Thu Jun 14, 2007 10:16 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
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.


Thu Jun 14, 2007 11:43 am
Profile YIM WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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:

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Fri Jun 15, 2007 6:48 am
Profile WWW
Duke
User avatar

Joined: Mon Mar 31, 2003 8:59 am
Posts: 1358
Location: right behind you
Reply with quote
Post 
I am a lurker.


Fri Jun 15, 2007 9:06 am
Profile YIM WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.