Inherit seems to good to be true

Posted on 2006-05-25
Last Modified: 2010-04-23
Is there any downfall to inheriting a class instead of the class being passed around  in the New() sub
Question by:Hepen
    LVL 11

    Expert Comment

    It would depend on what you where planning on doing, you're question is a bit vague btw.  Lobbing objects around is all good and well, but at the end of the day it will make your code messy and harder to debug.  Encapsulating certain basic functions and core tasks in base classes which get inherited makes the code neater and in the long run easier to maintain, also there would probably be less memory usage at the end of the day although the difference would probably be negligible unless the developer habitually did not clean up after himself.  If say for example you had a base class which would save and retrieve certain settings in the registry, then the inheriting class using this base class decides it wants to write an extra setting to the registry can override the base class method and write the extra setting into the registry before invoking the base classes registry update.  Trying stuff like that without inheritance etc. gets really messy after a while.  The key IMHO is determining what needs to go into the base class's.
    LVL 34

    Expert Comment

    I'm not sure I understand the question.  But, so far as I do, the answer is "I don't think so".

    You have a class called Widget.  You want some Black Widgets and some White Widgets.

    Scenario A: you inherit from Widget into (1) BlackWidget and (2) WhiteWidget - hardcoding the appropriate, different color into each.

    Scenario B: you code the New() method of Widget to accept a color argument and call New(Black) when you want BlackWidgets and New(White) when you want WhiteWidgets

    Scenario C: you give your Widget class a Color property and, having created an instance of it with New you then call
       myWidget.Color = Black
       myWidget.Color = White

    Whichever way you do it, each instance of the class/es is a separate object.  So, at this level anyway, I can't see what sort of "downfall" there might be.

    What sort of thing did you have in mind?

    LVL 44

    Expert Comment

    Inheritance only makes sense when the DERIVED class (that which is INHERITING from the base class) has an "IS A" relationship to the base class.

    Consider a Base class MAMMAL and a derived Class DOG ( clear a DOG 'IS A' MAMMAL).   In this case, Inheritance makes a lot of sense, since much of the nature of the DOG class can be placed in the MAMMAL class, and only those aspects that make a DOG a DOG (and are different from say a PERSON - a PERSON is also a MAMMAL, but quite different froma DOG - for one thing , a PERSON has 2 legs, while a DOG has 4 legs).

    But it would make abosolutely no sense whatsoever to try to create a BATTLESHIP class that inherits from a MAMMAL class - clearly a BATTLESHIP has absolutley nothing in common with a MAMMAL.

    LVL 96

    Expert Comment

    by:Bob Learned
    Yeah, I'll jump on the bandwagon and say that this question doesn't make any sense :(

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I think he wants to access values from one class, inside another class, and has discovered that if you inherit from the first class then you can access them.

    The purpose of the question would then be for us to contrast that approach with simply passing a reference to the first class into the second and list advantages/disadvantages of each.
    LVL 96

    Expert Comment

    by:Bob Learned
    Hi, Mike, you must be a mind reader, or maybe you helped with a different question that would give you important insights here ;)


    Author Comment

    I have a class that performs asynchronous TCP.  Within those async TCP threads I have to use delegates in order to get data out of the thread and into my form.  I have so many delegates now that it is taking up a lot of space in my class.  Now I just made a seperate class called clsFormDelegates and I Inherit it when I need to use the delegates to get data from a thread into my form.

    Is this the correct usage of Inheriting?

    Author Comment

    What if I were to just create an instance of clsFormDelegates and call each method like:


    Or if I inherited


    If I inherit it looks as if the above methods are part of the class which could throw some of my team members off.  They will be like where did these methods come from? Oh you inherited them? I see.
    LVL 85

    Accepted Solution

    Take the Delegates out of the Class then...

    Just make the Class raise events that notify the GUI of what is happening and to pass data out.

    Let the FORM store and make the Delegates since it needs to update itself.

    See this ridiculous example I came up with here:

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now