Object-oriented programming concepts
The Java language is (mostly) object-oriented. If you haven't used an object-oriented language before, its concepts might seem strange at first. This section is a short introduction to OOP language concepts, using structured programming as a point of contrast.
Structured programming languages like C and COBOL follow a very different programming paradigm from object-oriented ones. The structured-programming paradigm is highly data-oriented, which means that you have data structures on one hand, and then program instructions that act on that data. Object-oriented languages like the Java language combine data and program instructions into objects.
An object is a self-contained entity that contains attributes and behavior, and nothing
more. Rather than having a data structure with fields (attributes) and passing that structure around to all of the program logic that acts on it (behavior), in an object-oriented language, data and program logic are combined. This combination can occur at vastly different levels of granularity, from fine-grained objects like a
Number, to coarse-grained objects such as a
FundsTransfer service in a large banking application.
A parent object is one that serves as the structural basis for deriving more-complex child objects. A child object looks like its parent but is more specialized. The object-oriented paradigm allows you to reuse the common attributes and behavior of the parent object, adding to its child objects attributes and behavior that differ. (You'll learn more about inheritance in the next section of this tutorial.)
Objects talk to other objects by sending messages (method calls in the Java language). Furthermore, in an object-oriented application, program code coordinates the activities among objects to perform tasks within the context of the given application domain. (In the Model-View-Controller paradigm, this coordinating program code is the Controller. See Resources to learn more about MVC.)
A well-written object:
- Has crisp boundaries
- Does a finite set of activities
- Knows only about its data and any other objects that it needs to accomplish its activities
In essence, an object is a discrete entity that has only the necessary dependencies on other objects to perform its tasks.
Now you'll see what an object looks like.
I'll start with an example that is based on a common application-development scenario: an individual being represented by a
Going back to the definition of an object, you know that an object has two primary elements: attributes and behavior. You'll see how these apply to the
What attributes can a person have? Some common ones include:
- Eye color
You can probably think of more (and you can always add more attributes later), but this list is a good start.
An actual person can do all sorts of things, but object behaviors usually relate to some kind of application context. In a business-application context, for instance, you might want to ask your
Person object, "What is your age?" In response,
Person would tell you the value of its
More-complex logic could be hidden inside of the
Person object, but for now suppose that
Person has the behavior of answering these questions:
- What is your name?
- What is your age?
- What is your height?
- What is your weight?
- What is your eye color?
- What is your gender?
State is an important concept in OOP. An object's state is represented at any moment in time by the value of its attributes.
In the case of
Person, its state is defined by attributes
such as name, age, height, and weight. If you wanted to present a list of several of those attributes, you might do so using a
String class, which I'll talk more about later in the tutorial.
Together, the concepts of state and string allow you to say to
Person: tell me who you are by giving me a listing (or
String) of your attributes.