Lesson 6Packages, Interfaces, and Encapsulation

A programming language is considered object-oriented if it supports inheritance, encapsulation, and polymorphism. You know by now that Java supports inheritance, which lets you design a class by deriving it from an existing one. This feature allows you to reuse existing code without copy-pasting code fragments from other classes. The class NJTax from Listing 3-5 was designed this way.

In Chapter 6 and Chapter 7 you learn what encapsulation means, and continue studying coding techniques and best practices. Although this lesson shows you several short code examples illustrating certain programming topics, the next one brings all the pieces you’ve learned so far together in one larger application.

Java Packages

A decent size project can have hundreds of Java classes, and you need to organize them in packages (think file directories). This will allow you to categorize files, control access to your classes (see the section “Access Levels” later in this chapter), and avoid potential naming conflicts: If both you and your colleague coincidentally decide to name a class Util, this won’t be a problem as long as these classes are located in different packages.

Sometimes you’ll be using third-party libraries of Java classes written in a different department or even outside your firm. To minimize the chances that package names will be the same, it’s common to use so-called reverse domain name notation. For example, if you work for a company ...

Get Java Programming 24-Hour Trainer, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.