Advantages

Improved security

Using data-hiding / Use of private variables

So that other classes cannot (accidentally) access/change the data (directly) / So other classes have to use accessor methods to access the data / So programs can be made read-only

Ease of maintenance

Programs will be easier to update

Because changes to the encapsulated class should not impact on other classes

Code reuse

Methods are stored in a class.

These methods can be used by mutiple instances of the class ie code can be reused, reducing the amount of code needed in a program.

Ease of understanding

Programs will be easier to follow

Because all data and methods are in the same place

Ease of testing

Because the class can be fully tested in isolation

And limited code will contain fewer mistakes than the full program

Faster development (of whole program)

Different classes can be worked on by teams

Each class is effectively independent of others (although this is not always true - see dependencies)

Concurrency

When managing projects, Gantt Charts can be used to plan which tasks need to be completed, when they should start and how long they will take.

The above Gantt chart demonstrates that different classes can be designed concurrently by a team of programmers.

Of course, other tasks have to be completed sequentially ie we can't start some tasks until others are complete.

UML Diagrams

object oriented programming
 

A Customer Care call center in Berlin processes Customer information.

A program needs to be designed to manage Customer information.

It has been decided to use Object Oriented Programming in the design of the program and take advantage of

Many classes need to be designed in this project. One advantage of OOP and encapsulation is that different programmers can work on different classes - classes can be designed and tested .

UML Diagrams

Program designers produce UML diagrams describing the properties/attributes of the class and the methods/behavoiur of the class.

Here is the UML diagram describing the Customer class.

Customer
- name : String
- address : String
- phone : String
+ Customer() //constructor method has no return type
+ getName() : String
+ getAddress() : String
+ getPhone() : String
+ setName(String) : void
+ setAddress(String) : void
+ setPhone(String) : void

UML - Unified Modeling Language - diagrams can help software engineers to plan and visualise design ideas.

Points to note:

  • the diagram has 3 distinct sections: the title, the list of attributes, the list of methods
  • - is used to indicate private members of the class
  • + is used to indicate public members of the class
  • When describing a property, the format is identifier : data type
  • When describing a method, the format is identifier(parameter data types) : return type

The Bucket Challenge

Task A

 
Bucket
- maxVol : int
- currentVol : int
- isFull : boolean
+ Bucket(int) //constructor method has no return type
+ getMaxVol() : int
+ getCurrentVol() : int
+ getFull() : boolean
+ setCurrentVol(int) : void
+ toString() : String
+ speak() : void

A Bucket has:

  • a maxVolume eg 100
  • a currentVolume eg 37
  • an indicator that it is full eg true

When a Bucket is first constructed, maxVolume is set by a parameter, the current volume is set to zero and the indicator is set to false.

It has the following behaviour:

  • when the currentVolume reaches the maxVolume, the indicator is set to true
  • a Bucket can speak! When it is not full it says "More sand, please!", otherwise it says "I'm full!"
  • A bucket can be filled with random amounts of sand, but only if it is not already full

Use this information and the UML diagram above to design the Bucket class.

Task B

In another class, eg a Main class, construct 2 instances (or more) of the Bucket class (a Bucket object).

Write a program that will keep filling the Buckets until one of them is full. The buckets speak as it they are being filled.

Call the toString method of each Bucket instance at the end of the program.

Note: the toString method just returns a sentence containing the state of all instance variables.

Complete the HungryFoxes challenge.