Unit 3: Object-oriented programming concepts and principles
Understand the Java language's object-oriented paradigm
Before you begin
This unit is part of the "Intro to Java programming" learning path. Although the concepts discussed in the individual units are standalone in nature, the hands-on component builds as you progress through the units, and I recommend that you review the prerequisites, setup, and unit details before proceeding.
- Grasp how the object-oriented paradigm differs from the structured-programming paradigm
- Know the key characteristics of an object
- Understand the benefits that stem from the defining principles of object-oriented programming (OOP)
What is an object?
The Java language is (mostly) object oriented. This unit gives you a short introduction to OOP language concepts and principles, using structured programming as a point of contrast.
Object-oriented languages follow a different programming pattern from structured programming languages like C and COBOL. The structured-programming paradigm is highly data oriented: You have data structures, and then program instructions act on that data. Object-oriented languages such as 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. Instead of 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 such as a
Number, to coarse-grained objects, such as a
FundsTransfer service in a large banking application.
Parent and child objects
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. With the object-oriented paradigm, you can reuse the common attributes and behavior of the parent object, adding to its child objects attributes and behavior that differ.
Object communication and coordination
Objects talk to other objects by sending messages (method calls, in Java parlance). Furthermore, in an object-oriented application, program code coordinates the activities among objects to perform tasks within the context of the specific application domain.
A well-written object:
- Has well-defined boundaries
- Performs 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.
It's time to see what a Java object looks like.
Example: A person object
My first example is based on a common application-development scenario: an
individual being represented by a
You know from the definition of an object that an object has two primary
elements: attributes and behavior. Here's how these elements apply to the
As a rule of thumb, think of the attributes of an object as nouns and behavior as verbs.
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 application context of some kind. In a business-application
context, for instance, you might want to ask your
object, "What is your body mass index (BMI)?" In response,
Person would use the values of its height and weight
attributes to calculate the BMI.
More-complex logic can be hidden inside of the
but for now, suppose that
Person has the following
- Calculate BMI
- Print all attributes
State and string
State is an important concept in OOP. An object's state is represented at any moment in time by the values 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 by using a
String class, which you'll learn more about later.
Using the concepts of state and string together, you can say to
Person, "Tell me all about you by giving me a listing (or
String) of your attributes."
Principles of OOP
If you come from a structured-programming background, the OOP value proposition might not be clear yet. After all, the attributes of a person and any logic to retrieve (and convert) those values can be written in C or COBOL. The benefits of the OOP paradigm become clearer if you understand its defining principles: encapsulation, inheritance, and polymorphism.
Recall that an object is above all discrete, or self-contained. This characteristic is the principle of encapsulation at work. Hiding is another term that's sometimes used to express the self-contained, protected nature of objects.
Regardless of terminology, what's important is that the object maintains a boundary between its state and behavior and the outside world. Like objects in the real world, objects used in computer programming have various types of relationships with different categories of objects in the applications that use them.
On the Java platform, you can use access modifiers (which you'll learn about later) to vary the nature of object relationships from public to private. Public access is wide open, whereas private access means the object's attributes are accessible only within the object itself.
The public/private boundary enforces the object-oriented principle of encapsulation. On the Java platform, you can vary the strength of that boundary on an object-by-object basis. Encapsulation is a powerful feature of the Java language.
In structured programming, it's common to copy a structure, give it a new
name, and add or modify the attributes that make the new entity (such as
Account record) different from its original source. Over
time, this approach generates a great deal of duplicated code, which can
create maintenance issues.
OOP introduces the concept of inheritance, whereby specialized classes — without additional code — can "copy" the attributes and behavior of the source classes that they specialize. If some of those attributes or behaviors need to change, you override them. The only source code you change is the code needed for creating specialized classes. The source object is called the parent, and the new specialization is called the child— terms that you've already been introduced to.
Suppose that you're writing a human-resources application and want to use
Person class as the basis (also called the super
class) for a new class called
Employee. Being the
Employee would have all of the
attributes of a
Person class, along with additional ones,
- Taxpayer identification number
- Employee number
Inheritance makes it easy to create the new
without needing to copy all of the
Person code manually.
Polymorphism is a harder concept to grasp than encapsulation and inheritance. In essence, polymorphism means that objects that belong to the same branch of a hierarchy, when sent the same message (that is, when told to do the same thing), can manifest that behavior differently.
To understand how polymorphism applies to a business-application context,
return to the
Person example. Remember telling
Person to format its attributes into a
Polymorphism makes it possible for
Person to represent its
attributes in various ways depending on the type of
Polymorphism, one of the more complex concepts you'll encounter in OOP on the Java platform, is beyond the scope of this introductory course. You'll explore encapsulation and inheritance in more depth in subsequent units.
Not a purely object-oriented language
Two qualities differentiate the Java language from purely object-oriented languages such as Smalltalk. First, the Java language is a mixture of objects and primitive types. Second, with Java, you can write code that exposes the inner workings of one object to any other object that uses it.
The Java language does give you the tools necessary to follow sound OOP principles and produce sound object-oriented code. Because Java is not purely object oriented, you must exercise discipline in how you write code — the language doesn't force you to do the right thing, so you must do it yourself. You'll get tips in Unit 12, "Writing good Java code."
Test your understanding
Test your knowledge of what you've learned in this unit. Answers are below.
- An object is a data-oriented entity, and any program that needs to can
access any of that object's data.
- Which statement most accurately describes the relationship between a
parent and child object?
- A parent object contains the exact same code as its child.
- A child object is unrelated to its parent.
- A parent object inherits attributes and behavior from its child.
- A child object contains code from its parent but is more specialized and can define additional attributes.
- A well-written object can be described as:
- Has well-defined boundaries, does only a few things, and knows only about its own data.
- Is largely data oriented and open to any program that needs to access it.
- Has knowledge only of the other objects it needs to perform its functions.
- a and b
- a and c
- All of the above
- Which statement(s) best describes an object's attributes?
- Attributes act like the nouns of an object.
- Attributes describe an object's behavior.
- Attributes can mean pretty much whatever you want them to mean — that's what we mean by "open source."
- a and c
- b and c
- All of the above.
- Choose the option(s) that potentially contains some additional
- Calculate age in weeks
- Number of siblings
- Resting pulse (in beats per minute)
- a and c
- b and c
- All of the above.
- Which statement(s) best describes an object's behavior?
- Behavior acts like the nouns of an object.
- Behavior, like attributes, can be defined however we want — that's what we mean by "open source."
- Behavior acts like the verbs of an object.
- All of the above.
- Encapsulation means "open to all" and indicates that the internal
workings of an object are entirely visible to the outside world.
- Which statement(s) best describes inheritance?
- One object (the child) generalizes the attributes and behavior of another object (the parent), making the object more generalized.
- The child object inherits from its parent, adding specialized attributes and behavior.
- When a child object is created, a well-known best practice is for the developer to manually copy/paste all of the source code from the parent object to ensure that nothing gets broken.
- When a child object inherits from its parent, only the additional attributes and behavior must be coded, which is one of the benefits of OOP.
- a and b
- b and d
- All of the above.
- Java is a pure object-oriented programming language like Scala or
Answers to quiz
1. b 2. d 3. e 4. a 5. e 6. c 7. b 8. f 9. b