Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Designing a class for inheritance

>>>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
3 Solutions

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.

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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

gudii9Author Commented:
>>>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
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
java supports inheritence through subclassing
ie. to inherit a classes behaviour you subclass it

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now