CSC/ECE 517 Fall 2012/ch1 1w17 ir: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(107 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<big><big>'''Extending Objects'''</big></big>
<big><big>'''Extending Objects'''</big></big>
----
----
The ‘extend’ feature in computer programming languages allow the programmer to dynamically extend the functionality of an object at runtime, as opposed to extending functionality at compile time. This feature can be found in Object Oriented Programming (OOP) Languages.  
The ‘extend’ feature in computer programming languages allows the programmer to dynamically extend the functionality of an object at run-time, as opposed to extending functionality at compile time. This feature can be found in most Object Oriented Programming (OOP) Languages, some of which are Java, JavaScript, and Ruby. Some OOP languages do not support the 'extend' feature, such as C++.
 
__TOC__
__TOC__


== Introduction ==
= Introduction =
The programming paradigm OOP uses 'objects' for designing applications. These objects consist of an encapsulated set of data fields and methods, and are usually an instance of a class. These objects can communicate with each other through messages (or 'methods') and process data in [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]. But in order to increase an object's functionality, it is necessary to add the features of one object to another. This can be done dynamically at run time, without repeating the code, by extending the features of one class to another. This can be done by the use of the 'extend' keyword. Many OOP oriented languages support the 'extend' feature.
 
The Object Oriented Programming uses 'objects' for designing applications. These objects consist of an encapsulated set of data fields and methods, and are usually an instance of a class. These objects can communicate with each other through messages (or 'methods') and process data in [http://en.wikipedia.org/wiki/Object-oriented_programming object oriented programming]. But in order to increase an object's functionality, it is necessary to add the features of one object to another. This can be done dynamically at run time, without repeating the code, by extending the features of one class to another. This can be done by the use of the 'extend' keyword. Many Object Oriented Programming support the 'extend' feature.
 
''An object is an entity that serves as a container for data and also controls access to the data. Associated with an object is a set of attributes, which are essentially no more than variables belonging to that object. Also associated with an object is a set of functions that provide an interface to the functionality of the object, called methods. - Hal Fulton''<ref>http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html</ref>
 
Most Object Oriented Programming languages (like C++, Java, etc) are static; meaning the object attributes are set at compile time and cannot be changed at run-time. So if one wishes to add on to the features of an object in say, Java, they must do so at compile time. But in dynamically-typed languages like [http://www.ruby-lang.org/en/ Ruby] ,objects can be added onto at run-time. The adding on-to objects can be described as extending the properties of an object to reflect properties of other classes and/or objects.
 
= Languages Using 'extend' Functionality =
 
==Java==
 
 
Object Oriented Programming languages provide mechanisms to implement the [http://en.wikipedia.org/wiki/Object-oriented_modeling object-oriented model]. In the real world, the programs are organized around objects and these objects have certain behavior, properties, type, and identity <ref>http://www.tatamcgrawhill.com/html/9780070495432.html</ref>. In OOP based languages, the main aim is to identify the objects and manipulate their relation between each other. Larger applications can be developed with greater flexibility using the OOP. Another important work in OOP is to classify objects into different [http://www.roseindia.net/java/beginners/oop-in-java.shtml types] according to their properties and behavior.
 
[http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html Objects] in Java invoke methods and communicate with each other and perform various tasks such as Input-Output (IO) tasks, running animations and so on. In order to add additional functionality, we can extend these objects through inheritance, so as to enable the objects to perform more functions.
 
===Objects in Java===
 
An object is an instance of a class. Software objects are often used to model the real-world objects that you find in everyday life.  Objects are part of a class but are not the same. An object is expressed by the variable and methods within the objects <ref>http://docs.oracle.com/javase/tutorial/java/concepts/</ref>. Again these variables and methods are distinguished from each other as instant variables, instant methods and class variable and class methods. Java considers “Object”<ref>http://www.jvoegele.com/software/langcomp.html</ref> as the ancestor of all classes and provides interfaces to extend or inherit functionality.


''An object is an entity that serves as a container for data and also controls access to the data. Associated with an object is a set of attributes, which are essentially no more than variables belonging to that object. Also associated with an object is a set of functions that provide an interface to the functionality of the object, called methods. - Hal Fulton''[http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html]
====Extending Classes ====


== Languages Using 'extend' Functionality ==
In Java, an object can be created by instantiating a class. [http://www.javabeginner.com/learn-java/java-inheritance/ Inheritance] is one of the most powerful paradigms of Object oriented programming, which allows hierarchical classifications. Inheritance<ref>http://www.javabeginner.com/learn-java/java-inheritance/</ref> is defined as the ability to derive one class from another and inherit its state and behavior. Inheritance allows you to reuse code over and over again in each subclass you create. A class which is inherited is called super class, the class that does that inheriting is called subclass. The subclass can inherit all the instance variables and methods defined in the superclass and and its own functionality. Java uses 'extend' to extend a class (or an interface's) functionality dynamically (even though Java is a statically typed language, the extend functionality enables it to add features at run-time, i.e., dynamically).
===C++===


Object-oriented programming features like abstraction, encapsulation, inheritance, and polymorphism were added in C and introduced as  C++. In C++, the data and functions are bundled together as a self-contained unit called an object[http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-objects-and-classes.html]. In C programming language, the class describes only the data properties. Similar to the structures found in C, a class in C++ describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. 
A simple example for creating a new object using the 'new' keyword is mentioned below:


====Example Code Snippet====
<pre>


Greet today = new Greet();


----
</pre>


This statement creates a new Greet object (Greet is a class here). This single statement actually performs three actions: declaration, instantiation, and initialization. Greet today is a variable declaration which declares to the compiler that 'today' is the variable with datatype as Greet.


===Java===
In the case of [http://www.roseindia.net/java/language/inheritance.shtml single inheritance ], a subclass is derived simply from its parent class. To derive a class in java the keyword 'extends' is used. To clearly understand the concept of extending classes using the 'extends' keyword, go through the simple example of single inheritance:


Object Oriented Programming or OOP is the technique to create programs based on the real world. Unlike procedural programming, here in the OOP programming model programs are organized around objects and data rather than actions and logic. Objects represent some concepts or things and like any other objects in the real Objects in programming language have certain behavior, properties, type, and identity. In OOP based language the principal aim is to find out the objects to manipulate and their relation between each other. OOP offers greater flexibility and compatibility and is popular in developing larger application. Another important work in OOP is to classify objects into different types according to their properties and behavior.[http://www.roseindia.net/java/beginners/oop-in-java.shtml]
<pre>


====Objects in Java====
class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}


Objects are the basic unit of object orientation with behavior, identity. Objects are part of a class but are not the same. An object is expressed by the variable and methods within the objects. Again these variables and methods are distinguished from each other as instant variables, instant methods and class variable and class methods.  
class B extends A{
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
  void display(){
  System.out.println("B");
  }
}


====Creating Objects ====
</pre>


====Extending Interfaces ====


An [http://www.tutorialspoint.com/java/java_interfaces.htm interface] is a collection of [http://www.javatutorialhub.com/java-abstract-class-method.html abstract methods]. A class implements an interface, thereby inheriting the abstract methods of that interface. An interface can extend another interface, similar to the way a class extends another class. Here, the extends keyword is used to extend the interface, and the child interface inherits the methods of the parent interface. In this manner, the interface's functionalities are 'extended' i.e., added to the functionalities of the class.


Here is a simple example to declare an interface:


==== Extending Objects====
<pre>


public interface NameOfInterface
{
  //Any number of final, static fields
  //Any number of abstract method declarations
}


</pre>


Similar to the way that a class can extend another class, an interface can extend another interface.The 'extends' keyword is used to extend an interface and the child interface inherits the methods of the parent interface. In Java, a class can only extend one parent class. [http://en.wikipedia.org/wiki/Multiple_inheritance Multiple inheritance] is not allowed. Interfaces are not classes but they can extend more than one parent interface.


====Example Code Snippet====
Here is a simple example of how to extend an interface:
 
<pre>
 
public interface Hello
{
  public void setHi(String name);
  public void setBye(String name);
}
 
public interface Greet extends Hello
{
  public void greeting();
}
 
public class JavaExample extends Greet
{
  public void example();
}
 
</pre>
 
In the above example, the Hello interface has two methods, setHi() and setBye(), the Greet interface has one method. The JavaExample class has one method as well. Once an object for the class JavaExample is created, it can access methods setHi(), setBye() and greeting(), due to the extend functionality.
 
== JavaScript ==
 
As in Java, objects can be extended at run time to provide more functionality. Classes can be extended using the object.extend() functionality gained by using Prototypes. Although extension in JavaScript may look like extension in Java, they are quite different. The main difference between dynamic extension in Java and JavaScript is that JavaScript uses a Prototype-based extension system, rather than using a Class-based extension system<ref>http://www.bennadel.com/blog/1514-Extending-Classes-In-Object-Oriented-Javascript.htm</ref>.
 
===Prototypes===
Prototypes<ref>http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/</ref> are basically objects from which other objects can inherit properties (dynamically)<ref>http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes</ref>. The [http://www.prototypejs.org/api/object/extend prototype extend] functionality <ref>http://phrogz.net/JS/classes/ExtendingJavaScriptObjectsAndClasses.html#prototype</ref> can be represented by:
 
<pre>
 
Object.extend(Destination, Source)
 
</pre>
 
This copies all properties of the source object to the destination object.
 
==== Extending Objects ====
To extend an object dynamically in JavaScript, we can make use of the above function. For example<ref>http://andrewdupont.net/2009/08/28/deep-extending-objects-in-javascript/</ref>,
 
<pre>
 
var destination = { hello: "user" };
var source = { welcome: "to JavaScript" };
 
</pre>
 
Now, on extending these objects,
 
<pre>
 
Object.extend( destination, source);
 
</pre>
 
we can see the results:
 
<pre>
 
destination.hello; //gives "user"
destination.welcome; //gives "to JavaScript"
 
source.welcome = "destination is modified";
destination.welcome; //gives "destination is modified"
 
</pre>
 
==Ruby==


===Ruby===
Ruby is a OOP language. It differs from the previous two OOP languages mentioned above in how it interprets what 'object-oriented' means and the different terminologies for the concepts it employs.  
Ruby is a OOP language. It differs from the previous two OOP languages mentioned above in how it interprets what 'object-oriented' means and the different terminologies for the concepts it employs.  


In Ruby, everything is an object and all objects are instances of a class. Even classes are instances of class objects. So when there is a class, .new is called to get an instance of the class and when there is an instance, .class is called to obtain its class [http://skilldrick.co.uk/2011/08/understanding-the-ruby-object-model/]. So each class has a superclass and if it doesn't specify any class from which it inherits, then it inherits directly from the Object class.
In Ruby, everything is an object and all objects are instances of a class. Even classes are instances of class objects. So when there is a class, .new is called to get an instance of the class and when there is an instance, .class is called to obtain its class <ref>http://skilldrick.co.uk/2011/08/understanding-the-ruby-object-model/</ref>. So each class has a superclass and if it doesn't specify any class from which it inherits, then it inherits directly from the Object class.


The picture below depicts the linking between all classes in Ruby. Ruby contains several classes and modules that are available by default. Some of these are ''Array, Class, Exception, Fixnum, Integer, Numeric, Object, Float, Range, Time, BasicObject, Module'' and some built-in modules are ''Kernel, Math, Comparable, Enumerable'' and so on. All classes are derived from the ''BasicObject'' class in Ruby 1.9+. By inheritance rules, all of its methods are available to all the objects.  
The picture below depicts the linking between all classes in Ruby. Ruby contains several classes and [http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html modules] that are available by default. Some of these are ''Array, Class, Exception, Fixnum, Integer, Numeric, Object, Float, Range, Time, [http://www.ruby-doc.org/core-1.9.3/BasicObject.html BasicObject], Module'' and some built-in modules are ''Kernel, Math, Comparable, Enumerable'' and so on. All classes are derived from the ''BasicObject'' class in Ruby 1.9+. By inheritance rules, all of its methods are available to all the objects.  


[[File:rubyclass.jpg|thumb|center|6500px|A class hierarchy in Ruby [http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html]]]
[[File:rubyclass.jpg|thumb|center|10000px|A class hierarchy in Ruby <ref>http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html</ref>]]


Another way for an object to gain additional methods is by making use of the 'extend' keyword. By using 'extend' with an object, the object gains the methods from a module it is extending.  
Another way for an object to gain additional methods is by making use of the 'extend' keyword. By using 'extend' with an object, the object gains the methods from a module it is extending.  


====Example Code Snippet====
===Example Code Snippet===


Here is an example that shows how Object.extend(module) adds the methods defined in a module at the object's level.
Here is an example that shows how Object.extend(module) adds the methods defined in a module at the object's level.
<pre>
<pre>
  module RubyExample
  module RubyExample
   def helloWorld
   def helloWorld
Line 60: Line 177:
   end
   end
  end
  end
</pre>
</pre>


Line 65: Line 183:


<pre>
<pre>
  obj = Object.new
  obj = Object.new
  obj.extend(RubyExample)
  obj.extend(RubyExample)
  obj.helloWorld #=> "Hello user! Welcome to Ruby!"
  obj.helloWorld #=> "Hello user! Welcome to Ruby!"
</pre>
</pre>


Line 73: Line 193:


<pre>
<pre>
  >> RubyExample.object_id
  >> RubyExample.object_id
  => 45097
  => 45097
Line 79: Line 200:
  >> RubyExample.respond_to?(:extend)
  >> RubyExample.respond_to?(:extend)
  => true
  => true
</pre>
</pre>


Now this is not the only way to extend object functionality in Ruby. We can also do the same thing by  
Now this is not the only way to extend object functionality in Ruby. We can also do the same thing by  
<pre>
<pre>
  RubyExample = Module.new do
  RubyExample = Module.new do
   def helloWorld
   def helloWorld
Line 92: Line 215:


  RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"
  RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"
</pre>
</pre>


Line 97: Line 221:


<pre>
<pre>
  module RubyExample
  module RubyExample
     extend RubyExample
     extend RubyExample
Line 105: Line 230:


  RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"
  RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"
</pre>
</pre>


Line 110: Line 236:


<pre>
<pre>
  module RubyExample
  module RubyExample
     extend self
     extend self
Line 118: Line 245:


  RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"
  RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"
</pre>
</pre>


Another way to mix Ruby modules into a class is by using the 'include' keyword. For more information, see [http://www.dan-manges.com/blog/27 Extend through Include].
Another way to mix Ruby modules into a class is by using the 'include' keyword. For more information, see [http://www.dan-manges.com/blog/27 Extend through Include].


= Advantages & Disadvantages =
== Advantages ==
== Advantages ==
In Java, both normal and abstract classes and interfaces can be extended at run-time to provide more functionality. Extending an abstract class allows programmers to create a template of sorts as an abstract class and the classes that extend this class can define these functionalities, which leads to modularity an efficiency.
[http://www.w3schools.com/js/default.asp JavaScript] allows methods that are extended via prototypes to be changed simultaneously in all instances. Also, since prototyping enables lesser methods, it decreases page loading time which results in efficiency<ref>http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor</ref>.
Objects in Ruby are extended for all of their instances and therefore the code stays simple. Also, it is extremely easy to extend any object in ruby, making it one of Ruby's most admired qualities.
== Disadvantages ==
== Disadvantages ==
== Conclusion ==
 
== See also ==
The use of 'extend' in Java is argued to be unnecessarily complicated<ref>http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html</ref>.
== References ==
 
<references/>
In JavaScript, prototypes modify the destination object and leave the source object untouched in almost all scenarios. Supposing the functionality of object A is extended to that of object B, and object B and object A both have a method called 'greet' then object B's method 'greet' will be replaced by that of object A's, leading to a loss of data and method overriding. As this can happen unintentionally, it is a common source of errors.
== Further Reading ==
 
== External Links ==
With Ruby, modules are generally used with classes and hence, the 'include' functionality is made use of more, instead of 'extend'.
 
= Conclusion =
In a nutshell, several languages use the extend feature to extend their functionality, but only a few use it dynamically. The dynamic use promotes increased efficiency as well as robustness. Languages like Java, JavaScript and Ruby are all object-oriented and they can use the extend feature to their advantage. The size of code reduces because there is no need to re-use (or re-implement) methods and thus writing applications becomes simpler as well.
 
= See also =
 
[http://docs.oracle.com/javase/tutorial/java/concepts/interface.html Java Interfaces]
 
[http://www.tutorialspoint.com/java/java_interfaces.htm Interfaces in Java]
 
[http://mindprod.com/jgloss/interfacevsabstract.html Interface vs Abstract class]
 
[http://www.javascriptkit.com/javatutors/proto.shtml Prototype Object in JavaScript]
 
[http://www.javascriptkit.com/javatutors/oopjs2.shtml OOPS JavaScript]
 
[http://www.southsearepublic.org/article/1174/read/object_oriented_javascript__prototyping JavaScript Prototyping]
 
[http://yehudakatz.com/2009/11/12/better-ruby-idioms/ Extending ruby]
 
[http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/ Include vs Extend in Ruby]
 
[http://zef.me/1059/shining-ruby-extending-classes Extending classes in ruby]
 
= References =
<references />

Latest revision as of 17:42, 19 September 2012

Extending Objects


The ‘extend’ feature in computer programming languages allows the programmer to dynamically extend the functionality of an object at run-time, as opposed to extending functionality at compile time. This feature can be found in most Object Oriented Programming (OOP) Languages, some of which are Java, JavaScript, and Ruby. Some OOP languages do not support the 'extend' feature, such as C++.

Introduction

The Object Oriented Programming uses 'objects' for designing applications. These objects consist of an encapsulated set of data fields and methods, and are usually an instance of a class. These objects can communicate with each other through messages (or 'methods') and process data in object oriented programming. But in order to increase an object's functionality, it is necessary to add the features of one object to another. This can be done dynamically at run time, without repeating the code, by extending the features of one class to another. This can be done by the use of the 'extend' keyword. Many Object Oriented Programming support the 'extend' feature.

An object is an entity that serves as a container for data and also controls access to the data. Associated with an object is a set of attributes, which are essentially no more than variables belonging to that object. Also associated with an object is a set of functions that provide an interface to the functionality of the object, called methods. - Hal Fulton<ref>http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html</ref>

Most Object Oriented Programming languages (like C++, Java, etc) are static; meaning the object attributes are set at compile time and cannot be changed at run-time. So if one wishes to add on to the features of an object in say, Java, they must do so at compile time. But in dynamically-typed languages like Ruby ,objects can be added onto at run-time. The adding on-to objects can be described as extending the properties of an object to reflect properties of other classes and/or objects.

Languages Using 'extend' Functionality

Java

Object Oriented Programming languages provide mechanisms to implement the object-oriented model. In the real world, the programs are organized around objects and these objects have certain behavior, properties, type, and identity <ref>http://www.tatamcgrawhill.com/html/9780070495432.html</ref>. In OOP based languages, the main aim is to identify the objects and manipulate their relation between each other. Larger applications can be developed with greater flexibility using the OOP. Another important work in OOP is to classify objects into different types according to their properties and behavior.

Objects in Java invoke methods and communicate with each other and perform various tasks such as Input-Output (IO) tasks, running animations and so on. In order to add additional functionality, we can extend these objects through inheritance, so as to enable the objects to perform more functions.

Objects in Java

An object is an instance of a class. Software objects are often used to model the real-world objects that you find in everyday life. Objects are part of a class but are not the same. An object is expressed by the variable and methods within the objects <ref>http://docs.oracle.com/javase/tutorial/java/concepts/</ref>. Again these variables and methods are distinguished from each other as instant variables, instant methods and class variable and class methods. Java considers “Object”<ref>http://www.jvoegele.com/software/langcomp.html</ref> as the ancestor of all classes and provides interfaces to extend or inherit functionality.

Extending Classes

In Java, an object can be created by instantiating a class. Inheritance is one of the most powerful paradigms of Object oriented programming, which allows hierarchical classifications. Inheritance<ref>http://www.javabeginner.com/learn-java/java-inheritance/</ref> is defined as the ability to derive one class from another and inherit its state and behavior. Inheritance allows you to reuse code over and over again in each subclass you create. A class which is inherited is called super class, the class that does that inheriting is called subclass. The subclass can inherit all the instance variables and methods defined in the superclass and and its own functionality. Java uses 'extend' to extend a class (or an interface's) functionality dynamically (even though Java is a statically typed language, the extend functionality enables it to add features at run-time, i.e., dynamically).

A simple example for creating a new object using the 'new' keyword is mentioned below:


Greet today = new Greet();

This statement creates a new Greet object (Greet is a class here). This single statement actually performs three actions: declaration, instantiation, and initialization. Greet today is a variable declaration which declares to the compiler that 'today' is the variable with datatype as Greet.

In the case of single inheritance , a subclass is derived simply from its parent class. To derive a class in java the keyword 'extends' is used. To clearly understand the concept of extending classes using the 'extends' keyword, go through the simple example of single inheritance:


class A {
  int x;
  int y;
  int get(int p, int q){
  x=p; y=q; return(0);
  }
  void Show(){
  System.out.println(x);
  }
}

class B extends A{
  public static void main(String args[]){
  A a = new A();
  a.get(5,6);
  a.Show();
  }
  void display(){
  System.out.println("B");
  }
}

Extending Interfaces

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of that interface. An interface can extend another interface, similar to the way a class extends another class. Here, the extends keyword is used to extend the interface, and the child interface inherits the methods of the parent interface. In this manner, the interface's functionalities are 'extended' i.e., added to the functionalities of the class.

Here is a simple example to declare an interface:


public interface NameOfInterface
{
   //Any number of final, static fields
   //Any number of abstract method declarations
}

Similar to the way that a class can extend another class, an interface can extend another interface.The 'extends' keyword is used to extend an interface and the child interface inherits the methods of the parent interface. In Java, a class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes but they can extend more than one parent interface.

Here is a simple example of how to extend an interface:


public interface Hello
{
   public void setHi(String name);
   public void setBye(String name);
}

public interface Greet extends Hello
{
   public void greeting();
}

public class JavaExample extends Greet
{
   public void example();
}

In the above example, the Hello interface has two methods, setHi() and setBye(), the Greet interface has one method. The JavaExample class has one method as well. Once an object for the class JavaExample is created, it can access methods setHi(), setBye() and greeting(), due to the extend functionality.

JavaScript

As in Java, objects can be extended at run time to provide more functionality. Classes can be extended using the object.extend() functionality gained by using Prototypes. Although extension in JavaScript may look like extension in Java, they are quite different. The main difference between dynamic extension in Java and JavaScript is that JavaScript uses a Prototype-based extension system, rather than using a Class-based extension system<ref>http://www.bennadel.com/blog/1514-Extending-Classes-In-Object-Oriented-Javascript.htm</ref>.

Prototypes

Prototypes<ref>http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/</ref> are basically objects from which other objects can inherit properties (dynamically)<ref>http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes</ref>. The prototype extend functionality <ref>http://phrogz.net/JS/classes/ExtendingJavaScriptObjectsAndClasses.html#prototype</ref> can be represented by:


Object.extend(Destination, Source)

This copies all properties of the source object to the destination object.

Extending Objects

To extend an object dynamically in JavaScript, we can make use of the above function. For example<ref>http://andrewdupont.net/2009/08/28/deep-extending-objects-in-javascript/</ref>,


var destination = { hello: "user" };
var source = { welcome: "to JavaScript" };

Now, on extending these objects,


Object.extend( destination, source);

we can see the results:


destination.hello; //gives "user"
destination.welcome; //gives "to JavaScript"

source.welcome = "destination is modified";
destination.welcome; //gives "destination is modified"

Ruby

Ruby is a OOP language. It differs from the previous two OOP languages mentioned above in how it interprets what 'object-oriented' means and the different terminologies for the concepts it employs.

In Ruby, everything is an object and all objects are instances of a class. Even classes are instances of class objects. So when there is a class, .new is called to get an instance of the class and when there is an instance, .class is called to obtain its class <ref>http://skilldrick.co.uk/2011/08/understanding-the-ruby-object-model/</ref>. So each class has a superclass and if it doesn't specify any class from which it inherits, then it inherits directly from the Object class.

The picture below depicts the linking between all classes in Ruby. Ruby contains several classes and modules that are available by default. Some of these are Array, Class, Exception, Fixnum, Integer, Numeric, Object, Float, Range, Time, BasicObject, Module and some built-in modules are Kernel, Math, Comparable, Enumerable and so on. All classes are derived from the BasicObject class in Ruby 1.9+. By inheritance rules, all of its methods are available to all the objects.

A class hierarchy in Ruby <ref>http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html</ref>

Another way for an object to gain additional methods is by making use of the 'extend' keyword. By using 'extend' with an object, the object gains the methods from a module it is extending.

Example Code Snippet

Here is an example that shows how Object.extend(module) adds the methods defined in a module at the object's level.


 module RubyExample
   def helloWorld
       "Hello user! Welcome to Ruby!"
   end
 end

Now, we can extend this module's functionality (i.e, the Greet method) to any object we instantiate. Below we can see that the module's methods have been mixed in to the object.


 obj = Object.new
 obj.extend(RubyExample)
 obj.helloWorld #=> "Hello user! Welcome to Ruby!"

As mentioned earlier, everything in Ruby is an object. The following confirms this fact:


 >> RubyExample.object_id
 => 45097
 >> RubyExample.class
 => Module
 >> RubyExample.respond_to?(:extend)
 => true

Now this is not the only way to extend object functionality in Ruby. We can also do the same thing by


 RubyExample = Module.new do
   def helloWorld
       "Hello user! Welcome to Ruby!"
   end
 end

 RubyExample.extend(RubyExample)

 RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"

So, the above alternative method clarifies the fact that RubyWorld is just an instance of the class Module. We can also do the same thing by including the extend() call within the module definition.


 module RubyExample
    extend RubyExample
    def helloWorld
        "Hello user! Welcome to Ruby!"
    end
 end

 RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"

In Ruby, the keyword 'self' refers to the current or default object itself. So another way of implementing the above is as follows


 module RubyExample
    extend self
    def helloWorld
        "Hello user! Welcome to Ruby!"
    end
 end

 RubyExample.helloWorld #=> "Hello user! Welcome to Ruby!"

Another way to mix Ruby modules into a class is by using the 'include' keyword. For more information, see Extend through Include.

Advantages & Disadvantages

Advantages

In Java, both normal and abstract classes and interfaces can be extended at run-time to provide more functionality. Extending an abstract class allows programmers to create a template of sorts as an abstract class and the classes that extend this class can define these functionalities, which leads to modularity an efficiency.

JavaScript allows methods that are extended via prototypes to be changed simultaneously in all instances. Also, since prototyping enables lesser methods, it decreases page loading time which results in efficiency<ref>http://stackoverflow.com/questions/4508313/advantages-of-using-prototype-vs-defining-methods-straight-in-the-constructor</ref>.

Objects in Ruby are extended for all of their instances and therefore the code stays simple. Also, it is extremely easy to extend any object in ruby, making it one of Ruby's most admired qualities.

Disadvantages

The use of 'extend' in Java is argued to be unnecessarily complicated<ref>http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html</ref>.

In JavaScript, prototypes modify the destination object and leave the source object untouched in almost all scenarios. Supposing the functionality of object A is extended to that of object B, and object B and object A both have a method called 'greet' then object B's method 'greet' will be replaced by that of object A's, leading to a loss of data and method overriding. As this can happen unintentionally, it is a common source of errors.

With Ruby, modules are generally used with classes and hence, the 'include' functionality is made use of more, instead of 'extend'.

Conclusion

In a nutshell, several languages use the extend feature to extend their functionality, but only a few use it dynamically. The dynamic use promotes increased efficiency as well as robustness. Languages like Java, JavaScript and Ruby are all object-oriented and they can use the extend feature to their advantage. The size of code reduces because there is no need to re-use (or re-implement) methods and thus writing applications becomes simpler as well.

See also

Java Interfaces

Interfaces in Java

Interface vs Abstract class

Prototype Object in JavaScript

OOPS JavaScript

JavaScript Prototyping

Extending ruby

Include vs Extend in Ruby

Extending classes in ruby

References

<references />