Principles of OOP.

If you come from a structured-programming background, the OOP value proposition might not be clear yet. After all, the attributes of a person and any logic to retrieve (and convert) those values can be written in C or COBOL. This section clarifies the benefits of the OOP paradigm by explaining its defining principles: encapsulation, inheritance, and polymorphism.

Encapsulation

Recall that an object is above all discrete, or self-contained. This characteristic is the principle of encapsulation at work. Hiding is another term that is sometimes used to express the self-contained, protected nature of objects.
Regardless of terminology, what's important is that the object maintains a boundary between its state and behavior and the outside world. Like objects in the real world, objects used in computer programming have various types of relationships with different categories of objects in the applications that use them.
On the Java platform, you can use access modifiers (which I introduce later in the tutorial) to vary the nature of object relationships from public to private. Public access is wide open, whereas private access means the object's attributes are accessible only within the object itself.
The public/private boundary enforces the object-oriented principle of encapsulation. On the Java platform, you can vary the strength of that boundary on an object-by-object basis, depending on a system of trust. Encapsulation is a powerful feature of the Java language.

Inheritance

In structured programming, it's common to copy a structure, give it a new name, and add or modify the attributes that make the new entity (such as an Account record) different from its original source. Over time, this approach generates a great deal of duplicated code, which can create maintenance issues.
OOP introduces the concept of inheritance, whereby specialized classes — without additional code — can "copy" the attributes and behavior of the source classes that they specialize. If some of those attributes or behaviors need to change, you override them. The only source code you change is the code needed for creating specialized classes. As you know from the "Object-oriented programming concepts" section, the source object is called the parent, and the new specialization is called the child.

Inheritance at work

Suppose you are writing a human-resources application and want to use the Person class as the basis (called the super class) for a new class called Employee. Being the child of Person, Employee would have all of the attributes of a Person class, along with additional ones, such as:
  • Taxpayer identification number
  • Employee number
  • Salary
Inheritance makes it easy to create the new Employee class without needing to copy all of the Person code manually.
You'll see plenty of examples of inheritance in Java programming later in the tutorial, especially in Part 2.

Polymorphism

Polymorphism is a harder concept to grasp than encapsulation and inheritance. In essence, it means that objects that belong to the same branch of a hierarchy, when sent the same message (that is, when told to do the same thing), can manifest that behavior differently.
To understand how polymorphism applies to a business-application context, return to the Person example. Remember telling Person to format its attributes into a String? Polymorphism makes it possible for Person to represent its attributes in a variety of ways depending on the type of Person it is.
Polymorphism is one of the more complex concepts you'll encounter in OOP on the Java platform and is beyond the scope of an introductory tutorial.
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.

1 comments: