CSC/ECE 517 Fall 2011/ch4 4a aj: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 20: Line 20:


'''''In java:'''''
'''''In java:'''''
  <pre>
   
  A class declaration can use the extend keyword on another class as follows:
  A class declaration can use the extend keyword on another class as follows:
<Code>
  Class Course extends College  
  Class Course extends College  
  {
  {
Line 27: Line 28:
  }
  }


 
</code>
  When a class Child extends class Parent, Child automatically has all variables and methods defined in class Parent.
  When a class Child extends class Parent, Child automatically has all variables and methods defined in class Parent.
  If class Child defines a variable or method that has the same name in class Parent, class Child's definition  
  If class Child defines a variable or method that has the same name in class Parent, class Child's definition  

Revision as of 00:39, 20 October 2011

Extending objects. : Consider one or more practical examples of Ruby's extend method--that is,
examples that serve a real need and are not contrived.  What facilities do other languages have for extending
objects?  Are they more or less powerful than Ruby's?  Also consider prototype-based languages such as Self, 
which don't even provide classes.

Introduction

    One of the most fundamental advantages of using Object Orientation is to reuse code. Rather than coding what was already 
coded, we can simply extend it to suit the needs of the new class. This helps to develop applications rapidly. 
Many languages provide built in libraries which can be included in classes. The O-O languages also provide a way to add
functionality to existing classes through extension.

Implementation of Code Reuse:

“Inheritance” is the word used in O-O for the process of inheriting the members of a class and extending the functionality. 
A class can inherit from a single class or “Multiple Classes”, the latter is called as “Multiple Inheritance”.
While a child class inherits from the parent class,  it tends to inherit all the members of the class.

Comparison of Extend methods in Java, C# and Ruby.

In java:

A class declaration can use the extend keyword on another class as follows:

Class Course extends College 
{
..............
}

When a class Child extends class Parent, Child automatically has all variables and methods defined in class Parent.
If class Child defines a variable or method that has the same name in class Parent, class Child's definition 
overrides that of Parent's.


class Parent {
int x;
int y;
int get(int a, int b){
x=a; y=b; return(0);
}
void Show(){
System.out.println(x);
}
}
class Child extends Parent {
public static void main(String args[]){
Parent p = new Parent();
p.get(5,6);
p.Show();
}
void display(){
System.out.println("Child");
}
}


In C#:


 Extension methods enable the programmer to "add" methods to existing types without creating a new derived type, recompiling, 
 or otherwise modifying    the original type. Extension methods are a special kind of static method, but they are called 
 as if they were instance methods on the extended type. 

 class ExtensionMethod    
 {
    static void Main()
    {            
        int[] s = { 1, 55, 75, 93, 81, 96 };
        var r = s.OrderBy(g => g);
        foreach (var a in result)
        {
            System.Console.Write(a + " ");
        }           
    }        
 }

  Output: 
  1       55          15       81            93          96
  The above example shows how to call the standard query operator OrderBy method on an array of integers.

