6: Reusing classes | CodeGuru

6: Reusing classes

Bruce Eckel’s Thinking in Java Contents | Prev | Next One of the most compelling features about Java is code reuse. But to be revolutionary, you’ve got to be able to do a lot more than copy code and change it. That’s the approach used in procedural languages like C, and it hasn’t worked very […]

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

One


of the most compelling features about Java is code reuse

.
But to be revolutionary, you’ve got to be able to do a lot more than copy
code and change it.

That’s


the approach used in procedural languages like C, and it hasn’t worked


very well. Like everything in Java, the solution revolves around the class. You


reuse code by creating new classes, but instead of creating them from scratch,


you use existing classes that someone has already built and debugged.

The


trick is to use the classes without soiling the existing code. In this chapter


you’ll see two ways to accomplish this. The first is quite


straightforward: You simply create objects of your existing class inside the


new class. This is called


composition
because
the new class is composed of objects of existing classes. You’re simply
reusing the functionality of the code, not its form.

The


second approach is more subtle. It creates a new class as a


type
of

an existing class. You literally take the form of the existing class and add


code to it without modifying the existing class. This magical act is called


inheritance

,


and the compiler does most of the work. Inheritance is one of the cornerstones


of object-oriented programming and has additional implications that will be


explored in the next chapter.

It


turns out that much of the syntax and behavior are similar for both composition


and inheritance (which makes sense because they are both ways of making new


types from existing types). In this chapter, you’ll learn about these


code reuse mechanisms.


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.