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
Line 3: Line 3:




Example: <br />
Example:  




<pre>
<pre>
'''// In Java'''<br /><br />
// In Java
class Car  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Class<br />
class Car  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Class
{<br />
{
int number_of_wheels=0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// state of an object<br />
int number_of_wheels=0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// state of an object
<br />
 
public void setWheels(int count_wheels) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// property of an object<br />
public void setWheels(int count_wheels) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// property of an object
{<br />
{
this.number_of_wheels=count_wheels;<br />
this.number_of_wheels=count_wheels;
}<br />
}
<br />
 
public int getWheels() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// property of an object<br />
public int getWheels() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// property of an object
{<br />
{
return this.number_of_wheels;<br />
return this.number_of_wheels;
}<br />
}
<br />
 
public static void main(String args[])<br />
public static void main(String args[])
{<br />
{
Car c1=new Car();   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Object creation<br />
Car c1=new Car();   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Object creation
c1.setWheels(4);<br />
c1.setWheels(4);
System.out.println(“Car has ”+c1.getWheels()+” wheels”);<br />
System.out.println(“Car has ”+c1.getWheels()+” wheels”);
<br />
 
}
}
<br />
 
}
}
<br /><br />
 
</pre>
</pre>
The four major principles of Object-oriented Languages are:<br />
The four major principles of Object-oriented Languages are:<br />
Line 80: Line 80:


<pre>
<pre>
$a=4;<br />
$a=4;
$a=3.16;<br />
$a=3.16;
$b=4.56 + $a;<br />
$b=4.56 + $a;
$c=5 + $a;<br />
$c=5 + $a;
print $b;<br />
print $b;
print $c;<br /><br />
print $c;


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''' ==
{| class="wikitable"
|-
! 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
|-
|}





Revision as of 05:38, 13 September 2011

Introduction

Object Oriented Languages came into existence in 1960's when there was major focus on Procedural Languages[1] with respect to polymorphism, modularization and data abstraction. The major entity in object oriented languages is object. Object [2] is an instance of a Class [3] 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 [4] 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 [5]
  • Inheritance [6]
  • Abstraction [7]
  • Encapsulation [8]


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. 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.


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 [9]. 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


Benefits of Object Oriented Languages to Scripting Languages

  • Inheritance : Some of the object oriented languages implements multiple inheritance [10] and some implements multi-level inheritance [11]. The perfect example of scripting language that implements multiple inheritance is Python and multi-level inheritance is Ruby.
  • Access Modifiers [12] : 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 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 [13]
  • 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
  • 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.


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


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