Java Reference
In-Depth Information
All of the above characteristics are achieved during decomposition of a problem (or program that solves a
problem) using a process called abstraction. What is abstraction? Abstraction is a way to perform decomposition of a
problem by focusing on relevant details and ignoring the irrelevant details about it in a particular context. Note that
no details about a problem are irrelevant. In other words, every detail about a problem is relevant. However, some
details may be relevant in one context and some in another. It is important to note that it is the “context” that decides
what details are relevant and what are irrelevant. For example, consider the problem of designing and developing a
keyboard. For a user's perspective, a keyboard consists of keys that can be pressed and released to type text. Number,
type, size, and position of keys are the only details that are relevant to the users of a keyboard. However, keys are not
the only details about a keyboard. A keyboard has an electronic circuit and it is connected to a computer. A lot of
things occur inside the keyboard and the computer when a user presses a key. The internal workings of a keyboard are
relevant for keyboard designers and manufactures. However, they are irrelevant to the users of a keyboard. You can
say that different users have different views of the same thing in different contexts. What details about the thing are
relevant and what are irrelevant depends on the user and the context.
Abstraction is about considering details that are necessary to view the problem in the way that is appropriate in
a particular context and ignoring (hiding or suppressing or forgetting) the details that are unnecessary. Terms like
“hiding” and “suppressing” in the context of abstraction may be misleading. These terms may mean hiding some
details of a problem. Abstraction is concerned with which details of a thing should be considered and which should
not for a particular purpose. It does imply hiding of the details. How things are hidden is another concept called
information hiding, which is discussed in the following section.
The term “abstraction” is used to mean one of the two things: a process or an entity. As a process, it is a technique
to extract relevant details about a problem and ignore the irrelevant details. As an entity, it is a particular view of a
problem that considers some relevant details and ignores the irrelevant details.
Abstraction for Hiding Complexities
Let's discuss the application of abstraction in real-world programming. Suppose you want to write a program that will
compute the sum of all integers between two integers. Suppose you want to compute the sum of all integers between
10 and 20. You can write the program as follows. Do not worry if you do not understand the syntax used in programs
in this section; just try to grasp the big picture of how abstraction is used to decompose a program.
int sum = 0;
int counter = 10;
while(counter <= 20) {
sum = sum + counter;
counter = counter + 1;
}
System.out.println(sum);
This snippet of code will add 10 + 11 + 12 + ... + 20 and print 165 .
Suppose you want to compute sum of all integers between 40 and 60 . Here is the program to achieve just that:
int sum = 0;
int counter = 40;
while(counter <= 60) {
sum = sum + counter;
counter = counter + 1;
}
System.out.println(sum);
 
Search WWH ::




Custom Search