• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

SCJP Requirements V. OO Concepts: encapsulation cohesion coupling.

Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you see errors or additions please respond. Or if you have questions ask, someone will answer them.

Here are my notes on the requirements for the SCJP test from section 5 of the sun requirements. Point 1. as below.

Section 5.1 Develop code that implements tight encapsulation, loose coupling, and high cohesion in classes, and describe the benefits.

These three principles (encalsulation, cohesion, and coupling) are the basis of good programming for OO applications. To write good OO applications you need to follow these three principles. They are related to each other, so improving the status of one often improves the status of the other two.

Encapsulation is the combining of methods and data into one data structure called a class. You should add the phrase �and hiding the implementation of the data from the user.� The definition for encapsulation then includes data hiding but the two belong together. Encapsulation is the way data hiding is accomplished and this is one of two basic tenets of the entire OO approach to programming. The word tight here could be defined as hidden, as in tight encapsulation means hidden data.

Coupling by definition is the information that is passed back and forth that changes the state of a class or object. More generally it is the sharing of data between classes and methods. Here loose coupling means minimal coupling or the less data passed back and forth the better. A good principle of programming and design is to pass only the information needed by the method. When more information is passed back and forth (especially control arguments) the more likely changes to the state of the object become and thus data hiding is affected. A control argument for instance could provide direction to a method to change the instance variables value belonging to an object thus making private data, public data. Not a good programming practice. So encapsulation and coupling are interrelated.

Cohesiveness can be defined as the relatedness of data to data or methods to data. When classes are formed data and methods that operate on that data are placed into a single class. Data and methods which are closely reated are placed into one class while data and methods not closely related are placed into other classes. High cohesion means the data and methods are highly related to one another. When this is done there is less need for increased coupling or breaking encapsulation because the data and methods needed are all in one place.

Directions for achieving these three principles include:
1. keep data private.
2. use mutator and accessor methods.
3. break up classes which are vague or have too many responsibilities. 4.) 4. minimize the arguments for methods, both data and message.
5. not all fields require mutator and accessor methods.
6. a method should access only its own and never another objects.
7. try to minimize the classes that know about or use each other.

The benefits of these three principles include:
1. when using mutator methods only mutator methods needs changing.
2. easier to maintain.
3. increases reuse.
4. increases reliability
5. reduces data dependency.
6. reduces debugging time.

If you see things that should be added let me know.

Thanks Joe.
Joe Wolfe
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No one found mistakes or stuff I left out therefore I am a genus.

Thats good to know.

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic