Docstoc

OM

Document Sample
OM Powered By Docstoc
					                                           Object-Oriented Models
Problems with traditional relational databases and complex value databases

1. In relational databases, we have to use printable values called keys to refer to entities or objects in the
   world.

   Features of DB keys

  a)     It is a database concept
         From a physical point of view, many bytes of his or her names or ID
         must be used.

  b)     It carries information

  c)     It is updatable
         If the person changes his or her name or ID, then there is a break
         in the continuity in the representation of the person.
         We have to update all tuples refer to this person to reflect the change of the name

  d)     It must be unique in a DB state

  e)     It often has merge problem

  f)     It is often assigned by DB user

  g)     It is visible to the user

2. duplications in data storage

       easy to see

       EMPLOYEE
           __________________________________________________________
           | Name     BDate         Children                 Phones |
           |       ___________     _______________                  |
           |       |Year M D|      | Name Age    |         _______ |
           | Tom   |1970 1 10|     | Bob     8   |         | 1234 | |
           |                       | Ann     5   |         | 2341 | |
           |------------------------------------------------------ -|
           |       ___________     _______________                  |
           |       |Year M D|      | Name Age    |         _______ |
           | Pam   |1975 2 10|     | Bob     8   |         | 1234 | |
           |                       | Ann     5   |         | 2341 | |
           ----------------------------------------------------------
  3.     update managements
         if Tom and Pam have a new baby or get rid of a phone number
         more than one tuple is updated.
     4.    impossible for circular reference
           a person database with children who are persons

           Type person = [name:string, children:{person}]

           [name:'Tom',children:{[name:'Bob',children:[...]}]
           cannot stop

Solutions: object-oriented and object relational databases

Key features of object-oriented databases

1.        use object identifiers (oids) to represent real world entities
          use oids to reference them

2.        complex objects
          objects encapsulate values which can be as simple as an atomic value and as complex as a nested sets
          & tuples
          complex relationships between objects can be represented as attributes of objects using references
          and sets and tuples of references

3.        Object classes
          Objects are grouped into classes. All objects in a class have complex values of the same type.
          An object can belong to one class directly (direct instance) but can belong to several classes
          indirectly (see below)

4.        Class hierarchy and inheritance
          Classes can have subclasses and thus all classes form a hierarchy by subclass relationship
              Property inheritance:
                 A subclass monotonically inherits the type of its superclasses and can override and block
                 some properties inherited.
              Instance inheritance
                 A super class inherits all instances of its subclasses


Object identifiers
       each object has a unique identifier (OID) and this association between OID and object
       remains fixed, even thought the attribute values change.

     Features of OID

     a)    It is a modeling concept

     b)    It does not carry any information

     c)    It is non-updatable

     d)    It is unique across all possible states of the world

     e)    It has no merge problem
f)     It is generated by the system

g)     It is visible to the user

       objects can share information gracefully.

      __________________________________________________________
      | Name     BDate         Children                 Phones |
      |       ___________                                      |
      |       |Year M D|                                       |
 o1   | Tom   |1970 1 10|        o21                    o31    |
      |------------------------------------------------------ -|
      |       ___________                                      |
      |       |Year M D|                                       |
 O2   | Pam   |1970 1 10|        o21                    o31    |
      ----------------------------------------------------------
           ___________
    o21    | Name Age |
    o211 | Bob 8 |
    o212 | Ann 5 |
           ________
    o31    | 1234 |
           | 2341 |

       o1 -> [Name: Tom, BDate: [Year:1970, Month:1, Day:10],
              children: o21, phones: o31]
       o2 -> [Name: Pam, BDate: [Year:1970, Month:2, Day:25],
              children: o21, phones: o31]

       o21 -> {o211,o212}
       o31 -> {1234,2341}

       o211 -> [Name: Bob, Age: 8],
       o212 -> [Name: Ann, Age: 5]

       update: Tom and Pam delete the phone 2341:
              one deletion to the object identified by o31 is enough

     Another way
                   _____________________________________________________
                   |   Name      BDate         Children         Phones        |
                   |           _________                                      |
        o1         |   Tom    |1970 1 10|       {o3,o4}         {1234,4321}   |
                   |           _________                                      |
        o2         |   Pam    |1970 2 25|       {o3,o4}         {1234,4321}   |
                   |          ___________                                     |
        o3         |   Bob    |1995 1 10|                                     |
                   |          ___________                                     |
           o4       | Ann   |1996 1 10|                                     |
                    ---------------------------------------------------------
