Java Reference
In-Depth Information
When using an object‐oriented programming paradigm, objects encapsulate only local data, which
is by default accessible only by the object itself. Rather than having to think about data and code as
two separate concepts, an object‐oriented program merges the two in the concept of an object . This
increases understanding (analysts and programmers can consider objects of interest without inter-
nalizing the workings of the complete program) and ease of maintenance.
To realize the latter, object‐oriented programming applies two concepts known as encapsulation
and information hiding . One of the greatest sources of errors in programs is when some parts of the
program are interfering with other parts. Indeed, it is easy to see that, in this course administration
example, the addition of more procedures and data will quickly lead to so‐called spaghetti code,
where it becomes very complex to follow the trace of execution as data can jump from one part to
another in the program. Object‐oriented programming resolves this issue by encapsulating both
data and behavior within an object.
However, this in itself is not sufficient to guarantee maintainable programs, as you also
need to prevent an object's data from being directly accessible by other objects. Therefore,
object‐oriented programming also emphasizes the concept of information hiding , where an
object's data can by default be accessed only by methods contained in the same object. When
data elements of one object need to be used by another object, the latter must call a publicly
accessible method of the former, basically requesting the “owning object” to perform a change
to its data. As such, object‐oriented programming encourages programmers to place data where
it is not directly accessible or modifiable by the rest of the system. Instead, the data is accessible
through methods, which can also include checks and safeguards to make sure the requested
change is permitted by the owning object.
Object‐oriented programming also defines concepts to help with structuring programs so that
they can be easily extended and evolved. These concepts are polymorphism , which is the ability
to treat objects of different types in a similar manner, and inheritance , which is a concept to
allow for extending objects and enabling code reuse. You will revisit these concepts in more detail
in Chapter 8, when you delve deeper into object‐oriented concepts. For now, you'll see how the
object‐oriented concepts you have seen so far—the classes, objects, variables (data), and methods
(behavior)—are used in Java.
classes and oBjects in java
Now that you have gained knowledge on the basics of Java, it is time to move on to the topics that
make Java an object‐oriented language: classes and objects. The following sections will guide you
through the concept of a class, which serves as a declaration, or blueprint, for objects, which can be
instantiated from classes.
defining classes in java
As discussed in Chapter 2, Java is a “pure” object‐oriented programming language, meaning
that there are no standalone constants, variables, or functions. It is not possible to define such
standalone elements, and everything is thus accessed through classes and objects. Before version
5 of Java, primitive types (such as int and double ) were not represented as objects, a decision
made by Java's designers for performance reasons. Due to this, Java was not considered to be a
 
Search WWH ::




Custom Search