In C++


 Similar to Java, C++ also extends functionality of a base class to a derived class. The derived class can use the functions 
 and variables of a base  class as well as add their own functionality, thus making them a more specialized version of the base 
 class.  Similar to Java, overriding is also supported in C++. Here is an example of how a class is extended in C++.

 class Base{
 public:
 int x;
 int y;
 int get(int a, int b){
 x=a; 
 y=b; 
 return(0);
 }

 void display()
 cout<<x<<y;
 }
 }

 class derived:public base{
 void Main(){
 Base b;
 b.get(5,6);
 b.display();
 void display(){
 cout<<”Derived”;
 }
 }

In Python:

 To extend a class in Python, we simply call the parent class in the parenthesis while defining the child class. 
 This will pass all the methods and variables defined in the parent class to the child class.
 An illustration of how classes are extended in python.

 class Parent:
 a=5
 b=4
 def add():
 print(a+b)
 class Child(Parent):
 def difference():
 print(a-b)
 p = Parent()
 c = Child()
 c.add()
 c.difference()

In Ruby:

 The include method effectively adds a module as a super class of self. It is used inside a class definition 
 to make the instance methods in the module available to instances of the class.
 It is sometimes useful to add the instance methods to a particular object.
 This is done using Object#extend.

 
 module Write
 def pen
 "#{self} says use a pen not a pencil for your exam!"
 end
 end
 obj = " Proctor"
 obj.extend Write
 puts obj.pen

 Output: Proctor says use a pen not pencil for your exam

 If the extend key word is used within a class definition, the modules methods become class methods. 
 This is because extend is equal to self.extend, so  the methods are added to self, which in a class definition is the class itself.

 module Write
 def pen
 "#{self} says use a pen not pencil for your exam"
 end
 end
 class Proctor
 extend Write 
 end
 puts Proctor.pen

 Output: Proctor says use a pen not pencil for your exam

How Extending Objects in Ruby is more powerful than other languages

   The one main disadvantage of inheriting from a class is that, we tend to inherit all of its members, most of which
we may not use in our programs.  Ruby however gives us the option of extending only what we need. Several modules can 
be created and these modules can be extended at runtime. The extend keyword makes the module available to all instances
of the class.  A class can simply extend the modules and uses it the way it wants on the move. This, however, does not 
change the objects original code. This is really a unique and powerful feature of Ruby.

Comparison of "Extend property " in Prototype languages to Class Based Languages.

Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior
 reuse is performed via a process  of cloning (Cloning is the process of  making of an exact copy of an object) existing objects 
 that serve as prototypes. Languages like Self, Newton Script, Slate, Ioke, Io are examples of a prototype language.
 Most class-based Object Oriented languages are based on Classes which can be described as defining the basic qualities and 
 behaviors of objects and object instances which can be described as being manifestations of classes. 
 In prototype-based languages like Self, the duality between classes and object instances is not needed. There is no need for 
 having an instance of an object that is based on a class. For example, in Self  a copy of an existing object is made, and 
 then changes are made to it. Basic objects that are used primarily to make copies are known as prototypes. When an existing 
 object  proves to be an insufficient, a programmer can create a modified object with the expected behavior, and use that instead of 
 the previous one.  

 We saw how the extend key word is used in Java, C# and in Ruby. In languages like Self, instead of having a class definition take up
 memory, and every instance take up memory, clones of objects generally take up a small amount memory. Every object just points to another object.
 So the fewer changes the programmer makes to a clone of an object, the fewer the amounts of new data need to be stored, and thus lesser amount 
 of memory that a programmer needs to do a new clone. Prototype-based languages will help simplify the understanding of each object. 
 It is also a fact that languages like Self,  make it easier to fix bugs in code. This is because prototype-based languages use the concept of 
 slots.   We are referring to a method as slot because the programmer can change the functionality of any object, by changing a method alone.     
 When new code is placed into an object's slot the functionality of that object is changed. 
 Another advantage is that the user interfaces also benefit from the use of prototype-based languages. User interfaces 
 hold less amount of objects. Objects are created in interfaces for a specific purpose. Without creating many classes for many 
 purposes, or creating just a few big ones, each of them with a number of methods for implementation, cloning objects is a proved 
 easier solution.
 Prototype-based programming has its own disadvantages. Consider the cases when repetitive types of data is used like the ones in a database. 
 Here   prototype-based programming would decrease efficiency of these applications. And also in this case, Class-based programming will be best
 and perhaps the only solution for these kinds of problems.
 We saw that in Java, inheritance is achieved using extends keyword. In Java Script, a prototype based language, Prototype chaining is used
 to build new types of objects based on existing ones.  The functionality is very similar to inheritance in a class based language. 
 Constructor  functions have a property known as prototype. Adding properties and methods to the prototype property will also adds the
 method or property to all objects created by the constructor function. This prototype property is a mere javascript object such that it is
 possible to create a function’s prototype using another constructor function. When this is done, all of the properties and methods from 
 the   constructor function’s prototype are automatically added to new the prototype object. This makes it easy to create a constructor function 
 that builds objects that are just an extended version of an existing one.

References

[1]http://xahlee.org/java-a-day/extend.html

[2]http://msdn.microsoft.com/en-us/library/bb383977.aspx

[3]http://www.tenouk.com/Module14.html

[4]http://perfectionlabstips.wordpress.com/2008/11/19/how-to-extend-ruby-object-on-the-fly

[5]http://helephant.com/2009/08/17/javascript-prototype-chaining.