2. Values

       atomic values:    Tom, Pam, 8, 1
       OID values:       oid21, oid31
       tuple value:      [Name: Bob, Age: 8]
       set value:        {1234,2341}

3. Types

  a)   Primitive types

       bool, int,       float, char,    string, etc

  b)   object types

       Person
       Student
       Employee,

       Personset
       Phoneset

  c)   Set types

       if r is a type, then {r} is a set type

       {int}, {Person}

  d)   Tuple types

       if r1,...,rn are types, A1,...,An are distinct attribute names
       then [A1:r1,...,An:rn] is a tuple type.

       address = [street: string, no: int, city: string]

  e)   Circular reference (type)

       Person [name: string, spouse: Person, Children: {Person}]

       o1       [name:'Tom', spouse: o2, children: {o3,o4}]
       o2       [name:'Pam', spouse: o1, children: {o3,o4}]
       o3       [name:'Sam']
       o4       [name:'Jan']

4. Object classes

       Objects are grouped into classes. All objects in a class have complex values of the same type.
       Class employee [name: string, children: personset, phone: phoneset]
       Class person [name: string, children: personset]
       Class student [name: string, children: personset, takes: {course}]
       Class course [name: string, loc: string, time: date]

       Class personset {person}
       Class phoneset {integer}

   Note the use of object class personset, phoneset and set type {course}

5. OID assignment

       an OID assignment is a function π which maps each class to a disjoint finite set of OIDS

       π (Employee) = {o1, o2}       o1, o2 are direct instance of Employee
       π (Personset) = {o21}
       π (Phoneset) = {o31}
       π (Person) = {o211,o212 }

6. Objects

       an object is a pair of OID and a value (complex value)
       (oid, value)

       (o1, [Name: Tom, BDate: [Year:1970, Month:1, Day:10],
               children: o21, phones: o31])
       (o2, [Name: Pam, BDate: [Year:1970, Month:2, Day:25],
               children: o21, phones: o31])
       (o21, {o211,o212})
       (o31, {1234,2341})
       (o211, [Name: Bob, Age: 8])
       (o212, [Name: Ann, Age: 5])

7. Class hierarchy and inheritance

       Person [name: string, children: personset]
       Student under (isa or inherits) Person [takes: {course}]
       Employee under Person [phone: phoneset]
       Workstudent under Student, Employee

  implications

  7.1 instance inheritance

       π (Student) subseteq π (Person)
       π (Employee) subseteq π (Person)
       π (Workstudent) subseteq π (Student)
       π (Workstudent) subseteq π (Employee)

  7.2 type inheritance
      Student and Employee inherit the types of Person
      Workstudetn inherit the types of Student and Employee

      Result:

      class                        type
      person        [name: string, children: personset]
      student       [name: string, children: personset, takes: {course}]
      employee      [name: string, children: personset, phone:phoneset]
      workstudent   [name: string, children: personset, takes: {course}, phone:phoneset]

Another design
                 _____________________________________________________
                 | Name     BDate         Children       Phones          |
                 |        _________                                      |
        o1       | Tom   |1970 1 10|     {o3,o4,o5},     {1234,4321}     |
                 |        _________                                      |
        o2       | Pam   |1970 2 25|     {o3,o4,o5},     {1234,4321}     |
                 |       ___________                                     |
        o3       | Bob   |1995 1 10|                                     |
                 |       ___________                                     |
        o4       | Ann   |1996 1 10|                                     |
                 ---------------------------------------------------------
      update: Tom and Pam delete the phone 2341:
             still two deletions

      Person [Name: String, BDate: Date, Children: {Person}, Phones:{int}]

Directors
     _________________________________________
     |Director            Movies             |
     |              _______________________ |
     | Cook         |Title         Actors | |
     |              |Trouble       John   | |
     |              |              Edmund | |
     |              |              Shirley| |
     |              |              Alfred | |
     |              |              Cook   | |
     |              |Bird          Tippi | |
     |              |              Rod    | |
     |              |              Suzanne| |
     |              |              Alfred | |
     |              |Pyshcho       Tony   | |
     |              |              Janet | |
     |              |              Alfred | |
     -----------------------------------------
     |              _______________________ |
     | John         |Title         Actors | |
     |              |Night         John   | |
|              |              Edmund | |
|              |              Shirley| |
|              |              Alfred | |
|              |Beach         Tippi | |
|              |              Rod    | |
|              |              Suzanne| |
|              |              Alfred | |
|              |Sunlight      Tony   | |
|              |              Janet | |
|              |              Alfred | |
-----------------------------------------

Theatres
 _________________________________________
 |Name          Address        Phone     |
 |iMax          1234 St        1234567   |
 |Cinema        5432 St        7890123   |


Shows
 _________________________________________
 |Theatre            Movies              |
 |Champ         ______________________   |
 |              | Name         Times |   |
 |              | Trouble      1:30 |    |
 |              |              4:30 |    |
 |              |              8:30 |    |
 |              | Bird         2:00 |    |
 |              |              5:00 |    |
 |              |              10:00 |   |
 |                                       |
 |...                                    |
 -----------------------------------------

class Person [
        name: string,
        citizenship: string,
        gender: string]

class Director isa Person [directs: {movie}]

class Actor isa Person [acts_in: {movie}]

class Actor_Director isa Director, Actor

class Movie [title: string, directors: Director, actors: {Actor}]

class Theater [name: string,
        address: string,
        phone: string]
Example       (From KM94)

 Schema
     class Vertex
       [x, y, z: float;]
     end class;

      class Material
        [name: string,
              specWeight: float]
      end class;

      class Cuboid
        [v1, v2, v3, v4, v5, v6, v7, v8: Vertex;
              mat: Material;
              value: float]
      end class;

 Instantiation
      using either new Class() or .create()

      Cuboid myCuboid;
      myCuboid = new Cuboid(); or myCuboid.create();

      oid11-> v1: null
             v2: null
             v3: null
             v4: null
             v5: null
             v6: null
             v7: null
             v8: null
             mat: null
             value: 0.0

      myCuboid.v1 = new Vextex();             or myCuboid.v1.create();
      myCuboid.v1.x = 0.0;
      myCuboid.v1.y = 0.0;
      myCuboid.v1.z = 0.0;

      oid1 -> x: 0.0
             y: 0.0
             z: 0.0
      ...
      myCuboid.v8 = new Vextex();             or myCuboid.v8.create();
      myCuboid.v8.x = 0.0;
      myCuboid.v8.y = 0.0;
      myCuboid.v8.z = 0.0;
      myCuboid.value = 40.0;
      myCuboid.mat = new Material();  or myCuboid.mat.create();
      myCuboid.mat.name = "Iron";
      myCuboid.mat.specWeight = 0.89;

 result:

      oid21-> name: "Iron"
             specWeight: 0.89

      oid11-> v1: oid1
             v2: oid2
             v3: oid3
             v4: oid4
             v5: oid5
             v6: oid6
             v7: oid7
             v8: oid8
             mat: oid21
             value: 40.0

Example

      class PhoneNos
        {int}

      PhoneNos myPhones = new PhoneNos(); or PhoneNos myPhones.create();
      myPhones.insert(1234567);
      myPhones.insert(7891234);

      oid21->{1234567,7891234}

Example

      class Person [
      name: string,
      birthyear: int
      spouse: Person
      livesIn: City]
      end class;

      class City[
      name: string,
      mayor: Person,
      inhabitants: PersonSet]
      end class;

      class PersonSet
      {Person}
      end class;
       City cityofLA;
       Person mickey, mini, donald;

       cityofLA.create(); or cityofLA = new City()
       mickey.create();
       mini.create();
       donald.create();

       ... and finally we get the following result:

       oid11-> name: "Los Angeles"
              mayor: oid1
              inhabitants: oid21

       oid1 -> name: "Mickey Mouse"
              birthyear: 1940
              spouse: oid2
              livesIn: oid11

       oid2 -> name: "Mini Mouse"
              birthyear: 1950
              spouse: oid1
              livesIn: oid11

       oid3 -> name: "Donald Duck"
              birthyear: 1955
              spouse: NULL
              livesIn: oid11

       oid21 -> {oid1,oid2,oid3}

       int totalAge, ageOfSomeone
       Person anyBody;

       ageOfSomeone = cityOfLA.mayor.spouse.age;
       foreach (anybody in cityOfLA.inhabitants)
         totalAge = totalAge + anyBody.age;

                                              Behavior of Objects

