Designing a class for inheritance

Posted on 2011-04-25
Last Modified: 2012-05-11
>>>Designing a class for inheritance places substantial limitations on the class.

I have not clearly understood this statement, concept.

Please advise. Any ideas, sugestions, sample code, links, resources highly appreciated. thanks in advan
Question by:gudii9
    LVL 47

    Accepted Solution


    If you want to know the answer to that question,
    read pages 88-91 from this book (hope you'll see them freely - at least they show up for me):

    They decribe in detail what you should and should not do if you are writing the class which
    other programmers are supposed to subclass.

    This of course implies rather big projects where you are creating the code which will be used by wide range of programmers, say
    when you are writing some public libraries.
    If you are writing a short program for yourself, you may be less concerned with such issues.

    LVL 92

    Expert Comment

    LVL 26

    Assisted Solution

    The basic idea is that if you write a class that is NOT designed for inheritance, you are free to change the internal workings of the class at any time.  Any data or methods that are not public can be changed and any user of the class won't know the difference.

    However if the class IS designed to support inheritance, there's a much tighter relationship between it and any classes derived from it.  We like to say it's much more "tightly coupled".  This means it's much harder to design the class correctly in the first place as a lot more of the details of the class will affect how the subclasses will behave.  If you make a mistake in the original design, you may not be able to correct it later - so as the quote says there are a lot more limitations on the design of the class, than for one where the class won't be inherited.  You're not as free in the design of the class as you would be if nothing would derive from it.

    LVL 7

    Author Comment

    >>>Designing a class for inheritance places substantial limitations

    do they mean

    >>>Designing a class for subclassing (by extends not implements by inheritance) places substantial limitations

    >>>However if the class IS designed to support inheritance, there's a much tighter relationship between it and any classes derived from it.  We like to say it's much more "tightly coupled".

    Inheritance supposed to avoid tighter relationship/coupling right than subclassing. please advise
    LVL 47

    Expert Comment

    No, designing for inheritance and designing for subclassing is the same thing
    Thoese are just different terms but mean the same - if you are writing the class, which you believe
    at some point other people will want to write class which will extend your class (inherit from your class, subclass your class-
    all the same terms) , then youn want
    to follow these recommendations/guidelines.
    It is all the same - "the class which supports inheritance" still the same meaning - you believe, that someone will want
    to write another class which extends your class, then follow the above discussed points
    LVL 92

    Assisted Solution

    java supports inheritence through subclassing
    ie. to inherit a classes behaviour you subclass it

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now