Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I have a problem that I'm not sure of the best approach to take. I have a base class that has two sub-classes that inherit from it:
[base] / \ / \ [subclass A] [subclass B]
B has similar functionality to A. A returns some data, B writes the data to a file, but in a different format than the data returned from A. Under some circumstances, the data written to the file from B is in the exact same format as the data returned from A.

However, I don't want B to inherit A's methods as I don't want the user accessing them from the object. In other words, I might have

my $object = A->new; $object->someWeirdMethod;
What I don't want is
my $object = B->new; $object->someWeirdMethod;
But that happens if I inherit from A, unless I write a bunch of methods to trap those calls and let the user know they shouldn't do that.

The alternative seems to be having B instantiate an A object, but having subclasses instatiating instances of each other strikes me as problematic. I keep up that habit and I might start winding up with circular references that I'll have to explicitly destroy.

Summary: two options that I see:

  1. Have B inherit from A, but don't let people call A's methods from B.
  2. Have B instantiate an instance of A. I like this better, but somehow it feels wrong.
Any pointers? I don't do a lot of OO programming and I'd appreciate feedback as to the best approach to take.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to OO-style question by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2014-12-21 16:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (106 votes), past polls