By J Steven Perry | Published September 14, 2016
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.
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.
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.
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:
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.
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.
What attributes can a person have? Some common ones include:
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 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:
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.”
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 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:
Inheritance makes it easy to create the new Employee class 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.
Want more? Dig into the details of polymorphism with this step-by-step recipe, which demonstrates how polymorphism works using the Java language.
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.
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 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 well-written object can be described as:
Which statement(s) best describes an object’s attributes?
Choose the option(s) that potentially contains some additional attributes of Person.
Which statement(s) best describes an object’s behavior?
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?
Java is a pure object-oriented programming language like Scala or Smalltalk.
Previous: Setting up your Java development environmentNext: Getting started with the Java language
Which platforms, particular standards, and runtimes should enterprise developers base their applications?
March 1, 2019
March 28, 2019
Back to top