Encapsulation
Encapsulation is a technique that encourages abstraction by purposefully hiding
information. For example, the mechanical
details of a car engine are
encapsulated behind a steering wheel and floor pedals. Anyone familiar with this
interface could drive a car without knowing what type of engine was under the
hood. Java encourages encapsulation through
the use of interfaces and
by providing access modifiers that limit the visibility of classes, fields,
and methods.
Polymorphism
Polymorphism is a technique that encourages abstraction by allowing an entity
to assume multiple forms. For example, a smartphone is polymorphic because
it can
assume the role of a camera, web browser, music player, or digital clock.
Each application exposes a relevant interface to the user. In Java,
an object can
take on the form of any parent in its hierarchy or any interface in its hierarchy.
Mutability
Mutability refers to the ability of an entity to change its state. An iPod is an
example of a mutable entity because its contents frequently change. A vinyl
record is an example of an immutable entity
because its contents are
permanently engraved. Immutable objects provide numerous benefits in software
applications, such as stability and thread safety. Strings and all of the
primitive wrapper objects are examples of immutable objects.
In order to make
an object immutable, the class must be final,
all fields must be final, and it
cannot expose any mutable fields or methods that modify mutable fields.