Csc/ece 517 fall 2011/ch1 1g aa: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
== '''Introduction''' ==
== '''Introduction''' ==
Object Oriented Languages came into existence in 1960's when there was major focus on Procedural Languages[http://en.wikipedia.org/wiki/Procedural_programming] with respect to  polymorphism, modularization and data abstraction. The major entity in object oriented languages is object. Object [http://en.wikipedia.org/wiki/Object_(computer_science)] is an instance of a Class [http://en.wikipedia.org/wiki/Class_(computer_programming)] and Class acts as a blueprint for  an  Object.Class consists of methods and data that instances of class operate on. Methods in object-oriented is same as functions or subroutine [http://en.wikipedia.org/wiki/Subroutine] in procedural programming. Data in object oriented programming is used to store the state of an object.
Object Oriented Languages[http://en.wikipedia.org/wiki/Object-oriented_programming] came into existence in 1960's when there was major focus on Procedural Languages[http://en.wikipedia.org/wiki/Procedural_programming] with respect to  polymorphism, modularization and data abstraction. The major entity in object oriented languages is object. Object [http://en.wikipedia.org/wiki/Object_(computer_science)] is an instance of a Class [http://en.wikipedia.org/wiki/Class_(computer_programming)] and Class acts as a blueprint for  an  Object.Class consists of methods and data that instances of class operate on. Methods in object-oriented is same as functions or subroutine [http://en.wikipedia.org/wiki/Subroutine] in procedural programming. Data in object oriented programming is used to store the state of an object.




Line 42: Line 42:


== '''Classification of Object Oriented Languages''' ==
== '''Classification of Object Oriented Languages''' ==
Object Oriented Languages are being classified into two types: Class-based and prototype-based. Class-based Object Oriented Languages mainly focus on creation of objects using classes. Classes includes both state and properties of an object(instance of class).Some examples of commonly used class based object oriented languages are Ada, C++, C#, Cobra, Fortran, Java, Perl, PHP, Python, Ruby, Scala, Smalltalk, VBScript etc. Prototype-based Object Oriented Languages are those in which there exists no difference between classes and objects. Some examples of commonly used prototype-based languages are ABCL, Cecil, ActionScript, JavaScript, Lisaac, NewtonScipt, Slate, TADS etc.
Object Oriented Languages are being classified into two types: Class-based and prototype-based.   
Among these two kinds of object-oriented languages, there are many scripting languages which possesses  prominent Object Oriented features. Some of the scripting languages that comes into this category are ActionScript, JavaScript, Groovy, Perl, PHP, Python, Ruby, Smalltalk, VBScript, Windows PowerShell etc.
Among these two kinds of object-oriented languages, there are many scripting languages which possesses  prominent Object Oriented features. Some of the scripting languages that comes into this category are ActionScript, JavaScript, Groovy, Perl, PHP, Python, Ruby, Smalltalk, VBScript, Windows PowerShell etc.
=== '''Class based object oriented languages''' ===
Class-based Object Oriented Languages mainly focus on creation of objects using classes. Classes includes both state and properties of an object(instance of class).Some  examples of commonly used class based object oriented languages are Ada, C++, C#, Cobra, Fortran, Java, Perl, PHP, Python, Ruby, Scala, Smalltalk, VBScript etc.
=== '''Prototype based object oriented languages''' ===
Prototype-based Object Oriented Languages are those in which there exists no difference between classes and objects. Some examples of commonly used prototype-based languages are ABCL, Cecil, ActionScript, JavaScript, Lisaac, NewtonScipt, Slate, TADS etc.




== '''Scripting Languages''' ==
== '''Scripting Languages''' ==
A scripting language is a high level programming language that interprets a normal interactive program and allocates it a list of tasks that need to be done by the program at runtime [http://www.webopedia.com/TERM/S/scripting_language.html]. Scripting languages gained popularity as internet became a powerful communication tool. Scripting languages can be used on the client side and on the server side. On the client side, scripts can be used to change data that end user views in the browser window. While on the server side, scripts can be used to manipulate data in the repository / database. Some of the popular scripting languages include Perl, Python, PHP, JavaScript, Active Server Pages, JSP , Tcl
A scripting language is a high level programming language that interprets a normal interactive program and allocates it a list of tasks that need to be done by the program at runtime [http://www.webopedia.com/TERM/S/scripting_language.html]. Scripting languages gained popularity as internet became a powerful communication tool. Scripting languages can be used on the client side and on the server side. On the client side, scripts can be used to change data that end user views in the browser window. While on the server side, scripts can be used to manipulate data in the repository / database.
 
 
=== '''When to use scripting languages?''' ===
Scripting languages are used when the speed of development is more important than the speed of execution. One instance that answers the question that "When to use scripting languages?" is that, say we have a ton of numeric data in a file and we need to sort all the numbers within the file, in such scenario scripting comes handy as we can do sorting by collecting the file data in a list / array (list_name) and then calling the sort(@list_name) procedure.
<pre>
//sort function written in C++
void class_name::sort(int a[],length)
{
int i,j,temp;
for(i=0;i<length;i++)
  for(j=1;j<length;j++)
        if(a[i]<a[j])
        {
        temp=a[i];              //temp variable is used to swap the values iff a[i] < a[j]
        a[i]=a[j];
        a[j]=temp;
        }
}
</pre>
 
AS you can see for sorting a array / list we have to write modules in object oriented languages.
 
<pre>
@array;                            #this snippet is written in Perl
@array1=sort(@array);              #thus array 1 will contain the sorted list
</pre>
 
In scripting languages the library has functions like sort that make the task of developers much easier especially when a deliverable is due soon and if a developer has a imminent deadline. 
 
Some of the popular scripting languages include Perl, Python, PHP, JavaScript, Active Server Pages, JSP , Tcl




Line 65: Line 103:
* Scripting requires minimum programming knowledge or experience.  
* Scripting requires minimum programming knowledge or experience.  


* At times scripting can do the same task in fewer LOC compared to OO Languages. For instance to sort a list using an OO language would require the user to code the procedure that might take approximately 25-30 LOC. However in a scripting language such as Perl using a command called “sort” within the script will perform the same task in 3-4 lines [http://www.sqa.org.uk/e-learning/ClientSide01CD/page_22.htm]  
* At times scripting can do the same task in fewer LOC [http://en.wikipedia.org/wiki/Source_lines_of_code] compared to OO Languages. For instance to sort a list using an OO language would require the user to code the procedure that might take approximately 25-30 LOC. However in a scripting language such as Perl using a command called “sort” within the script will perform the same task in 3-4 lines [http://www.sqa.org.uk/e-learning/ClientSide01CD/page_22.htm]  


* Scripting allows faster editing and code running.  
* Scripting allows faster editing and code running.  
Line 71: Line 109:


== '''Features of Scripting languages that are not in OO Languages''' ==
== '''Features of Scripting languages that are not in OO Languages''' ==
* Scripting languages do not enforce type declaration unlike the OO Languages   
* Scripting languages do not enforce type declaration unlike the OO Languages  
<pre>
//Snippet built in c++
//sum has a local scope
int class_name::sum_of_num(int a, int b)
{
sum = a + b;
return sum;
}
</pre>
 
If we compile this code the compiler will throw an exception saying that "sum" is a variable that is not defined. This is because the compiler needs to know at runtime the data type of all the variables used.  
 
<pre>
def sum_of_num(a,b)              #this function is written in Ruby
@sum=a+b
end
</pre>
 
Since most of the scripting languages are dynamically typed i.e we need not specify the data types of the variables. The compiler determines the data type automatically at runtime.


* In scripting languages such as Perl one can define an array using @ and insert any number of elements in the list. However in OO languages you need to create an object first and then perform specific tasks.
 
* In scripting languages such as Perl, Ruby etc. one can define an array using @ and insert any number of elements in the list. However in OO languages you need to create an object first and then perform specific tasks.
 
<pre>
//This code snippet is built in C++
int a[100];
char b[100];
</pre>
 
When the compiler encounters these declarations the compiler will reserve 100 integer blocks for a and 100 character blocks for b. We must ensure that we do not exceed 100 entities in both the above arrays. If we do so we will get a segmentation fault. This can be overcome by using dynamic allocation of memory.
 
<pre>
@array=[1, "metallica", 3.14, 6];              #Perl array declaration
</pre>
 
Thus we observe that in most scripting languages we can store any data type element in an array and access individual element by their index. Scripting has many functions that allows developers to play with an array. Check the following example:
 
<pre>
$last=pop(@array);              #pop method will store 6 in $last
push @array,8                    #will store 8 in the last location
$first=shift @array              #will return and delete the first element from the array and assign to $first
unshift(@array,3)                #will add 3 to the beginning of the array.
</pre>
 
Just imagine if you were to implement these function in an object oriented languages it would take a lot of effort and lines of code to build the same functionality which in case of scripting will take just a few lines of code.
 
Scripting languages make use of negative index for arrays to read an array backward.
 
<pre>
@array = [1,2,3,4,5];
print array[-1];              #-1 will return the last element in the array i.e 5
</pre>




Line 89: Line 177:
The value stored in $b will be 7.72 and for $c will store 9.
The value stored in $b will be 7.72 and for $c will store 9.
</pre>
</pre>


== '''Comparison between Scripting languages and Object Oriented languages''' ==  
== '''Comparison between Scripting languages and Object Oriented languages''' ==  
Line 169: Line 258:
12 http://en.wikibooks.org/wiki/Java_Programming/Access_Modifiers <br />
12 http://en.wikibooks.org/wiki/Java_Programming/Access_Modifiers <br />
13 http://www.sqa.org.uk/e-learning/ClientSide01CD/page_22.htm <br />
13 http://www.sqa.org.uk/e-learning/ClientSide01CD/page_22.htm <br />
14 http://www.jvoegele.com/software/langcomp.html
14 http://en.wikipedia.org/wiki/Source_lines_of_code <br />
15 http://perldoc.perl.org/functions/sort.html <br />
16 http://www.jvoegele.com/software/langcomp.html

Latest revision as of 07:23, 25 September 2011

Introduction

Object Oriented Languages[1] came into existence in 1960's when there was major focus on Procedural Languages[2] with respect to polymorphism, modularization and data abstraction. The major entity in object oriented languages is object. Object [3] is an instance of a Class [4] and Class acts as a blueprint for an Object.Class consists of methods and data that instances of class operate on. Methods in object-oriented is same as functions or subroutine [5] in procedural programming. Data in object oriented programming is used to store the state of an object.


Example:


// In Java
class Car  		                        //Class
{
int number_of_wheels=0;				                        // state of an object

public void setWheels(int count_wheels)		                        // property of an object
{
this.number_of_wheels=count_wheels;
}

public int getWheels()				                        // property of an object
{
return this.number_of_wheels;
}

public static void main(String args[])
{
Car c1=new Car();	  			                        	// Object creation
c1.setWheels(4);
System.out.println(“Car has ”+c1.getWheels()+” wheels”);

}

}

The four major principles of Object-oriented Languages are:

  • Polymorphism [6]
  • Inheritance [7]
  • Abstraction [8]
  • Encapsulation [9]


Classification of Object Oriented Languages

Object Oriented Languages are being classified into two types: Class-based and prototype-based. Among these two kinds of object-oriented languages, there are many scripting languages which possesses prominent Object Oriented features. Some of the scripting languages that comes into this category are ActionScript, JavaScript, Groovy, Perl, PHP, Python, Ruby, Smalltalk, VBScript, Windows PowerShell etc.


Class based object oriented languages

Class-based Object Oriented Languages mainly focus on creation of objects using classes. Classes includes both state and properties of an object(instance of class).Some examples of commonly used class based object oriented languages are Ada, C++, C#, Cobra, Fortran, Java, Perl, PHP, Python, Ruby, Scala, Smalltalk, VBScript etc.


Prototype based object oriented languages

Prototype-based Object Oriented Languages are those in which there exists no difference between classes and objects. Some examples of commonly used prototype-based languages are ABCL, Cecil, ActionScript, JavaScript, Lisaac, NewtonScipt, Slate, TADS etc.


Scripting Languages

A scripting language is a high level programming language that interprets a normal interactive program and allocates it a list of tasks that need to be done by the program at runtime [10]. Scripting languages gained popularity as internet became a powerful communication tool. Scripting languages can be used on the client side and on the server side. On the client side, scripts can be used to change data that end user views in the browser window. While on the server side, scripts can be used to manipulate data in the repository / database.


When to use scripting languages?

Scripting languages are used when the speed of development is more important than the speed of execution. One instance that answers the question that "When to use scripting languages?" is that, say we have a ton of numeric data in a file and we need to sort all the numbers within the file, in such scenario scripting comes handy as we can do sorting by collecting the file data in a list / array (list_name) and then calling the sort(@list_name) procedure.

//sort function written in C++
void class_name::sort(int a[],length)
{
int i,j,temp;
for(i=0;i<length;i++)
   for(j=1;j<length;j++)
        if(a[i]<a[j])
        {
         temp=a[i];               //temp variable is used to swap the values iff a[i] < a[j]
         a[i]=a[j];
         a[j]=temp;
        }
}

AS you can see for sorting a array / list we have to write modules in object oriented languages.

@array;                            #this snippet is written in Perl
@array1=sort(@array);              #thus array 1 will contain the sorted list

In scripting languages the library has functions like sort that make the task of developers much easier especially when a deliverable is due soon and if a developer has a imminent deadline.

Some of the popular scripting languages include Perl, Python, PHP, JavaScript, Active Server Pages, JSP , Tcl


Benefits of Object Oriented Languages to Scripting Languages

  • Inheritance : Some of the object oriented languages implements multiple inheritance [11] and some implements multi-level inheritance [12]. The perfect example of scripting language that implements multiple inheritance is Python and multi-level inheritance is Ruby.
  • Access Modifiers [13] : Object Oriented features of using the access modifiers (public, protected, private) for member variables and methods helped scripting languages in controlling the access to the same. Access modifiers are being commonly used in most of the scripting languages like Ruby.
  • Polymorphism : This feature helps Scripting languages to override methods that are already present in base class and overload the methods present in base class or its own class. Good example for the same is method overriding in Ruby and method overloading implemented in Google's Go scripting language.
  • Encapsulation : This object oriented feature helps scripting languages in structuring the methods and member data having same type of functionality to be defined in one class or interface. However, not all scripting languages support this feature but most of them do support. Python is one of the language that does not support encapsulation. Ruby and Perl do support encapsulation.


Advantages incorporated by scripting to OO languages

  • Scripts are easy to use and learn.
  • Scripting requires minimum programming knowledge or experience.
  • At times scripting can do the same task in fewer LOC [14] compared to OO Languages. For instance to sort a list using an OO language would require the user to code the procedure that might take approximately 25-30 LOC. However in a scripting language such as Perl using a command called “sort” within the script will perform the same task in 3-4 lines [15]
  • Scripting allows faster editing and code running.


Features of Scripting languages that are not in OO Languages

  • Scripting languages do not enforce type declaration unlike the OO Languages
//Snippet built in c++
//sum has a local scope 
int class_name::sum_of_num(int a, int b)
{
sum = a + b;
return sum;
}

If we compile this code the compiler will throw an exception saying that "sum" is a variable that is not defined. This is because the compiler needs to know at runtime the data type of all the variables used.

 
def sum_of_num(a,b)               #this function is written in Ruby 
@sum=a+b
end

Since most of the scripting languages are dynamically typed i.e we need not specify the data types of the variables. The compiler determines the data type automatically at runtime.


  • In scripting languages such as Perl, Ruby etc. one can define an array using @ and insert any number of elements in the list. However in OO languages you need to create an object first and then perform specific tasks.
//This code snippet is built in C++
int a[100];
char b[100];

When the compiler encounters these declarations the compiler will reserve 100 integer blocks for a and 100 character blocks for b. We must ensure that we do not exceed 100 entities in both the above arrays. If we do so we will get a segmentation fault. This can be overcome by using dynamic allocation of memory.

@array=[1, "metallica", 3.14, 6];               #Perl array declaration

Thus we observe that in most scripting languages we can store any data type element in an array and access individual element by their index. Scripting has many functions that allows developers to play with an array. Check the following example:

$last=pop(@array);               #pop method will store 6 in $last
push @array,8                    #will store 8 in the last location
$first=shift @array              #will return and delete the first element from the array and assign to $first
unshift(@array,3)                #will add 3 to the beginning of the array. 

Just imagine if you were to implement these function in an object oriented languages it would take a lot of effort and lines of code to build the same functionality which in case of scripting will take just a few lines of code.

Scripting languages make use of negative index for arrays to read an array backward.

@array = [1,2,3,4,5];
print array[-1];               #-1 will return the last element in the array i.e 5


Is scripting synonymous to dynamic typing

Yes scripting is synonymous to dynamic typing. As discussed earlier one need not specify the type of variable used in scripting. The compiler determines the context of the variable and uses it accordingly at run time.

$a=4;
$a=3.16;
$b=4.56 + $a;
$c=5 + $a;
print $b;
print $c;

The value stored in $b will be 7.72 and for $c will store 9.


Comparison between Scripting languages and Object Oriented languages

Features Ruby Java Perl C# C++ Visual Basic
Object Orientation Pure Hybrid Add-on / Hybrid Hybrid Hybrid / Multi-Paradigm Partial support
Static / Dynamic typing Dynamic Static Dynamic Static Static Static
Generic classes N/A No N/A No Yes No
Inheritance Single class, multiple "mixins" Single class multiple interfaces Multiple Single class multiple interfaces Multiple None
Regular expressions Built in Standard library Built in Standard library No No
Operator Overloading Yes No Yes Yes Yes No

[16]


References

1 http://en.wikipedia.org/wiki/Procedural_programming
2 http://en.wikipedia.org/wiki/Object_(computer_science)
3 http://en.wikipedia.org/wiki/Class_(computer_programming)
4 http://en.wikipedia.org/wiki/Subroutine
5 http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
6 http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
7 http://en.wikipedia.org/wiki/Abstraction_(computer_science)
8 http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)
9 http://www.webopedia.com/TERM/S/scripting_language.html
10 http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
11 http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
12 http://en.wikibooks.org/wiki/Java_Programming/Access_Modifiers
13 http://www.sqa.org.uk/e-learning/ClientSide01CD/page_22.htm
14 http://en.wikipedia.org/wiki/Source_lines_of_code
15 http://perldoc.perl.org/functions/sort.html
16 http://www.jvoegele.com/software/langcomp.html