|
In this article, I explore the distinction between encapsulation and information hiding through the development of example code. The discussion shows how Java facilitates encapsulation and investigates the negative ramifications of encapsulation without data hiding. The examples also show how to improve class design through the principle of information hiding. In this example we created 3 classes, Mammal, Dog, and an application class called Packaging. The reason that we named the application Packaging was due to the fact that we are going to address our encapsulation/inheritance dilemma by using Java packaging. The Mammal class is shown in listing 1. A description of an object-oriented program will be provided, along with a description of an object, and how it relates to encapsulation. 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. Peter Coad and Mark Mayfield make a case that, when using inheritance, encapsulation is inherently weakened within a class hierarchy. They discuss a specific risk: Inheritance indicates strong encapsulation with other classes, but weak encapsulation between a superclass and its subclasses. |
| ww_w_.j___a__va__2__s___.___c___om | Contact Us |
| Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
| All other trademarks are property of their respective owners. |