Reusing the implementation | CodeGuru

Reusing the implementation

Bruce Eckel’s Thinking in Java Contents | Prev | Next the implementation Once a class has been created and tested, it should (ideally) represent a useful unit of code. It turns out that this reusability is not nearly so easy to achieve as many would hope; it takes experience and insight to achieve a good […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

the
implementation

Once


a class has been created and tested, it should (ideally) represent a useful


unit of code. It turns out that this reusability is not nearly so easy to


achieve as many would hope; it takes experience and insight to achieve a good


design. But once you have such a design, it begs to be reused. Code reuse is


arguably the greatest leverage that object-oriented programming languages


provide.

The


simplest way to reuse a class is to just use an object of that class directly,


but you can also place an object of that class inside a new class. We call this


“creating a member object.” Your new class can be made up of any


number and type of other objects, whatever is necessary to achieve the


functionality desired in your new class. This concept is called


composition

,


since you are composing a new class from existing classes. Sometimes


composition is referred to as a “has-a” relationship, as in


“a car has a trunk.”

Composition


comes with a great deal of flexibility. The


member
objects

of your new class are usually private, making them inaccessible to client


programmers using the class. This allows you to change those members without


disturbing existing client code. You can also change the member objects


at
run time

,


which provides great flexibility. Inheritance, which is described next, does


not have this flexibility since the compiler must place restrictions on classes


created with inheritance.

Because


inheritance is so important in object-oriented programming it is often highly


emphasized, and the new programmer can get the idea that inheritance should be


used everywhere. This can result in awkward and overcomplicated designs.


Instead, you should first look to composition when creating new classes, since


it is simpler and more flexible. If you take this approach, your designs will


stay cleaner. It will be reasonably obvious when you need inheritance.


Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.