Contents


Unit 3: Object-oriented programming concepts and principles

Understand the Java language's object-oriented paradigm

Comments

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.

Unit objectives

  • 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.

Object summary

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 Person object.

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 Person object.

As a rule of thumb, think of the attributes of an object as nouns and behavior as verbs.

Attributes (nouns)

What attributes can a person have? Some common ones include:

  • Name
  • Age
  • Height
  • Weight
  • Eye color
  • Gender

You can probably think of more (and you can always add more attributes later), but this list is a good start.

Behavior (verbs)

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 Person 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 Person object, but for now, suppose that Person has the following behavior:

  • 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.

Encapsulation

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.

Inheritance

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 an 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 the Person class as the basis (also called the super class) for a new class called Employee. Being the child of Person, Employee would have all of the attributes of a Person class, along with additional ones, such as:

  • Taxpayer identification number
  • Employee number
  • Salary

Inheritance makes it easy to create the new Employee class without needing to copy all of the Person code manually.

Polymorphism

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 String? Polymorphism makes it possible for Person to represent its attributes in various ways depending on the type of Person it is.

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.

  1. An object is a data-oriented entity, and any program that needs to can access any of that object's data.
    1. True
    2. False
    3. Sometimes
  2. Which statement most accurately describes the relationship between a parent and child object?
    1. A parent object contains the exact same code as its child.
    2. A child object is unrelated to its parent.
    3. A parent object inherits attributes and behavior from its child.
    4. A child object contains code from its parent but is more specialized and can define additional attributes.
  3. A well-written object can be described as:
    1. Has well-defined boundaries, does only a few things, and knows only about its own data.
    2. Is largely data oriented and open to any program that needs to access it.
    3. Has knowledge only of the other objects it needs to perform its functions.
    4. a and b
    5. a and c
    6. All of the above
  4. Which statement(s) best describes an object's attributes?
    1. Attributes act like the nouns of an object.
    2. Attributes describe an object's behavior.
    3. Attributes can mean pretty much whatever you want them to mean — that's what we mean by "open source."
    4. a and c
    5. b and c
    6. All of the above.
  5. Choose the option(s) that potentially contains some additional attributes of Person.
    1. Calculate age in weeks
    2. Number of siblings
    3. Resting pulse (in beats per minute)
    4. a and c
    5. b and c
    6. All of the above.
  6. Which statement(s) best describes an object's behavior?
    1. Behavior acts like the nouns of an object.
    2. Behavior, like attributes, can be defined however we want — that's what we mean by "open source."
    3. Behavior acts like the verbs of an object.
    4. All of the above.
  7. Encapsulation means "open to all" and indicates that the internal workings of an object are entirely visible to the outside world.
    1. True
    2. False
    3. Sometimes
  8. Which statement(s) best describes inheritance?
    1. One object (the child) generalizes the attributes and behavior of another object (the parent), making the object more generalized.
    2. The child object inherits from its parent, adding specialized attributes and behavior.
    3. 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.
    4. 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.
    5. a and b
    6. b and d
    7. All of the above.
  9. Java is a pure object-oriented programming language like Scala or Smalltalk.
    1. True
    2. False
    3. Sometimes

Answers to quiz

1. b 2. d 3. e 4. a 5. e 6. c 7. b 8. f 9. b

For further exploration

OO Design Process: The object primer

Object-Oriented Programming Concepts

Object-oriented language basics

IBM Code: Java journeys

Previous: Setting up your Java development environmentNext: Getting started with the Java language


Downloadable resources


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java development
ArticleID=1036604
ArticleTitle=Unit 3: Object-oriented programming concepts and principles
publish-date=09142016