Java Reference
In-Depth Information
Basic concepts of oBject‐oriented programming
As you saw in Chapter 1, object‐oriented programming is a programming paradigm where concepts
in the program are represented by “objects.” Each object is an instance of a class, which can be seen
as a “blueprint” or template of the object's characteristics. Contrary to procedural programming,
these characteristics include data—attributes or variables describing the object's state —and behav-
iors—methods or procedures describing the actions an object can perform.
A simple example can help explain this. Imagine you are developing an application to keep track of
courses and student registrations. In procedural programming, your first task would be to come up
with an appropriate data structure to represent the concepts you are dealing with. You thus might
define two lists—one for holding the students and one for holding the courses. Each list would con-
tain a dictionary of values representing a single student or course. This might look as follows:
STUDENTS = [
{id : 'S0001', last: 'Demmick', first: 'Larry', birthdate: '1989-05-13'},
{id : 'S0002', last: 'Newandyke', first: 'Freddy', birthdate: '1991-01-05'},
...
]
COURSES = [
{id : 'C00A', name: 'Introduction to Java'},
{id : 'C00B', name: 'Advanced Data Base Management'},
...
]
Pay no attention to the syntax being used here—it's just pseudo‐code to illustrate the point. The fol-
lowing step defines a series of operations—procedures—that you want to perform on your concepts.
For example, you'll need a procedure to add a student:
procedure add_student(i, n, fn, bd) {
STUDENTS += {id : i, name: n, firstname: fn, birthdate: bd}
}
Similar procedures can be created to add courses and to remove or modify students and courses.
You also might want to keep track of which students registered for which courses. Multiple pos-
sibilities exist as to how to approach this: you can either add a list of course IDs to each student
(representing the courses the student registered for), or a list of student IDs to each course (repre-
senting the students registered for this course). Another way to do this is to create an additional data
structure— REGISTRATIONS —to keep track of registrations.
Procedural programming has substantial drawbacks from a maintenance viewpoint. For one, when
the definition of a data structure changes, all procedures using this structure need to be reviewed
and updated accordingly. Second, when data structures are linked, care needs to be taken that the
state of the program is kept valid at all times. When deleting a course in this example, for instance,
you'll also need to ensure that all registrations for this course are removed as well. The greatest
drawback comes from the fact that all data is stored in global structures, which are accessible to all
procedures. For large programs, it becomes unwieldy to keep track of which procedures are using
which structures, how they modify them, and what effect a change in one procedure or data struc-
ture will have in other parts of the program.
 
Search WWH ::




Custom Search