1. Methods: used to manipulate objects in the class.

       class Vertex
         type [x, y, z: float;]
         methods:
               translate(Vertex v) -> void;
                 self.x = self.x + v.x;
                 self.y = self.y + v.y;
                 self.z = self.z + v.z;
             }
             scale(Vertex v) -> void {
             ...
             }
             distance(Vertex v) -> float {
                 float dx,dy,dz;
                 dx = self.x - v.x;
                 dy = self.y - v.y;
                 dz = self.z - v.z;
                 return sqrt(dx*dx + dy*dy + dz*dz)
             }
             inOrigin() -> bool {
             ...
             }
        end class

       class Cuboid
         type [v1, v2, v3, v4, v5, v6, v7, v8: Vertex;
                mat: Material;
                value: float]
         methods
               length() -> float {
                   return v1.distance(v2)
               }
               width() -> float {
                   return v1.distance(v4)
               }
               height() -> float {
                   return v1.distance(v5)
               }
               volume() -> float {
                   return length() * width() * height()
               }
               weight() -> float {
                   return volume() * mat.specWeight
               }
               translate(Vertex) -> void {
               ...
               }
               scale(Vertex) -> void {
               ...
               }
               rotate(float, char) -> void {
               ...
               }
         end class

2. Constructor and Destructor

       Instantiation
      Cuboid myCuboid;
      myCuboid = new Cuboid(); or myCuboid.create();
      myCuboid.v1 = new Vextex();    or myCuboid.v1.create();
      myCuboid.v1.x = 0.0;
      myCuboid.v1.y = 0.0;
      myCuboid.v1.z = 0.0;
      ...

      class Cuboid
        type [v1, v2, v3, v4, v5, v6, v7, v8: Vertex;
               mat: Material;
               value: float]
        methods
              Cuboid(Material m) { //constructor
                mat = m;
                v1.create();
                v1.x = 0;
                v1.y = 0;
                v1.z = 0;
                v2.create();
                ...
                v8.create();
              }
              ~Cuboid() -> void { //destructor
                v1.delete();
                v2.delete();
                ...
                v8.delete();
              }

      Cuboid myCuboid;
      Material m = new Material("Iron");
      myCuboid = new Cuboid(m);

      constructors are used automatically when the object are created
      to perform some needed initializing work

      destructors are used automatically when the object are deleted

3. Overloading

      Constructor overloading

             Cuboid(Material m) { //constructor
              mat = m;
              v1.create();
              v1.x = 0;
              v1.y = 0;
              v1.z = 0;
               v2.create();
               ...
             }
             Cuboid(Material m, Integer v) {    //constructor
               mat = m;
               value = v;
               v1.create();
               v1.x = 0;
               v1.y = 0;
               v1.z = 0;
               v2.create();
               ...
             }
             Cuboid(Material m, Vertex v1,v2,v3,v4,v5,v6,v7,v8) {
               mat = m;
               self.v1 = v1;
               ...
               self.v8 = v8;
             }

      Cuboid myCuboid, yourCuboid;
      Material m = new Material("Iron");
      myCuboid = new Cuboid();
      yourCuboid = new Cuboid("Iron", 40.0);

4. Parameter Passing

  Methods

      selectExpensiveCuboids(CuboidSet allCs, CuboidSet selectedCs) -> void {
         var Cuboid candidate;
         foreach (candidate in allCs)
           if (candidate.value > 500.0)
                 selectedCs.insert(candidate);
      }

      selectedLargeCuboids(CuboidSet allCs, CuboidSet selectedCs) -> void {
         var Cuboid candidate;
         foreach (candidate in allCs)
           if (candidate.volume > 100.0)
                 selectedCs.insert(candidate);
      }

      combined

      selectCuboids(CuboidSet allCs, bool Pred, CuboidSet selectedCs) {
         var Cuboid candidate;
         foreach (candidate in allCs)
           if (candidate.Pred)
                 selectedCs.insert(candidate);
         }

         pass selection predicates as parameter

5. A Problem

         class Person
           [name: string, birthyear: int, spouse: person]
           methods
             age() -> int {
                 return (2010 - birthyear)
             }
             marry(Person p) {
                 spouse = p;
             }
         end class;

         class Employee
           type [name: string, birthyear: int, spouse: person, boss: Employee]
           methods
             marry(Person p) {
                 spouse = p;
             }
             isRetired() -> bool {
                 return (age() > 64)
             }
             salary() -> integer {
                 return (age() * 1000)
             }
         end class

       Employee type contains all the features of Person type
         plus additional properties and methods.

  Problems

  1)     lacking reusability

         redefine attributes/methods in Employee

  2)     Lacking flexibility

         Person and Employee are not related at all by the above definition

  Example

         Employee mickeyMouse;
         Person miniMouse;
         mickeyMouse.marry(miniMouse);         ok
         miniMouse.marry(mickeyMouse);         illegal
6. Solution: Inheritance

       class Person
         [name: string, birthyear: int, spouse: person]
         method ...
       end class

       class Employee isa Person
         [boss: Employee]
         methods
           isRetired() -> bool {
              return (age() > 64)
           }
       end class

  a)   Type/methods inheritance from Person to Employee

       class Person
         [name: string, birthyear: int, spouse: person]

       class Employee isa Person
         [name: string, birthyear: int, spouse: person, boss: Employee]

       Employee is called a subtype of Person
       (even it has more!)

  b)   Instance inheritance from Employee to Person

       Employee mickeyMouse;
       Person miniMouse;
       mickeyMouse.marry(miniMouse);         ok
       miniMouse.marry(mickeyMouse);         ok too

       every instance (object) of a subclass is also an instance of
       the superclass. It understands all methods that Person has.

7. Typing rules

       Person mini;
       Employee mickey;
       Employee donald;
       integer i;

       mickey.spouse = mini;                          ok
       mini.spouse = mickey;                          ok
       mickey.boss = donald;                          ok
       mini.spouse.boss = donald;                     illegal
       i = mickey.boss.birthyear;                     ok
       i = mini.boss.birthyear;                       illegal
       i = mini.spouse.spouse.birthyear;   ok
       i = mickey.spouse.boss.birthyear;   illegal
       mickey.boss.spouse.marry(donald);   ok type-wise

8. A program

       oid1 -> name: "Mickey Mouse"
              birthyear: 1940
              spouse: oid2
              livesIn: oid11

       oid2 -> name: "Mini Mouse"
              birthyear: 1950
              spouse: oid1
              livesIn: oid11

       oid3 -> name: "Donald Duck"
              birthyear: 1955
              spouse: NULL
              livesIn: oid11

       oid21 -> {oid1,oid2,oid3}

       Person c;
       PersonSet people;

       int totalAge = 0;
       int count = 0;
       int averageAge = 0;
       ...

       foreach(c in people) {
         totalAge = totalAge + c.age()
         count = count + 1;
       }
       averageAge = totalAge / count;

9. Late binding

       class Square isa Shape
         type [length: float]
         method
           area() -> float {
                return (length * length)
           }
       end class

       class Rectangle isa Shape
         type [length, width: float]
         method
         area() -> float {
             return (length * width)
         }
      end class

      class Circle isa Shape
        type [radius: float]
        method
          area() -> float {
              return (3.14 * radius * radius)
          }
      end class

      oid1 -> length: 100            square

      oid2 -> length: 100            rectangle
             width: 50

      oid3 -> radius: 50

      oid21 -> {oid1,oid2,oid3}

      Shape c;
      ShapeSet s;

      int totalArea = 0;

      foreach(c in s)
        totalArea = totalArea + c.area()

10. Anchor Type Constraints

      class Person
        methods
          incAge() -> Person {
              age = age + 1
              return self
          }

      Person someP;

      someP.incAge().incAge();

      class Employee isa Person
        methods
          incSalary(int s) -> Employee {
              salary = salary + s
              return self
          }
       Employee someE;

       someE.incSalary(100.0).incAge();       ok
       someE.incAge().incSalary(100.0);       rejected by
                                              compiler type checker
  Solution

       class Person
         methods
           incAge() -> self.Class {
               age = age + 1
               return self
           }

       class Employee
         methods
           incSalary(int s) -> self.Class {
               salary = salary + s
               return self
           }

       Employee someE;

       someE.incAge().incSalary(100.0);       ok

1. Summary of object operators

  a)   new:   create a new instance of a class and
              run constructor       automatically if any
       usage: Person p = new Person(); or
              Person p = new Person('John') if a constructor
                      Person(String N) is defined

  b)   create: same as new but used differently
       usage: Person p;
               p.create(); or
               p.create('John');

  c)   delete: delete an existing instance of a class and
               run destructor automatically if any
       usage: p.delete();

2. Set object operations

  a)   insert: insert an element into a set object
       usage: PersonSet Ps = new PersonSet();
               Person P = new Person();
               Ps.insert(P);

  b)   remove:        delete an element in a set object
       usage: Person P;
              Ps.remove(P);

              to delete an element in a set object that satisfy some
              condition
              foreach (P in Ps)
                if (P.name = 'John') P.delete()


3. Subclasses of a Sets types

       class PersonSet
           type {Person}
       ...

       class EmployeeSet
         type {Employee}

       ...

       Person P                 P -> p3
       PersonSet Ps             Ps -> {p1, p2}
       EmployeeSet Es                   Es -> {e1, e2, e3}

  Question: is Es an instance of PersonSet? If so, then

       Ps = Es;                 Ps,Es -> {e1, e2, e3}
       Ps.insert(P)             Ps,Es -> {e1, e2, e3, p3} illegal

       Object -> Person -> Employee -> Manager -> CEO
       |-> PersonSet
       |-> EmployeeSet
       |-> ManagerSet
       |-> CEOSet

  All set- and list-structured classes are located directly below Object

  Question: can PersonSet have a subclass?

    class MyPersonSet isa PersonSet
         type                        empty
         methods
           avgAge() -> float {
              float total = 0;
              int count = 0;
              foreach (emp in self) {
                 total = total + emp.Age;
                 count = count + 1;
              }
              return (total/count)
          }
       end class MyPersonSet

4. Virtual class and Virtual Methods

       class has no direct instance
       method has no implementation only the signature

       increase the flexibility of the object model

       virtual class Shape
         methods
           virtual area() -> void {}
       end

       class Square isa Shape
         methods
           area() -> void {
               ...
           }
       end

       class Rectangle isa Shape
         methods
           area() -> void {
               ...
           }
       end

       virtual class Faculty
          ...
       end

       class AssProf isa Faculty
          ...
       end

       class AssoProf isa Faculty
          ...
       end

       class Professor isa Faculty
          ...
       end

5. classcase

       to specify code that depends on the dynamic binding

                      Person
                     /    \
      gpa --- Student                     Employee --- salary
                               |
                               Manager --- Cars

      every person gets bonus but in different form

  Program fragment

      Person john;
      Car benz;
      Person p;

      ...
      classcase (p)
          case Student:
                p.gpa = pga * 1.1;
          case Employee:
                p.salary = salary.*1.1;
          case Manager
                p.cars.insert(benz);
      end

6. Non-monotonic Multiple Inheritance

      Student   Employee
            \   /      \
             TA          Manager

  Problem

      class Employee
        type [..., room: String]
        method
           bonus(float r) -> void {
                salary = salary * r
              }

      class Student
        type [..., room: Integer]
        method
           bonus(float r) -> void {
                gpa = gpa * r
              }

      class TA isa Employee, Student

      Employee bestEmp;
      Student bestStu;
      TA Sam, Tom;
       Boss Jim;

       bestEmp = Sam;                due to instance inheritance
       bestEmp.room = 'CL300';       supposedly room in Employee
       bestEmp.bonus(1.1)            supposedly salary
       bestStu = Tom;
       bestStu.room = 123;           supposedly room in Student
       bestStu.bonus(1.1)            supposedly gpa
       jim.bonus(1.1)                no ambiguity can occur

  Kind of conflicts

       1. attribute conflicts

         same attribute or same attribute name in both superclasses

       2. method conflicts

         same method or same method name in bother superclasses

7. Conflict Resolutions

  a)   User-Specified Priority

       The order of attributes and methods is based on the order of
       superclasses

       class TA isa Employee, Student

       bestEmp = Sam;
       bestEmp.room = 'CL300';       supposedly room in Employee      ok
       bestEmp.bonus(1.1)            supposedly salary                ok
       bestStu = Tom;
       bestStu.room = 123;           supposedly room in Student       wrong
       bestStu.bonus(1.1)            supposedly gpa                   wrong
       jim.bonus(1.1)                no ambiguity can occur

       problem

       only one attribute/method can be inherited

  b)   Refinement (overriding)

       class TA isa Employee, Student
         type [room: String]
         method
           refine bonus (float r) {
                gpa = gpa * r;
                salary = salary * r;
           }
       or

       class TA isa Employee, Student
         type [room: String]
         method
           refine bonus (float r) {
                self.Student.bonus(r);
                self.Employee.bonus(r);
           }

       bestEmp = Sam;
       bestEmp.room = 'CL300';                 room in Employee     ok
       bestEmp.bonus(1.1)                      salary and gpa       ok

  c)   Explicit Renaming

       class TA isa Employee (rename room to eroom, bonus to ebonus),
                  Student (rename room to sroom, bonus to sbonus),

       bestEmp = Sam;
       bestEmp.eroom = 'CL300';          supposedly room in Employee       ok
       bestEmp.ebonus(1.1)               supposedly salary          ok
       bestStu = Tom;
       bestStu.sroom = 123;              supposedly room in Student ok
       bestStu.sbonus(1.1)               supposedly gpa             ok
       jim.bonus(1.1)                    no ambiguity can occur

8. Type Conflicts

       Due to conflict resolving, a subclass is no longer a subtype of a superclass.

       So this could lead to a typing error

       class Student
         type [..., room: String]
         method
            skill() -> float {
                 return gpa;
               }

       class Employee
         method
            skill() -> String {
                 if (salary > 10000.0)
                   return 'high';
                 else
                   return 'low';
               }
           type [..., room: Integer]

          Employee someEmp;
          Student someStu;
          TA John;
          String empSkill;
          float stuSkill;

          someEmp = John;
          someStu = John;
          empSkill = someEmp.skill();
          stuSkill = someStu.skill();

          one of the two assignment statement is type inconsistent
          no matter which conflicting method we choose.
          So it leads to type inconsistency that can be detected only at run time.

          Type errors can only by avoided by out ruling the inheritance of conflicting features

          The problem is because of substitution of subclass instance for any of their superclasses

  Key Point

               Loss of type safety is the price one has to pay when using multiple inheritance unless no
               conflicting features

Benefit

          definition sharing
          code sharing

          organize objects into meaningful classes
          build taxonomies utilizing the isa relationship

9. Application

          C++: out ruling the inheritance of conflicting features

          Java: disallow multiple inheritance

10. Relations

          In Object Databases, it is still important to have relations (complex value relations with OID as
          values without OID). More generally, we can have a tuple which is not a relation,
          we can have a value too. The text book use name definitions instead.

11. Difference between class and relation

          classes have objects while relations have tuples
          objects have oids and values while tuple only has values
Example (based on Figure 21.1)

       Relation Pariscope
        type {[theater: Theater,
               time: string,
               price: integer
              ]}

       Relation Person-I-like
        type {person}

       Relation Actors-I-like
        type {actor}

       Relation Actors-you-like
        type {actor}

       Relation My-favorite-director
        type Director

  Instance

                      _________________________
       Pariscope:       theater   time    price   a set of tuples

                       ....

                      _______
       Person-I-like: person                      a set of oids

                       ....

       My-favorite-director: one oid as a value

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:8/31/2011
language:English
pages:25