CSC/ECE 517 Fall 2009/wiki2 15 A&OM: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Abstraction and the Object Model==
==Abstraction and the Object Model==
===Abstraction===
===Abstraction===
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [1].  
Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html].  


Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.
Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.


In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures.  
In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29].  


* Control abstraction in involves the use of subprograms and related concepts control flows  
* Control abstraction in involves the use of subprograms and related concepts control flows  
Line 13: Line 13:


===Object Model===
===Object Model===
Four concepts are critical to understanding object models. They are:  
Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are:  
* Data Abstraction  
* Data Abstraction  
* Encapsulation  
* Encapsulation  
Line 21: Line 21:
====Encapsulation====
====Encapsulation====
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.
The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.
====Data abstraction ====
====Data Abstraction ====
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation.  
Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types.  The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data.''  That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete'' data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[http://www.service-architecture.com/database/articles/object_model_concepts.html].
 
====Inheritance ====
====Inheritance ====
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.
====Polymorphism====
====Polymorphism====
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism ia a feature that allows Objects of different data types to be handled using a uniform/consistent interface.
Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.
 
===Abstraction in object oriented programming===
===Abstraction in Object Oriented Programming Languages===
Various object-oriented programming languages offer similar facilities for abstraction, all to support a general strategy of polymorphism in object-oriented programming, which includes the substitution of one type for another in the same or similar role. Although not as generally supported, a configuration or image or package may predetermine a great many of these bindings at compile-time, link-time, or loadtime. This would leave only a minimum of such bindings to change at run-time.
In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word "abstract" which we can use to create an abstract class. "An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses." Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.
Common Lisp Object System or self, for example, feature less of a class-instance distinction and more use of delegation for polymorphism. Individual objects and functions are abstracted more flexibly to better fit with a shared functional heritage from Lisp.  
 
C++ exemplifies another extreme: it relies heavily on templates and overloading and other static bindings at compile-time, which in turn has certain flexibility problems.  
Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are "center", "forward" and "guard" . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to "dribble" and "pass" to name a few.
Although these examples offer alternate strategies for achieving the same abstraction, they do not fundamentally alter the need to support abstract nouns in code - all programming relies on an ability to abstract verbs as functions, nouns as data structures, and either as processes.
 
Consider for example a sample Java fragment to represent some common farm "animals" to a level of abstraction suitable to model simple aspects of their hunger and feeding.It defines an <code>Animal</code> class to represent both the state of the animal and its functions:
Lets see how a Java Class hierarchy will look like for such an example:
 
<code>


<source lang = java>
Java Code


class Animal extends LivingThing
public abstract class BasketballPlayer
{
{
    private Location loc;
  private int number; //number on jersey
    private double energyReserves;
   public void dribble(); // abstract method declaration
    
  public void pass();
    boolean isHungry() {
  public void run()
        return energyReserves < 2.5;
    {
    }
      // code that can make a player run
    void eat(Food f) {
     }  
        // Consume food
        energyReserves += f.getCalories();
    }
    void moveTo(Location l) {
        // Move to new location
        loc = l;
     }
}
}
</source>
With the above definition, one could create objects of type <tt>Animal</tt> and call their methods like this:


<source lang=java>
public class Guard extends BasketballPlayer
thePig = new Animal();
{
theCow = new Animal();
  public void dribble() // abstract method implementation
if (thePig.isHungry()) {
  {
    thePig.eat(tableScraps);
    // code that can make my player dribble extraordinarily and block Center Players
  }
  public void pass()
  {
    // excellent pass throwing and blocking code
  }
}
}
if (theCow.isHungry()) {
 
    theCow.eat(grass);
public class Centre extends BasketballPlayer
{
  public void dribble() // abstract method implementation
  {
    // code that can make my player dribble extraordinarily and trick Gaurd Players
  }
  public void pass()
  {
    // excellent pass throwing and Recieving code
  }
  public void shoot() // method specific to a centre player
  {
    // shooting code goes here
  }
}
}
theCow.moveTo(theBarn);
</source>
In the above example, the class ''<code>Animal</code>'' is an abstraction used in place of an actual animal, ''<code>LivingThing</code>'' is a further abstraction (in this case a generalisation) of <code>Animal</code>.


If one requires a more differentiated hierarchy of animals — 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.
</code>
Such an abstraction could remove the need for the application coder to specify the type of food, so s/he could concentrate instead on the feeding schedule. The two classes could be related using inheritance or stand alone, and the programmer could define varying degrees of polymorphism between the two types. These facilities tend to vary drastically between languages, but in general each can achieve anything that is possible with any of the others. A great many operation overloads, data type by data type, can have the same effect at compile-time as any degree of inheritance or other means to achieve polymorphism. The class notation is simply a coder's convenience.
 
In the above example:
 
* Basketball Player is an abstract class
* Guard and Center are concrete implementations of abstract class BasketballPlayer
 
Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example
 
*Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be "Guard" or "Centre"
* Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'
* Inheritance: Both Guard and Centre inherit features like "dribble" and 'jersey number' from BasketballPlayer
* Polymorphism:  "Guard" and "Center" player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer
 
Also, something worth noting here is the way we will use any player whether he is "Guard" or "Centre" is the same as we have provided a common interface for them using Inheritance and Polymorphism.
 
===Summary===
As seen from our discussion Object Model inculcates the principles of Abstraction to enable the developer to create user friendly, consistent and reusable software.


===Object-oriented design===
Decisions regarding what to abstract and what to keep under the control of the coder become the major concern of object-oriented design and domain analysis — actually determining the relevant relationships in the real world is the concern of object-oriented analysis or legacy analysis.
In general, to determine appropriate abstraction, one must make many small decisions about scope (domain analysis), determine what other systems one must cooperate with (legacy analysis), then perform a detailed object-oriented analysis which is expressed within project time and budget constraints as an object-oriented design. In our simple example, the domain is the barnyard, the live pigs and cows and their eating habits are the legacy constraints, the detailed analysis is that coders must have the flexibility to feed the animals what is available and thus there is no reason to code the type of food into the class itself, and the design is a single simple Animal class of which pigs and cows are instances with the same functions. A decision to differentiate DairyAnimal would change the detailed analysis but the domain and legacy analysis would be unchanged—thus it is entirely under the control of the programmer, and we refer to abstraction in object-oriented programming as distinct from abstraction in domain or legacy analysis. Abstract Class: An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendent subclasses
===References ===
===References ===
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html  
#http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html  
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29
#http://www.service-architecture.com/database/articles/object_model_concepts.html  
#http://www.service-architecture.com/database/articles/object_model_concepts.html  
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html
#http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html
#http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29

Latest revision as of 00:52, 10 October 2009

Abstraction and the Object Model

Abstraction

Abstraction means hiding of details that are not relevant for the current viewpoint. For example, when we want search a path to reach an address in another city, we first consult a map of inter city highways. This map hides the city roads because they would clutter up the map too much. Once we have found a path to reach the destination city, we go down to the next lower level of abstraction, i.e. we consult the city road map to reach the desired locality. However this map hides small lanes and plot numbers. To locate the desired house number, we need the municipal survey map of that locality [1].

Abstraction can also be seen as a way to provide consistency to the user. For example, there are several car manufactures in the world, yet the interface offered to the drivers to operate any car is same. This consistency in the basic control mechanism enables the user to drive any car without any special training. A equivalent analogy in the software world will be GUI. Almost all applications developed these days have consistent GUI’s that is when you use any application you can be assured that there will be a tool bar and menu bar having options like print,edit open file etc.

In computer programming, abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures[2].

  • Control abstraction in involves the use of subprograms and related concepts control flows
  • Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype.

One can regard the notion of object from object-oriented programming as an attempt to combine abstractions of data and code [2].The following discussion on Object Models will show us how these principles of abstraction have been included in the Object Model.

Object Model

Object Models make use of following Four concepts or ideas to achieve abstraction principles. Also one can see these ideas as the corner stones of any Object Model. They are:

  • Data Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Encapsulation

The term encapsulation refers to the hiding of state details, but extending the concept of data type from earlier programming languages to associate behavior most strongly with the data, and standardizing the way that different data types interact, is the beginning of abstraction.

Data Abstraction

Data abstraction is a methodology that enables us to isolate how a compound data structure is used from the details of how it is constructed from more primitive data types. The basic idea of data abstraction is to structure the programs that are to use compound data objects so that they operate on ``abstract data. That is, our programs should use data in such a way as to make no assumptions about the data that are not strictly necessary for performing the task at hand. At the same time, a ``concrete data representation is defined independent of the programs that use the data. The interface between these two parts of our system will be a set of procedures, called selectors and constructors, that implement the abstract data in terms of the concrete representation[3].

Inheritance

Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.

Polymorphism

Literally Polymorphism means the ability to take multiple forms. In case of Object oriented Languages polymorphism is a feature that allows Objects of different data types to be handled using a uniform/consistent interface. Basically Polymorphism allows us to treat subclass objects in the same way as parent class objects and gives us the flexibility to add subclass specific behavior.

Abstraction in Object Oriented Programming Languages

In this section we will see how Java which is an Object Oriented Programming Language, supports the object model and abstraction principles. Java offers the key word "abstract" which we can use to create an abstract class. "An abstract class is a class whose instances can be created only in conjunction with an instance of one of its descendant subclasses." Seems complicated but it isn't. What we mean here is you cannot instantiate an object of an abstract class. To use the features of an abstract class we will have to create an instance of an object of a sub class. Lets take an example to understand this in a better way.

Consider the game of Basketball. Now a Basketball consists of several players that performed specialized tasks. Some of the common positions are "center", "forward" and "guard" . Though all of them are specialized positions they have several skills in common for instance all of these players must know how to "dribble" and "pass" to name a few.

Lets see how a Java Class hierarchy will look like for such an example:

Java Code

public abstract class BasketballPlayer {

  private int number; //number on jersey
  public void dribble(); // abstract method declaration
  public void pass();
  public void run()
   {
     // code that can make a player run
    }   

}

public class Guard extends BasketballPlayer {

  public void dribble() // abstract method implementation
  {
    // code that can make my player dribble extraordinarily and block Center Players
  } 
  public void pass()
  {
    // excellent pass throwing and blocking code 
  }

}

public class Centre extends BasketballPlayer {

  public void dribble() // abstract method implementation
  {
    // code that can make my player dribble extraordinarily and trick Gaurd Players
  } 
  public void pass()
  {
    // excellent pass throwing and Recieving code 
  }
  public void shoot() // method specific to a centre player
  {
    // shooting code goes here
  }

}

In the above example:

  • Basketball Player is an abstract class
  • Guard and Center are concrete implementations of abstract class BasketballPlayer

Now lets see how have we managed to include all the principles of abstraction and Object Modelling that we have discussed so far in this example

  • Data Abstraction: Our system uses 'jersey number' to identify players but the user will use the abstracted form which will be "Guard" or "Centre"
  • Encapsulation: By making 'jersey number' private we have encapsulated it from user's visibility and we can chose to provide methods for the user to manage the 'jersey number'
  • Inheritance: Both Guard and Centre inherit features like "dribble" and 'jersey number' from BasketballPlayer
  • Polymorphism: "Guard" and "Center" player have there own specific roles in the team but there true type is Basketballplayer that is they are forms of BasketballPlayer

Also, something worth noting here is the way we will use any player whether he is "Guard" or "Centre" is the same as we have provided a common interface for them using Inheritance and Polymorphism.

Summary

As seen from our discussion Object Model inculcates the principles of Abstraction to enable the developer to create user friendly, consistent and reusable software.

References

  1. http://highered.mcgraw-hill.com/sites/0070648255/information_center_view0/about_the_author.html
  2. http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29
  3. http://www.service-architecture.com/database/articles/object_model_concepts.html
  4. http://mitpress.mit.edu/sicp/full-text/sicp/book/node27.html