CSC/ECE 517 Fall 2007/wiki2 6 ubs

From Expertiza_Wiki
Jump to navigation Jump to search

Type vs. class. Survey the differences on type vs. class in object-oriented languages. Often, the distinction is that class pertains to an object, whereas type pertains to a variable. Consider the distinction between the two terms in several different programming languages. Cover the differences between type and class, from type-theoretic definitions to practical aspects.


A class is a cohesive package that consists of a particular kind of metadata. It 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. A class is the most specific type of an object in relation to a specific layer. A class may also have a representation (metaobject) at runtime, which provides runtime support for manipulating the class-related metadata. The term class is traditionally used to imply an object created by the class statement. Once a class is defined, it can be treated as a new kind of data type.

A type is a set of data values, and a set of operations (methods) on those values, applied to an object. Yet another definition of type is – type is the definition of the domain of allowable values that an object may possess and the set of operations that may be performed upon the object. The terms class and type are usually (but not always) interchangeable; a type is a slightly different concept than a class, in that it emphasizes the importance of conformance to a common protocol. Type defines the proper use of a name or an expression. Type can be a built-in-type or a user-defined type. In programming language like C++, type provided directly by the language such as int, double, and char* is called built-in-type. Class or Enumeration is used to define user-defined type. In Java language, types can be defined using classes and interfaces. So by creating a new class, a new type is created. But that is not true with all programming languages. For instance, in Python all classes are of type ClassType, and all class interfaces are of type InstanceType.


Example of Class in C++

#include <iostream>
#include <string>

class Hello
{
   std::string what;

   public:
       Hello(const char* s)
           : what(s)
       {
       }

       void say()
       {
           std::cout << "Hello " << what << "!" << std::endl;
       }
};

int main( int argc, char** argv )
{
   Hello hello_world("world");
   hello_world.say();

   return 0;
}

This example shows how to define a C++ class named "Hello". It has a private string attribute named "what", and a public method named "say". In this example, user-defined type is created by declaring class “Hello”. Instance of class Hello “Hello_world” belongs to user-defined type. Also, in line :std::string what;”, variable what belongs to a built-in type ‘string’.


Example of Class in Java

public class Example1
{
   // This is a Java class, it automatically extends the class Object
   public static void main (String args[]) 
   {
       System.out.println("Hello world!");
   }
}


Example of Class in PHP

<?php
class A {
   public function foo() {
       echo “Hello World”
   }
}
?>

Examples in C#

using System;

public class Program
{
   public static void Main(string[] args)
   {
       System.Console.WriteLine("Hello world!");
   }
}

This example demonstrates a traditional "Hello world!" example in Microsoft's C# language. The Program class contains a single static method, Main(), which calls System.Console.WriteLine to print text onto the console.

Example in Ruby

class Person 
  @@count = 0 
  def initialize(name, height) 
  @name = name 
  @height = height 
  @@count += 1 
 end 
 def self.info 
  puts "Number of instances = #{@@count}" 
 end 
 def to_s 
  "Name: #{@name} Height: #{@height}" 
 end 
end

This defines a Person class with attributes name and height. It has a class variable count, which keeps track of the number of instances of the class created.

Examples of Type in Java

public class Color 
{
   String Red, Blue, Green;
}
public class Point 
{ 
   public int x, y; 
}
public interface IBase 
{
   void F();
}
public interface IDerived extends IBase 
{
   void G();
}
public class A 
{
   protected  void H() {
       System.out.println("A.H");
   }
}
public class B extends A implements IDerived  
{
   public void F() {
       System.out.println("B.F, implementation of IDerived.F");
   }
   public void G() {
       System.out.println("B.G, implementation of IDerived.G");
   }
   protected void H() {
       System.out.println("B.H, override of A.H");
   }
}

Examples of Type in C#

public enum Color
{
   Red, Blue, Green
}
public struct Point 
{ 
   public int x, y; 
}
public interface IBase
{
   void F();
}
public interface IDerived: IBase
{
   void G();
}
public class A
{
   protected virtual void H() {
       Console.WriteLine("A.H");
   }
}
public class B: A, IDerived 
{
   public void F() {
       Console.WriteLine("B.F, implementation of IDerived.F");
   }
   public void G() {
       Console.WriteLine("B.G, implementation of IDerived.G");
   }
   override protected void H() {
       Console.WriteLine("B.H, override of A.H");
   }
}

Above examples clearly demonstrate usage of type. Red, blue and green have type of ‘Color’. X, Y are ‘Point’ type.

Here is another excerpt that distinguish class and type. “Classes were created with 'class', and they were fundamentally different from C types created in extension modules. All instances of old-style classes are of type 'instance', which is why they have the __class__ attribute, so you can find their actual class. (Functions like "isinstance" contain hacks to support old-style classes.) New-style classes elevate Python-level classes to types equal to the built-in ones, which is why the word "type" is now sufficient for both.”

Additional Resources

Java Related resources

http://java.sun.com/docs/books/tutorial/java/concepts/class.html

http://www.neiu.edu/~ncaftori/java/class06.html

http://www.javalobby.org/java/forums/t18560.html

Other Programming Languages

http://www.cplusplus.com/doc/tutorial/classes.html

http://www.ibiblio.org/g2swap/byteofpython/read/oops.html

http://msdn2.microsoft.com/en-us/vbasic/ms789107.aspx

http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html