Java Reference
In-Depth Information
At this stage, you may be asking, “Where is the value 26 , which is associated with the name n1 , stored in
memory?” You know from the definition of int data type that n1 will take 32-bit memory. However, you do not know,
cannot know, and do not need to know where in the memory that 32-bit is allocated for n1 . Do you see an example
of abstraction here? If you see an example of abstraction in this case, you are right. This is an example of abstraction,
which is built into the Java language. In this instance, the pieces of information about the data representation of the
data value for int data type are hidden from the users (programmers) of the data type. In other words, a programmer
ignores the memory location of n1 and focuses on its value and the operations that can be performed on it.
A programmer does not care if the memory for n1 is allocated in a register, RAM, or the hard disk.
Data Abstraction
Object-oriented programming languages such as Java let you create new data types using an abstraction mechanism
called data abstraction. The new data types are known as abstract data types (ADT). The data objects in ADT may
consist of a combination of primitive data types and other ADTs. An ADT defines a set of operations that can be
applied to all its data objects. The data representation is always hidden in ADT. For users of an ADT, it consists of
operations only. Its data elements may only be accessed and manipulated using its operations. The advantage of using
data abstraction is that its data representation can be changed without affecting any code that uses the ADT.
Data abstraction lets programmers create a new data type called an abstract data type, where the storage
representation of the data objects is hidden from the users of the data type. in other words, aDt is defined solely in terms
of operations that can be applied to the data objects of its type without knowing the internal representation of the data.
the reason this kind of data type is called abstract is that users of aDt never see the representation of the data values.
Users view the data objects of an aDt in an abstract way by applying operations on them without knowing the details
about representation of the data objects. note that an aDt does not mean absence of data representation. Data
representation is always present in an aDt. it only means hiding of the data representation from its users.
Tip
Java language has constructs, for example, class, interface, and enum , that let you define new ADTs. When you
use a class to define a new ADT, you need to be careful to hide the data representation, so your new data type is really
abstract. If the data representation in a Java class is not hidden, that class creates a new data type, but not an ADT. A
class in Java gives you features that you can use to expose the data representation or hide it. In Java, the set of values of
a class data type are called objects. Operations on the objects are called methods. Instance variables (also known as
fields) of objects are the data representation for the class type.
A class in Java also lets you provide an implementation of operations that operates on the data representation.
An interface in Java lets you create a pure ADT. An interface lets you provide only the specification for operations
that can be applied to the data objects of its type. No implementation for operations or data representation can be
mentioned in an interface. Listing 1-1 shows the definition of the Person class using Java language syntax. By defining
a class named Person , you have created a new ADT. Its internal data representation for name and gender uses String
data type ( String is built-in ADT provided by Java class library). Note that the definition of the Person class uses the
private keyword in the name and gender declarations to hide it from the outside world. Users of the Person class
cannot access the name and gender data elements. It provides four operations: a constructor and three methods
( getName , setName , and getGender ).
A constructor operation is used to initialize a newly constructed data object of Person type. The getName and
setName operations are used to access and modify the name data element, respectively. The getGender operation is
used to access the value of the gender data element.
 
 
Search WWH ::




Custom Search