CSC/ECE 517 Fall 2009/wiki2 15 ms: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(51 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=Introduction=
=Introduction=
"Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which repressents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships."<i>taken from Object-Oriented Analysis and Design by Grady Booch</i><br />
"Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which repressents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships."<i>taken from Object-Oriented Analysis and Design by Grady Booch</i><br />
For all the things that we define as object oriented, the main framework is the object model. An object model can be defined as the collection of principles that form the foundation of object oriented design. It emphasizes on the principles of abstraction, encapsulation, modularity, hierarchy, typing, concurrency and persistence. There are main elements of an object model, namely -:
For all the things that we define as object oriented, the main framework is the object model. An object model can be defined as the collection of principles that form the foundation of object oriented design. The main elements of an object model are -:
*Abstraction
*Abstraction
*Encapsulation
*Encapsulation - It means that object's data is hidden in the object and access to it is limited to the members of the class.
*Modularity
*Modularity - It means making a software as a collection of different modules which have different functionality.
*Hierarchy
*Hierarchy - It enables classifying relationship between objects.


The goal of this article is to describe how abstraction relates to object oriented languages.
The goal of this article is to describe how abstraction relates to object oriented languages.


=Definition of Abstraction=
=Definition of Abstraction=
Abstraction (from the Latin abs, meaning away from and trahere, meaning to draw) is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.<i>[taken from [http://www.tutorialspoint.com/ruby/ruby_modules.htm what is abstraction?]]</i>
Abstraction (from the Latin abs, meaning away from and trahere, meaning to draw) is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.<i>[taken from [http://www.tutorialspoint.com/ruby/ruby_modules.htm What is abstraction?]]</i>


An alternate definition given in the book object oriented analysis and design,states that-:<br />
An alternate definition given in the book object oriented analysis and design,states that-:<br />
Line 17: Line 17:


The gist is that abstraction is a concept wherein all but the relevant information about an object is hidden in order to reduce complexity and increase the efficiency. This concept even applies in the field of arts wherein an object or a piece of art created by an artist is a representation of the original model with all the unwanted details removed.
The gist is that abstraction is a concept wherein all but the relevant information about an object is hidden in order to reduce complexity and increase the efficiency. This concept even applies in the field of arts wherein an object or a piece of art created by an artist is a representation of the original model with all the unwanted details removed.
In other words it is nothing but recognizing the similarities between objects and ignoring for some time their differences. It focuses on the outside view of the object and separate it's behavior from it's implementation.
In other words it is nothing but recognizing the similarities between objects and ignoring for some time their differences. It focuses on the outside view of the object and separate it's behavior from it's implementation. <br />
<table  BORDER=3>
<tr>
<td>
[[Image:Abstraction.jpg]]
</td>
</tr>
</table>
'''Abstraction focuses on the essential characteristics of some object, relative to perspective of the viewer.'''<br />
The above picture shows a car which is being viewed by two people. One of them is a mechanic and the other is a layman. Now when
mechanic thinks of a car, what comes to his mind are it's parts, mileage etc. When a layman thinks about a car, what comes to
his mind is color of the car, the look etc. Hence each person sees the same object in a different light.


=Types of abstractions=
=Types of abstractions=
There are different types of abstractions as suggested by Seidewitz and Stark of which some closely model problem domain entities and some which have no reason for existence. These are as following (mentioned in the order of their usefulness) -:<br />
There are different types of [http://delivery.acm.org/10.1145/30000/25315/p54-seidewitz.pdf?key1=25315&key2=8831215521&coll=GUIDE&dl=GUIDE&CFID=55753397&CFTOKEN=61066169 abstractions as suggested by Seidewitz and Stark] of which some closely model problem domain entities These are as following (mentioned in the order of their usefulness) -:<br />
 
* Entity abstraction - Object represents a useful model of a problem domain entity.
 
* Action abstraction - Object provides a set of operations which have the same functionality


* Entity abstraction - This is where an object represents a useful model of a problem domain or solution domain entity.
* Action abstraction - This is when an object provides a generalized set of operations, all of which perform the same kind of function.
* Virtual Machine abstraction - This is where an object groups together operations that are all used by some superior level of control, or operations that all use some junior-level set of operations.
* Virtual Machine abstraction - This is where an object groups together operations that are all used by some superior level of control, or operations that all use some junior-level set of operations.
* Conincidental abstraction - An object that packages a set of operations that have no relation to each other.


Entity abstractions is what is used widely because it directly parallels a given problem domain.<br />
* Conincidental abstraction - An object that puts together a set of operations that are not co-related
 
Entity abstractions is what is used widely because it directly relates to a given problem domain.<br />
 
==Key Abstractions==
 
A abstraction which describes the system is referred as a [http://epf.eclipse.org/wikis/openup/core.tech.common.extend_supp/guidances/concepts/key_abstractions_1474DBF2.html key abstraction]. They give boundaries to our problem.They highlight things that are more relevant to our system and hence to the design and suppress the things that are outside the system. There are two important steps in identifying the key abstractions. These are -:<br />
 
* Discovery : Discovery is the process of recognizing the abstraction used in the design of the system.
For example, a customer doing a bank transaction speaks in terms of accounts,deposits and withdrawals;
these works are part of the vocabulary of the problem domain.
 
* Invention : Through invention, we can create new classes and objects that are not necessarily part of the problem domain, but are useful in design and implementation.
For example A developer of such a system uses these same abstractions, but must also introduce new ones, such as databases, screen 
mangers, lists, queues and so on. These key abstractions are artifacts of the particular design, not of the problem domain.
 
= Abstraction in Different O-O languages =
 
== Java ==
Java implements abstraction using Abstract classes and methods. Any class which cannot be instantiated and contains one or more abstract methods in referred as an '''Abstract class''' in JAVA. Such class can only be inherited.
 
The code shown below is an implementation of the picture example given previously. Here, there is an abstract class called 'Car' which contains an abstract method called 'thingsILookFor()'. Classes 'Mechanic' and 'LayMan' extend the class 'Car'. Each of these classes implement the method relative to their own perception of the method thingsILookFor().
 
''' Example :  Java code for the above figure.'''
<font color = green >
/* The abstract class car is extended by both the classes Mechanic and LayMan.        */
/* They implement the abstract method thingsILookFor                                  */
/* according to what they consider important in a car                                  */</font>
<font color = brown>
public abstract class Car {
abstract void thingsILookFor();
}
public class Mechanic extends Car {
private int enginePower;
 
private int carMileage;
  void thingsILookFor() {
 
  enginePower = 1000;
 
  carMileage = 32;
  }
 
}
public class LayMan extends Car {
 
private string color;
private int seats;
  void thingsILookFor() {
  color = "yellow";
  seats = 4;
  }
  </font>
 
== C++ ==
'''Abstract classes:''' These classes can be used as a framework to build new classes that provide new functionality. These class have one or more virtual function. 
Functions of the base class whose functionality can be overridden by functions of the derived class at run time are called '''virtual functions'''.
 
In the code below the Class 'Car' has a virtual function 'things_I_like'. This method is implemented by the Class Mechanic and LayMan according the perspective of the class.
 
<font color = red>
class Car
{
public:
      virtual void things_I_like()
      {
          cout << " No method implementation \n";
      }
}
class Mechanic:public car
{
public:
      virtual void things_I_like()
      {
          cout << " I like the car to have a powerful engine\n";
      }
 
}
class LayMan:public car
{
public:
      virtual void dump()
      {
          cout << " I like the car to be spacious and have bright colors\n";
      }
 
}
</font>
 
== Ruby ==
Ruby has no Abstract classes like in Java or Virtual function like in C++, but the same functionality can be implemented as follows.
 
In this example the class 'Car' has not implemented the method 'thingsILike()'. Suppose the class 'Mechanic' and 'LayMan' were not defined and now if we call Car.new.thingsILike, it will raise an error. Hence this method needs to be implemented by the subclasses as shown in the example.
 
In the code below the method 'thingsILike' is implemented differently by the class Mechanic and LayMan. They implement the method according to the context in which they see the class 'Car'.
 
<font color = blue>
class Car
  def thingsILike()
  raise NotImplementedError.new("no implementation found \n")
  end
end
class Mechanic < Car
  def thingsILike()
  puts " I like the horse power of the engine and the mileage \n"
  end
end
class LayMan < Car
  def thingsILike()
  puts " I like the color \n"
  end
end
</font>


<i>Definitions taken from object oriented analysis and design by Grady Booch</i>
=References=
* [http://www.amazon.com/Object-Oriented-Analysis-Design-Applications-2nd/dp/0805353402 Object oriented Analysis and design by Grady Booch]


=Mechanisms of finding key Abstractions=
* [http://searchcio-midmarket.techtarget.com/sDefinition/0,,sid183_gci343038,00.html Definition of Abstraction]
A key abstraction is an abstraction without which we cannot describe the system. They give boundaries to our problem.They highlight things that are more relevant to our system and hence to the design and suppress the things that are outside the system. There are two important steps in identifying the key abstractions. These are -:<br />
* [http://epf.eclipse.org/wikis/openup/core.tech.common.extend_supp/guidances/concepts/key_abstractions_1474DBF2.html Key Abstraction]
* [http://www.mymindleaks.com/blog/programming/object-oriented-programming-abstraction/ Object Oriented Programming - Abstraction]
* [http://www.madsci.org/posts/archives/apr2001/987192403.Eg.r.html What is meant by abstraction?]
* [http://www.codeproject.com/KB/architecture/OOP_Concepts_and_manymore.aspx#Generalization What is abstraction?]
* [http://www.answers.com/topic/object-oriented-programming A discussion on object oriented programming]
* [http://computer-programming-tutorials.suite101.com/article.cfm/a_brief_introduction_to_objectoriented_design An example on Anstraction]
* [http://delivery.acm.org/10.1145/30000/25315/p54-seidewitz.pdf?key1=25315&key2=8831215521&coll=GUIDE&dl=GUIDE&CFID=55753397&CFTOKEN=61066169 A paper on "Toward a general object-oriented software development methodology"]
* [http://www.research.att.com/~bs/abstraction-and-machine.pdf An article on Abstraction by Bjarne Stroustrup]


* Discovery
=Further Reading=
* Invention
* [http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/OOP_ObjC/Articles/ooObjectModel.html iphone reference library - The object model]
* [http://java.sun.com/docs/books/tutorial/java/concepts/ Lesson: Object-Oriented Programming Concepts]
* [http://computing.southern.edu/halterman/OOPJ/ A textbook on Object oriented programming using Java]

Latest revision as of 03:15, 15 October 2009

Introduction

"Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which repressents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships."taken from Object-Oriented Analysis and Design by Grady Booch
For all the things that we define as object oriented, the main framework is the object model. An object model can be defined as the collection of principles that form the foundation of object oriented design. The main elements of an object model are -:

  • Abstraction
  • Encapsulation - It means that object's data is hidden in the object and access to it is limited to the members of the class.
  • Modularity - It means making a software as a collection of different modules which have different functionality.
  • Hierarchy - It enables classifying relationship between objects.

The goal of this article is to describe how abstraction relates to object oriented languages.

Definition of Abstraction

Abstraction (from the Latin abs, meaning away from and trahere, meaning to draw) is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.[taken from What is abstraction?]

An alternate definition given in the book object oriented analysis and design,states that-:

"An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries,relative to the perceptive of the viewer."

The gist is that abstraction is a concept wherein all but the relevant information about an object is hidden in order to reduce complexity and increase the efficiency. This concept even applies in the field of arts wherein an object or a piece of art created by an artist is a representation of the original model with all the unwanted details removed. In other words it is nothing but recognizing the similarities between objects and ignoring for some time their differences. It focuses on the outside view of the object and separate it's behavior from it's implementation.

Abstraction focuses on the essential characteristics of some object, relative to perspective of the viewer.
The above picture shows a car which is being viewed by two people. One of them is a mechanic and the other is a layman. Now when mechanic thinks of a car, what comes to his mind are it's parts, mileage etc. When a layman thinks about a car, what comes to his mind is color of the car, the look etc. Hence each person sees the same object in a different light.

Types of abstractions

There are different types of abstractions as suggested by Seidewitz and Stark of which some closely model problem domain entities These are as following (mentioned in the order of their usefulness) -:

  • Entity abstraction - Object represents a useful model of a problem domain entity.
  • Action abstraction - Object provides a set of operations which have the same functionality
  • Virtual Machine abstraction - This is where an object groups together operations that are all used by some superior level of control, or operations that all use some junior-level set of operations.
  • Conincidental abstraction - An object that puts together a set of operations that are not co-related

Entity abstractions is what is used widely because it directly relates to a given problem domain.

Key Abstractions

A abstraction which describes the system is referred as a key abstraction. They give boundaries to our problem.They highlight things that are more relevant to our system and hence to the design and suppress the things that are outside the system. There are two important steps in identifying the key abstractions. These are -:

  • Discovery : Discovery is the process of recognizing the abstraction used in the design of the system.
For example, a customer doing a bank transaction speaks in terms of accounts,deposits and withdrawals; 
these works are part of the vocabulary of the problem domain.
  • Invention : Through invention, we can create new classes and objects that are not necessarily part of the problem domain, but are useful in design and implementation.
For example A developer of such a system uses these same abstractions, but must also introduce new ones, such as databases, screen  
mangers, lists, queues and so on. These key abstractions are artifacts of the particular design, not of the problem domain.

Abstraction in Different O-O languages

Java

Java implements abstraction using Abstract classes and methods. Any class which cannot be instantiated and contains one or more abstract methods in referred as an Abstract class in JAVA. Such class can only be inherited.

The code shown below is an implementation of the picture example given previously. Here, there is an abstract class called 'Car' which contains an abstract method called 'thingsILookFor()'. Classes 'Mechanic' and 'LayMan' extend the class 'Car'. Each of these classes implement the method relative to their own perception of the method thingsILookFor().

 Example :  Java code for the above figure.


/* The abstract class car is extended by both the classes Mechanic and LayMan.         */
/* They implement the abstract method thingsILookFor                                   */
/* according to what they consider important in a car                                  */


public abstract class Car {

abstract void thingsILookFor();

}

public class Mechanic extends Car {

private int enginePower;
 
private int carMileage;

 void thingsILookFor() {
 
 enginePower = 1000;
 
 carMileage = 32; 
 }
 
}

public class LayMan extends Car {
 
private string color;

private int seats;

 void thingsILookFor() {

 color = "yellow";

 seats = 4; 
 }
 

C++

Abstract classes: These classes can be used as a framework to build new classes that provide new functionality. These class have one or more virtual function. Functions of the base class whose functionality can be overridden by functions of the derived class at run time are called virtual functions.

In the code below the Class 'Car' has a virtual function 'things_I_like'. This method is implemented by the Class Mechanic and LayMan according the perspective of the class.

class Car
{

public:
      virtual void things_I_like()
      {
          cout << " No method implementation \n";
      }
}


class Mechanic:public car
{

public:
      virtual void things_I_like()
      {
          cout << " I like the car to have a powerful engine\n";
      }
  
}
class LayMan:public car
{

public:
      virtual void dump()
      {
          cout << " I like the car to be spacious and have bright colors\n";
      }
  
}

Ruby

Ruby has no Abstract classes like in Java or Virtual function like in C++, but the same functionality can be implemented as follows.

In this example the class 'Car' has not implemented the method 'thingsILike()'. Suppose the class 'Mechanic' and 'LayMan' were not defined and now if we call Car.new.thingsILike, it will raise an error. Hence this method needs to be implemented by the subclasses as shown in the example.

In the code below the method 'thingsILike' is implemented differently by the class Mechanic and LayMan. They implement the method according to the context in which they see the class 'Car'.

class Car
 def thingsILike()
  raise NotImplementedError.new("no implementation found \n")
 end
end

class Mechanic < Car
 def thingsILike()
  puts " I like the horse power of the engine and the mileage \n"
 end
end

class LayMan < Car
 def thingsILike()
  puts " I like the color \n"
 end
end

References

Further Reading