Object-oriented programming concepts.

The Java language is (mostly) object oriented. If you haven't used an object-oriented language before, OOP concepts might seem strange at first. This section is a short introduction to OOP language concepts, using structured programming as a point of contrast.

What is an object?

Structured programming languages like C and COBOL follow a different programming paradigm from object-oriented ones. 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. (You learn more about inheritance in the next section of this tutorial.)

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 given application domain.

Object summary

A well-written object:
  • Has crisp 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.
Now, you see what an object looks like.

The Person object

I start with an example that is based on a common application-development scenario: an individual being represented by a Person object.
Going back to the definition of an object, you know that an object has two primary elements: attributes and behavior. I show how these apply to the Person object.

Attributes

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

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 Age attribute.
More-complex logic can be hidden inside of the Person object — for example, calculating a person's Body Mass Index (BMI) for a health application — 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 and string

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 talk more about later in the tutorial.
Using the concepts of state and string together, you can say to Person, "Tell me who you are by giving me a listing (or String) of your attributes."
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment