October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Exploring Encapsulation

  • June 30, 2004
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Introduction

This is the sixth installment in a series of articles about fundamental object-oriented (OO) concepts. The material presented in these articles is based on material from the second edition of my book, The Object-Oriented Thought Process, 2nd edition. The Object-Oriented Thought Process is intended for anyone who needs to understand the basic object-oriented concepts before jumping into the code. Click here to start at the beginning of the series.

Now that we have covered the conceptual basics of classes and objects, we can start to explore specific concepts in more detail. Remember that there are three criteria that are applied to object-oriented languages: They have to implement encapsulation, inheritance, and polymorphism. Of course, these are not the only important terms, but they are a great place to start a discussion.

In this article, and several ones that follow, we will focus on a single concept and explore how it fits in to the object-oriented model. We will also begin to get much more involved with code. In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the J2SE 1.4.2 SDK (software development kit) to compile and execute these applications and will provide the code listings for all examples in this article. I have the SDK 1.4.0 loaded on my machine. I will provide figures and the output for these examples.

Encapsulation

en7cap7su7late (n-kps-lt) also in7cap7su7late: To encase in or as if in a capsule.

Definition from [http://www.dictionary.com]

Remember that encapsulation means that the attributes (data) and the behaviors (code) are encapsulated in to a single object. In other models, namely a structured model, code is in files that are separate from the data. An object, conceptually, combines the code and data into a single entity. To illustrate, let's begin immediately with a code example.

For this example, we will create a very simple, but functional, checking account object. At first pass, all we want our checking account object to do is hold the value representing the balance of the account, set the balance, and return the balance. It is always good to start simple and work your way to more complicated things. The UML diagram for this example can be seen in Figure 1.

Figure 1: UML Diagram for Checking Account Class

Here is the corresponding code for our simple checking account object:

class CheckingAccount {   private double balance = 0;   public void setBalance(double bal) {      balance = bal;   };   public double getBalance(){      return balance;   };}

Listing 1: CheckingAccount.java

Remember that to create objects we must first create the class (or template) of the object. Listing 1 is a class called CheckingAccount, which satisfies the requirements that we had regarding the class. This class has an attribute called balance, which will hold the balance of the CheckingAccount. The class also has two methods, one called setBalance(), which changes balance and getBalance(), which returns the value of balance.

The methods setBalance() and getBalance() are often called getters and setters (for obvious reasons). In many cases, an attribute of the object will have both a getter and a setter. This is not always the case and we will discuss this concept later in the article. What IS always the case is that the getters and setters are used to control access to the attributes and this is a key part of encapsulation.

Before we explore this code in much more detail, let's actually use this simple class. It is important to know that the class, as it is written in Listing 1, cannot be executed directly. In short, the class is not an application. To actually run something, you must create an application that may or may not use a class you have written. In Java, this application must start with a main() method as the entry point. Here is an example of an application that uses our CheckingAccount class to instantiate and use a CheckingAccount object.

class Encapsulation {   public static void main(String args[]) {      System.out.println("Starting myEncapsulation...");      CheckingAccount myAccount = new CheckingAccount();      myAccount.setBalance(40.00);      System.out.println("Balance = " + myAccount.getBalance());   }}

Listing 2: Encapsulation.java

All this application does is instantiate a CheckingAccount object, called myAccount, set the value of balance to 40.00 and then print that balance out. Figure 2 presents a screen shot of the output of this application.



Click here for a larger image.

Figure 2: Checking Account application output.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel