CSC/ECE 517 Fall 2007/wiki2 6 ap: Difference between revisions

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


Although the specification of a class is pretty similar to that of a type, it is more of a runtime notion. There are two major aspects of a class – an object factory and an object warehouse. The object factory is the means by which objects of a class are created, using the new operation on the class. Object warehouse stores the set of objects that are instances of the class. Users can work with all these objects by performing all the allowed operations on them and thereby change the contents of the object warehouse. Thus, classes by no means determine the correctness of a program, rather they are used to create and manipulate objects, thereby making the system more flexible.
Although the specification of a class is pretty similar to that of a type, it is more of a runtime notion. There are two major aspects of a class – an object factory and an object warehouse. The object factory is the means by which objects of a class are created, using the new operation on the class. Object warehouse stores the set of objects that are instances of the class. Users can work with all these objects by performing all the allowed operations on them and thereby change the contents of the object warehouse. Thus, classes by no means determine the correctness of a program, rather they are used to create and manipulate objects, thereby making the system more flexible.




==Types vs Classes in Java==
==Types vs Classes in Java==


Java is a strongly typed language i.e. the type of every variable and expression used in a program must be known at compile time. The type of a variable / expression defines the range of values that it can hold. The type of a variable also determines the operations that can be performed on that variable. The strong typing nature of Java enables it to determine errors at compile time.
Java is a strongly typed language i.e. the type of every variable and expression used in a program must be known at compile time. The type of a variable / expression defines the range of values that it can hold. The type of a variable also determines the operations that can be performed on that variable. The strong typing nature of Java enables it to determine errors at compile time.
Line 62: Line 60:


Whenever a variable is declared in Java, we need to mention its type, which may be a class, interface or a primitive. When an object of a subclass of some other class is instantiated, we can say that the object is of type subclass and type superclass and it can perform all operations of both the subclass and the superclass.<br>
Whenever a variable is declared in Java, we need to mention its type, which may be a class, interface or a primitive. When an object of a subclass of some other class is instantiated, we can say that the object is of type subclass and type superclass and it can perform all operations of both the subclass and the superclass.<br>




==Types and Classes in Ruby==
==Types and Classes in Ruby==


In Ruby, all classes are instances of the class Class. Whenever a new class is defined, an object of type Class is created and assigned to the ''Name'' of the class. ''Name.new'' is used to create a new object of the class. This invokes the ''new'' instance method in Class, which in turn calls the ''allocate'' method to allocate memory for the object, before finally calling the new object's initialize method. Thus, the construction of an object is separate from its initialization, and both can be over-ridden. The initialization is done via the ''initialize'' instance method while the construction is done via the ''new'' class method. <br>
In Ruby, all classes are instances of the class Class. Whenever a new class is defined, an object of type Class is created and assigned to the ''Name'' of the class. ''Name.new'' is used to create a new object of the class. This invokes the ''new'' instance method in Class, which in turn calls the ''allocate'' method to allocate memory for the object, before finally calling the new object's initialize method. Thus, the construction of an object is separate from its initialization, and both can be over-ridden. The initialization is done via the ''initialize'' instance method while the construction is done via the ''new'' class method. <br>
Line 87: Line 82:


Derived types are new types that can be used in a program, and can include directly derived types and composed derivative types.
Derived types are new types that can be used in a program, and can include directly derived types and composed derivative types.
====Directly Derived Types====
====Directly Derived Types====


Line 98: Line 92:
     * Constants
     * Constants
     * Pointers to Class Members
     * Pointers to Class Members
====Composed Derivative Types====
====Composed Derivative Types====


Line 113: Line 106:
Class types are defined using the class, struct, and union keywords. For simplicity, types defined with these keywords are called class declarations. Class Type names are introduced as identifiers immediately after the compiler processes them (before entry into the class body); they can be used to declare class members. This allows declaration of self-referential data structures
Class types are defined using the class, struct, and union keywords. For simplicity, types defined with these keywords are called class declarations. Class Type names are introduced as identifiers immediately after the compiler processes them (before entry into the class body); they can be used to declare class members. This allows declaration of self-referential data structures


 
===Example :===
===Example===


<code><pre>
<code><pre>
Line 162: Line 154:
</pre></code>
</pre></code>


'''Output'''
'''Output:'''


<code><pre>
<code><pre>

Revision as of 16:02, 23 October 2007

Types vs Classes

In object oriented programming concepts, the difference between types and classes is not clearly understood. The differences between them are subtle and the two terms can be used interchangeably with respect to object oriented programming concepts. In the article we try to bring out the differences between Types and Classes and provide more insight into them.

Definitions

Type

A type, in an object-oriented system, summarizes the common features of a set of objects with the same characteristics. It corresponds to the notion of an abstract data type. Type has 2 parts: the interface and the implementation(s). The interface, which is the only part visible to the user, consists of a list of operations and their signatures. The type implementation is of concern only to the type designer.

Class

A class, in object oriented systems, is a kind of metadata that describes the rules by which objects behave; these objects are referred to as instances of that class. A class specifies the structure of data (a.k.a. properties, fields, attributes, data members) within each instance as well as methods (functions) (a.k.a. behaviors) which manipulate the data of the object and perform tasks.

Types

Types are abstract views that define properties of a set of entities. Object-oriented programming languages must allow to implement these types. Consequently, once a type is implemented we have a particular representation of it available. We will use the seminal concept of the integer data type to explain the concept of type in detail. Programming languages such as C, Java, C++, Pascal and others already offer an implementation for it. Sometimes it is called int or integer. Once you've created a variable of this type you can use its provided operations. For example, you can add two integers:

  int i, j, k;    /* Define three integers */

  i = 1;          /* Assign 1 to integer i */
  j = 2;          /* Assign 2 to integer j */
  k = i + j;      /* Assign the sum of i and j to k */

The first line defines three instances i, j and k of type Integer. Consequently, for each instance the special operation constructor should be called. In our example, this is internally done by the compiler. The compiler reserves memory to hold the value of an integer and binds the corresponding name to it. If you refer to i you actually refer to this memory area which was constructed by the definition of i.

The implementation of types comprises of the data (internal structure of the object’s data) and operation (methods / procedures which implement the interface operations). By making users declare the types of variables at compile time, the system simplifies the procedure of checking the program correctness at compile time, thereby increasing programmer productivity. The types cannot be modified at run-time.

Properties of a Type

  • It exports a set of operations. This set is called interface.
  • Operations of the interface are the one and only access mechanism to the type's data structure.
  • Axioms and preconditions define the application domain of the type.


Classes

A class is a combination of state (data) and behavior (methods) that manipulate the state. A class is a blueprint for an object.It is an actual representation of a type. It therefore provides implementation details for the data structure used and operations specified in the type. Now we will define a class that implements the Integer data type:

  
class Integer {
  attributes: || implementation : 
    int i /* Integer data type */

  methods: || operation :
    setValue(int n) /* To assign value to i */
    Integer addValue(Integer j) /* To add integer j with i */
  }

In this notation class {...} denotes the definition of a class. Enclosed in the curly brackets are two sections attributes: and methods: which define the implementation of the data structure and operations of the corresponding class. Again we distinguish the two levels with different terms: At the implementation level we speak of attributes which are elements of the data structure at the class level. The same applies to methods which are the implementation of the class operations. In our example, the attribute is an ordinary integer of a programming language . We only define two methods setValue() and addValue() representing the two of the many operations that can be performed on an integer data type set and add, to make the comparison succinct and concise.

Although the specification of a class is pretty similar to that of a type, it is more of a runtime notion. There are two major aspects of a class – an object factory and an object warehouse. The object factory is the means by which objects of a class are created, using the new operation on the class. Object warehouse stores the set of objects that are instances of the class. Users can work with all these objects by performing all the allowed operations on them and thereby change the contents of the object warehouse. Thus, classes by no means determine the correctness of a program, rather they are used to create and manipulate objects, thereby making the system more flexible.


Types vs Classes in Java

Java is a strongly typed language i.e. the type of every variable and expression used in a program must be known at compile time. The type of a variable / expression defines the range of values that it can hold. The type of a variable also determines the operations that can be performed on that variable. The strong typing nature of Java enables it to determine errors at compile time. The 2 types in Java are – primitive types and reference types. The primitive types are boolean and numeric. The reference types are class types, interface types and array types.

Whenever a variable is declared in Java, we need to mention its type, which may be a class, interface or a primitive. When an object of a subclass of some other class is instantiated, we can say that the object is of type subclass and type superclass and it can perform all operations of both the subclass and the superclass.


Types and Classes in Ruby

In Ruby, all classes are instances of the class Class. Whenever a new class is defined, an object of type Class is created and assigned to the Name of the class. Name.new is used to create a new object of the class. This invokes the new instance method in Class, which in turn calls the allocate method to allocate memory for the object, before finally calling the new object's initialize method. Thus, the construction of an object is separate from its initialization, and both can be over-ridden. The initialization is done via the initialize instance method while the construction is done via the new class method.


Types and Classes in C++

C++ supports three kinds of object types:

   * Fundamental types are built into the language (such as int, float, or double). Instances of these fundamental types are 
     often called variables.
   * Derived types are new types derived from built-in types.
   * Class types are new types created by combining existing types. These include Classes, Structures, and Unions.

Fundamental Types

Fundamental types in C++ are divided into three categories: integral, floating, and void. Integral types are capable of handling whole numbers. Floating types are capable of specifying values that may have fractional parts.The void type describes an empty set of values. No variable of type void can be specified — it is used primarily to declare functions that return no values or to declare generic pointers to untyped or arbitrarily typed data

Derived Types

Derived types are new types that can be used in a program, and can include directly derived types and composed derivative types.

Directly Derived Types

New types derived directly from existing types are types that point to, refer to, or (in the case of functions) transform type data to return a new type.

   * Arrays of Variables or Objects
   * Functions
   * Pointers of a Given Type
   * References to Objects
   * Constants
   * Pointers to Class Members

Composed Derivative Types

The following are the various composed derivative types:

   * Classes
   * Structures
   * Unions

Class types are derivatives of composed types. More on class types below.

Class Types

Class types are defined using the class, struct, and union keywords. For simplicity, types defined with these keywords are called class declarations. Class Type names are introduced as identifiers immediately after the compiler processes them (before entry into the class body); they can be used to declare class members. This allows declaration of self-referential data structures

Example :


#include <iostream.h>
#include <string.h>
#define TRUE = 1

class dog 
{
   //implementation
   public:
   dog()
   {
      legs = 4;
      bark = true;
   }

   void setDogSize(string dogSize)
   {
      _dogSize = dogSize;
   }
   string getDogSize()
   {
      return _dogSize;
   }
   void setBreed(string breed)
   {
      _breed = breed;
   }

   private: // attributes || fundamental types
   string _dogSize, _breed;
   int _legs;
   bool _bark;

};

int main()
{
   dog mongrel;//class types
   //setting specific attributes that makes it different from types
   mongrel.setDogSize("medium");
   mongrel.setBreed("country specific");
   cout << "Ricky is a " << mongrel.getDogSize() << " sized dog" << endl;
}

Output:

Ricky is a medium sized dog

References:

http://www.cs.cmu.edu/People/clamen/OODBMS/Manifesto/htManifesto/node6.html
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html
http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html
http://www.python.org/download/releases/2.2/descrintro/