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

From Expertiza_Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 37: Line 37:


<code>
<code>
Java Code
public abstract class BasketballPlayer
public abstract class BasketballPlayer
{
{
Line 92: Line 95:
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.
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.


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.
===Summary===
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.
As seen from our discussion Object Model inculcates the principles of Abstraction to enable the developer to create user friendly, consistent and reusable software.
 
===Conclusion===
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.


===References ===
===References ===

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