Tuesday, July 29, 2008

Abstraction - Complete Picture


What is Abstraction?
Abstraction is process of modeling real-world objects. When you create a class you create an abstraction of a real-world object. It is virtual class design. It focuses on outside view of an object (i.e the Interface).
It Focus on What the Object is and what Object does.
In Other Word Abstraction is the facility to define objects that represent abstract "actors" that can perform work, report on and change their state, and "communicate" with other objects in the system
Java Example

public class Animal extends LivingThing
{
private Location loc;
private double energyReserves;

boolean isHungry() {
return energyReserves < 2.5;
}
void eat(Food f) {
// Consume food
energyReserves += f.getCalories();
}
void moveTo(Location l) {
// Move to new location
loc = l;
}
}
With the above definition, one could create objects of type Animal and call their methods like this:
public static void main(String[] args)
{
thePig = new Animal();
theCow = new Animal();
if (thePig.isHungry()) {
thePig.eat(tableScraps);
}
if (theCow.isHungry()) {
theCow.eat(grass);
}
theCow.moveTo(theBarn);
}
In the above example, the class Animal is an abstraction used in place of an actual animal, LivingThing is a further abstraction (in this case a generalisation) of Animal.

If a more differentiated hierarchy of animals is required to differentiate, say, those who provide milk from those who provide nothing except meat at the end of their lives, that is an intermediary level of abstraction, probably DairyAnimal (cows, goats) who would eat foods suitable to giving good milk, and Animal (pigs, steers) who would eat foods to give the best meat quality.
Level of Abstraction
Obviously, the objects represent different kinds of vehicles. By realizing that fact, we have found our first abstraction and will take a class "vehicles" as the base class of our hierarchy.
A first attempt in building a hierarchy may look like:

Figure1: First attempt in building a class hierarchy
What can be said about this hierarchy is: "Don't try this at home!". Although it contains all objects and therefore it's "correct", it is not very intelligent. An evident violation of the abstraction principle is, for example, that a "sports car" can be found on the same level as a "Ferrari" and a "Lotus". This must be wrong, because they both are kinds of a "sports car".
So we change our hierarchy to the following:
Figure2: Second attempt in building a class hierarchy
Now we have got a more structured hierarchy with the subclasses "bikes", "sports cars" and "transporters". But they are still too specific. For instance, if a new object "Rolls Royce" is to be added to the hierarchy, where should it be inserted, for it is no sports car?
We rearrange our structure by introducing a new class "cars":
Figure3: An important abstraction: The new class "cars"

Now there are four levels of abstraction. The new class "cars" may contain the properties that are common to all of its subclasses. But there is still something wrong with our sports cars and limousines. As you can see, they are arranged on the same abstraction level as, for example, a "truck". But a truck is a more abstract thing than a Ferrari or a Rolls Royce.
We try to improve our hierarchy by doing another abstraction:
Figure4: A new abstraction: The class "private motorcars"
The fifth abstraction level had been created and our class hierarchy is now much more structured than it was at our first attempt, but is still far from being finished.
This was an example of the abstractioning process that should only demonstrate, how a class hierarchy can be built up and which thoughts have to be made.
The next sections consist of my class hierarchy for neural networks.
Because neural nets are themselves abstract things, the design process was, literally, an "abstraction of abstractions" and, unlike the previous example, took me a few more attempts to get any results.

No comments: