James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

Quick and Easy OOP - Part II - Abstraction

8:00 PM
Overview
OOP has many terms that relate to programming techniques.  I am going to try and break this down into bite-size and tasty morsels of programmatic goodness.

Abstraction
Before we get into Abstraction, lets first understand the definition of Abstract, according to Dictionary.com:
ab-stract, adjective,
1. thought of apart from concrete realities, specific objects, or actual instances.
2. expressing a quality or characteristic apart from any specific object or instance, as justice, poverty, and speed
Maybe you are saying "Huh?"  Lets look at the Latin origin:
...from Latin abstractus meaning "drawn away,"...

Let's put that into programmatic terms using an example that you might be able to relate to.  Let's start with a goal:  Create an abstraction object from which other similar objects can derive common features so we don't have to add those common features manually to each object we have to create.

Animal.  There are many types of Animals out there.  Cats, Dogs, Horses, Elephants.  They have some common features.  Yet they also have some unique qualities about them.  Let's first look at the animals and see their similarities.

TraitDog     Horse     Elephant
Speak
x
x
x
Bite
x
x
Eat
x
x
x
Run
x
x
x
Ride
x
x
Domesticated
x
x
Has Tusks
x
As you can see there are similarities.  Items such as Speak, Eat, Run, are common among the animals.  I will then abstract, or draw away from the individual animals their common features and put them in the Animal object.

My Animal object will look like this:
  • Speak.  A method for making a vocalization.
  • Run.  A method for running.
  • Eat.  A method for eating.

My Dog object will inherit the Speak, Run, and Eat from the Animal object.  But my dog object will also have some additional features, such as:
  • Bite.  A method for biting.
  • Domesticated.  This dog can be domesticated, or not.  A true/false situation.
My Horse object will inherit Speak, Run, and Eat from the Animal object.  But my horse object will also have some additional features, such as:
  • Ride.  A true/false situation on whether or not you can ride the horse.
  • Domesticated.  This horse can be domesticated, or not.  A true/false situation.

My Elephant object will inherit Speak, Run, and Eat from the Animal object.  But my elephant object will also have some additional features, such as:
  • Ride.  A true/false situation on whether or not you can ride the horse.
  • HasTusks.  This elephant has tusks, or not.  A true/false situation.

As you can see, you only had to write the Speak, Run, and Eat methods in the Animal class.  Since these are common - this saves you time and you get to re-use the code within those abstracted pieces. In this case you used Abstraction to "draw away" the commonalities from the individual animals.

0 comments:

 
Toggle Footer
Top