Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 1, 2009

Basic Pillars of an Object-Oriented System

Filed under: C#,OOP,VB.NET @ 12:16 pm

The four basic elements of an object-oriented system are abstraction,
encapsulation, inheritance, and polymorphism. This post defines
these terms and describes why they are important to software design and
development.

[To begin with an overview of OO, start here.]

Abstraction: Focusing on What is Important

Abstraction is a technique that we all use to manage the complexity of the
information we collect every day. It allows us to recognize how things are
similar and ignore how they are different, to think about generalities and
not specifics, and to see what things are without thinking about what makes
them that way. You can abstract important characteristics at any given time
and for any particular purpose and ignore all other aspects.

How you develop an abstraction depends on both its purpose and your
perspective. For example, on a warm summer day I look at a tree and
abstract it as a shade provider. A young child abstracts it as a place to
climb. One tree, two different abstractions.

Abstraction is used to identify the objects involved with a particular
application. In developing a payroll system, you would think about Jessica
Jones and abstract her as an employee, thinking only about her salary and
how she gets paid. When working on the company softball league application, you would abstract Jessica as a player and be more concerned with her position and batting average. One object, two completely different abstractions.

Scenarios (or use cases) can ensure that the correct abstraction is used in a particular application. They provide context for the objects, giving them a purpose.

Using abstraction, you can focus on the objects and not on the implementation. This lets you think about what needs to be done and not how the computer will do it.

Encapsulation: Hiding Your Private Parts

Most organizations have independent units, usually called departments.
The sales department makes the sales, the production department produces
the item, the shipping department ships it, and so on. Each department
is responsible for its own procedures and internal information. For example, the sales department has procedures for calling prospects, evaluating
the opportunity, sending sales materials, following up with current
customers, maintaining prospect information, and so on.

You could say the departments are encapsulated because the internal
information (properties) and standard operating procedures (methods)
are contained within the department. These are figuratively hidden from
other departments except through defined interfaces. Anyone who needs
the department’s procedures or information goes to that department to
ask for it.

If a shipping clerk acquires the name of a prospect, the clerk knows
better than to handle the prospect directly. Instead, the clerk collaborates
with the sales department by using the publicly accessible defining prospects
feature and sends the name to them.

The same principles are used in object-oriented applications to encapsulate
each object’s properties and methods. When an object needs to perform
a procedure that is encapsulated in another class, it does not perform
the procedure directly. Instead, it collaborates with an object belonging to
the other class to perform the procedure.

Encapsulation aids in abstraction by hiding the internal implementation
of an object within the class. You then can use an object without
understanding how its class is implemented. So the shipping clerk can
define a prospect to the sales department without knowing how the sales
department actually handles the prospect.

Inheritance: Attaining Reuse

Things that are similar still have some differences, and things that are
different often have some similarities. Reviewing the inheritance
example (from my post here), both your desk phone and your cell phone can be classified as phones. Looking at their differences, your desk phone can transfer calls, and your cell phone can take pictures.

Say you are asked to create phone emulator software. You could implement
all properties and methods for a cell phone into a cell phone class. This class would include volume and picture count properties and dial, answer, disconnect, and take picture methods. Likewise, you could implement all properties and methods for a desk phone into a desk phone class.

This class would include volume and line status properties and dial,
answer, disconnect, and transfer call methods. This duplicates the common
phone information and functionality in both classes.

You can remove the property and method redundancy and attain reuse
by using inheritance. You can remove the common phone properties and
methods from the specialized classes and put them in a higher-level phone
class. The cell phone class and desk phone class can then inherit from the
phone class, attaining all its properties and methods. This “is a” relationship
is depicted here.

If a new type of phone class, such as a Web phone, were added, objects of the new class would automatically have all the properties and methods defined for the base phone class, greatly simplifying the development of the new class.
Inheritance is a powerful tool for code reuse.

Polymorphism: Same Behavior, Different Implementation

Two or more classes can have methods that are named the same and have
the same basic purpose but different implementations. This is polymorphism.
The Text property of the .NET Framework is an example of polymorphism.
Although the basic purpose of the Text property is the same, how the property works depends on whether you are setting the Text property of a Form, a Label, or a TextBox.

In the phone example, say the implementation of the Dial method
needed to be different in the cell phone class and the desk phone class.
The cell phone class would then need its own Dial method, and a desk
phone class would need its own Dial method. You can request the Dial
method by an object from either class without knowing how either class
plans to implement that request. Both the cell phone class and the desk
phone class have the Dial method, but the implementation of that behavior
can be completely different.

Polymorphism can be implemented using inheritance. The base phone
class would retain a Dial method, but the implementation would be empty
(or would provide a default implementation). Each derived class would
then override the base class Dial method by implementing the method.
The result is that the desk phone class and cell phone class each have a
Dial method, but the implementations are different.

Polymorphism can also be implemented using an interface. In the
phone example, the Dial method would be removed from the base phone
class. An IDial interface would define the properties and methods for
handling dialing. Both the desk phone class and cell phone class implement
this interface so that they both have the same properties and methods
for dialing but different implementations. (See this link for more information on interfaces.)

The benefit of polymorphism is that you don’t need to know the
object’s class to execute the polymorphic behavior. For example, you may
have many classes in an application, each with its own save method. When
the application is saved, each object knows the class it belongs to and automatically calls the correct save routine.

(Based on an except from "Doing Objects in Visual Basic 2005".)

Enjoy!

4 Comments

  1.   M. K. Basher — February 4, 2010 @ 11:51 am    Reply

    Excellent documents. After read it, I have got a clear concept about about the basic pillar of OOP

  2.   Anand Chavan — January 30, 2012 @ 3:30 am    Reply

    Very well documented !

  3.   Brian Swiger — April 10, 2012 @ 1:24 pm    Reply

    As a developer, I’m surprised to know of other “developers” that are not aware of these basics. Thanks for the post.

  4.   Anonymous Coward — April 11, 2012 @ 7:05 am    Reply

    Some people might object to inheritance 🙂

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2018 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs