2

Object Modelling

LEARNING OBJECTIVES

At the end of this chapter, you should be able to

  • Understand object modelling.

  • Use notations of UML to specify objects and classes, their notations, meta models and concepts like mandatory profiles, meta data, meta classes, etc.

  • Learn to use class diagrams and object diagrams of UML.

  • Understand the concept of links and hierarchy, polymorphism and abstract classes in OOPS language.

2.1 Introduction

Object modelling encompasses all the elements that we studied in Chapter 1 regarding objectoriented (OO) methodology. The principles of object modelling are abstraction, encapsulation, modularity, hierarchy, typing, concurrency and persistence.

In conventional programming styles, we conceive the problem and subdivide the main task into subtasks and develop functions or methods to solve these subtasks. We call this paradigm procedure-oriented programming style. In this paradigm procedure is primacy and data that is acted on is stored at a different location in computer memory called the code area. In object modelling, the object embodies data and the functions that act on the data as a single entity. To meet users’ requirements, the specifications are prepared by experts as simple statements of problems in English. In order to analyse these specifications, we need to study the vocabulary of the problem statements in terms of objects (nouns) and classes, and relationships between classes appearing in the problem domain.

2.2 Object Model

The basic building block in an OO language is a module. What is a module? Modules can be defined as logical collections of classes and objects. If functions are verbs, then objects are nouns. Objects encompass the functions and member data. The building block in an OO methodology is shown in Figure 2.1, where each module comprises a logical collection of classes which in turn contain objects. Objects call member functions from other objects, a phenomenon, we called it as message passing and thereby achieve the result.

 

Object-oriented programming methodology – Module

 

Figure 2.1 Object-oriented programming methodology – Module

 

We observe that our building blocks are not a function or a procedure or an algorithm, but rather the classes and objects and messages passing between them. Observe from the above diagram that individual objects are responsible for their own member data and a set of functionality. Thus objects store data and also functions necessary to update the data and do the necessary computations on the stored data. In addition, cooperating objects communicate with each other by invoking functions and passing information amongst them.

Object modelling is not only about mere object and contained data. Many a times, the system requires that links between data belonging to different objects be defined to achieve some global functionality. The object model must contain the relationship between the classes and data belonging to objects belonging to different classes. This will be decided by inheritance relationship between the classes.

The links between objects are message calls. They are requests sent to receiving objects to execute the function belonging to it with the parameters sent by requesting objects.

We are now ready for the definition of OO programming as a collection of cooperating objects which are in turn instances of a class. The cooperating classes have a hierarchy of relationships defined by inheritance relationship.

Notice that OOP language is about cooperating objects and not procedures and functions. Further notice that objects are instances of classes. Classes themselves are linked to other classes via the inheritance relationship. Therefore, we can say that OOP language means: objects, classes, inheritance.

2.3 Object-oriented Design

At the end of analysis and design, we ultimately develop the code. The code has a structure comprising of objects, classes, messages and links between objects, and associations and relationships between classes.

Now wouldn't it be better if our design model were to consist of diagrams that had the same structure as that of the code? Better still, is there a graphical and diagrammatic language that can convert our structure into code? The answer is UML. There are two types of diagrams supported by UML. One set of diagrams depicts the structure, i.e. the interconnections and resulting topology. This set is also called static diagrams. The other set of diagrams, called dynamic diagrams, depicts the messages that are passed between objects.

 

Example 2.1:   College Administration Example – A Case Study

In this section, we use the college administration system to explain the concepts in object modelling and its implementation in UML. College academic administration revolves around students, teachers and courses. Colleges have departments, and departments offer courses. Students register for the courses offered by a teacher.

We explain object modelling with object models that pertain to college administration and include actors like students, teachers, HODs, courses, departments, etc. We use this as a running example to learn about OOPS concepts and also while dealing with specifics of C++ and Java in subsequent chapters. Basic to our model is a class called student. It contains information such as roll number, name, etc. We use a class called student and store information about students in a database called StudentMaster. Course files will have the details of students registered for a particular course. All such courses will be a part of a department. Colleges running courses such as BTech, MBA and MCA would in turn be a part of a campus.

2.4 Classes and Objects

The object is an entity that contains data and functionality. The college administration has data about students, teachers, and courses on offer. The decision we have to take in OOP design is how to divide data and functionality into meaningful objects that interact amongst them and produce the result the college administration expects.

The first thing that comes to mind is an object called student. In a college there are several students, but all have the same functionality and individual attributes. Hence, these objects can be grouped into a class called student.

A class is a collection of a set of objects that share a common behaviour and attributes. Class definition describes attributes as well as functions that implement the behaviour.

An object is an entity that contains attributes and member functions and an identity. Objects of common attributes and functionality are grouped together in a class.

2.4.1 Class

A class is a description or specification for the object, whereas the object is an instance of the class that is created at run-time. There can be one or several instances of the class. In Example 2.2, we show the representation of a student class in C++, Java, and UML.

 

Example 2.2:   Representation of Class Student in C++

class Student
{ // constructors and member functions
     public:
       Student(){rollNo=0,name=”No Name”;}
       Student(int n, char *p){rollNo=n,name=p;}
       int GetRollNo() const { return rollNo;}
       void SetRollNo ( int n) { rollNo=n;}
       char * GetName() const { return name;}
       void SetName( char *p) { name=p;}
     private: int rollNo; char *name; // member data & pointer to 
     name
};

 

Example 2.3:   Representation of Class Student in Java

public class Student
 {private String name;
     private long rollNo;
    public Student( String n , long r )
     {rollNo=r; name=n; }
    public String GetName() { return name;}
    public long GetRollNo() { return rollNo;}
}// end of class

 

Example 2.4:   Representation of Class Student in UML

 

Student class representation in UML

 

Figure 2.2 Student class representation in UML

2.4.2 Notations and Meta Models

UML employs notations and meta models. A notation is a graphical representation and syntax of the modelling language. A meta model, on the other hand, is a class diagram that is used to define the rigor and intricacies of the language. In the following example, you will see that we will use notations of UML, as well as meta models involving class diagrams, to explain the intricacies of generalization, specialization, etc. Fortunately, we can use the same representations in C++ and UML. A rectangle box represents a class of objects. The name of the class is at the top. In the next part of the box, list the data that each object contains. In the third part of the class box, list the operations (functions) that an object can carry out.

2.4.3 Mandatory Profile

It defines minimum services, i.e. functionality to be provided by a class. Therefore, it is essential that class definition contain a constructor for the proper initialization of objects. This initialization is achieved through parameterized constructors with default arguments to take care of the correct and required initialization of class attributes and the creation of objects with minimum mandatory services. Consider the example shown below:

class Student
{// constructors and member functions
    public:
       Student(){rollNo=0,name=”No Name”;}
      Student(int n, char *p){rollNo=n,name=p;}
        int GetRollNo() const { return rollNo;}
        void SetRollNo ( int n) { rollNo=n;}
        char * GetName() const { return name;}
        void SetName( char *p) { name=p;}
    private: int rollNo; char *name; // member data& pointer to name
};

If no parameters are provided at the time of the creation of an object, then default values specified by the default constructor are used in initializing the object. The default constructor requires an appropriate behaviour for this class. Hence, we have provided “no name“: for the name and 0 for the roll number. Later in the program we can use the SetRollNo() and GetRollNo() functions to modify the initial values:

Student std; // object created
std.SetRollNo(6060);
std.SetName(“Gautam”);

In the above declaration, it can be seen that we need to provide two parameters, namely student's number and student's age. Creating objects without proper initialization often leads to many programming faults and problems. Hence, it should be a good programming practice to define a default constructor as part of a mandatory profile that is put to work by the compiler if the user does not define a suitable constructor. Indeed C++ and Java support the concept of a default constructor as part of a mandatory profile.

2.4.4 Meta Data – Meta Class

2.4.4.1 Meta Class

In object-oriented programming, meta class is a class created by language and all other user-defined classes are instances of this meta class. Meta class defines the behaviour of user-defined classes and their instances. Meta class implementation is individual to languages like C++ and Java, and there is no uniform protocol to implement meta class.

When you create an instance of class and class methods to your class, they are actually added to meta class instance. It means that you are able to change the behaviour of instance that you have created to suit your requirements. In Java, classes you create are instances of java.lang.Class. However, Java does not support meta classes directly, but here too there is exactly one instance of class of java.lang. We can use the methods defined in java.lang.Class: getName() or newInstance() or getMethod(), getConstructor(), isArray(), getInterfaces(). For example, we can get the class description for an object's class by calling getClass() on the object.

2.4.4.2 Meta Data

Meta classes in addition hold meta data, called attributes, i.e. data belonging to meta classes. Meta classes allow meta data to be accessed by classes or packages. The attributes are specified in an object-oriented language as markup script, which meta class is capable of reading and loading at run-time, so that meta data is made available to the programmer.

2.4.5 Constraints

Constraints will help the designer to develop the system as per specifications by ensuring one-to-one mapping of operations. Specifying operations are usually carried out in graphical modelling so that they define underlying constraints with precision. The net result is a better designed system.

  • Performance: Performance criteria are defined at the class definition level. Performance dictates the operations. Operations, in turn, decide the responsibilities of the class.
  • Reliability: Reliability of a class depends on the way the reliability aspect is distributed among the other classes and the way one class interacts with other classes. There should be a match between user operational profiles and functional profiles.
  • Security: Security issues crop up because the user at operational level is able to exploit some operations that are not part of original the specification. This loophole might have been created inadvertently at operations of other classes’ levels. The criteria to be considered to plug these loopholes are described below.
  • Access Control: Subdivision of functionality must ensure that only certain classes or users are able to initiate and use the functionality with authorization codes.
  • Integrity of Data: The data is primacy and no unauthorized access can be allowed at submodules. Suitable encryption techniques can solve the problems.
  • Control of User Behaviour: The normal behaviour of user of a class must be specified and any deviations from this normal and designed process must be recognised by the module, and further exploitation of unintended operations must be denied.

2.4.6 Object Creation

Once a class is defined at run-time we can go ahead and create an instance of the class, i.e. object. Remember that an object is a variable of a class. Therefore, you can define the object just like you define the variable of a data type. In the example that follows, we create an ordinary object called std and 50 instances of class student, and also a pointer that creates an instance of student on the heap memory.

 

Example 2.5:   Creation of an Object of a Class in C++

Student Std; // Std is an object of class Student
Student Std[50]; // There are 50 instances created for Student class
Std.SetRollNo(8345); // Sets roll no to 8345

 

Example 2.6:   Creation of an Object of a Class Student in Java and Assigning Initial Values

Student Std = new Student(757,“Anand”)// std is an object of class Student
757 is rollNo and Anand is name allocated to this instance of object. Std is name of the object.

 

Example 2.7:   Creation of an Object of a Class Student in UML and Assigning Initial Values

2.4.7 Garbage Collection

Garbage collection is the automatic detection and freeing of memory that is no longer in use. An object becomes available for garbage collection when it is no longer referred to by any other object. Java uses garbage collection rather than explicit destructors found in other OO languages such as C++.

2.5 Object Properties

Object is an entity that has its own data structures and functionality to implement the behaviour. What are the essential properties that an object should possess?

 

State: An instance of an object is an object, created at run-time with initial values set by a default constructor. The values are particular to that instance and can be changed at any time during the life of an object by accessory functions defined within the objects (see Figure 2.3). The values of 757 and Anand are true only at that instance and hence they represent the state of the object.

 

An instance of object represented in UML

 

Figure 2.3 An instance of object represented in UML

 

Behaviour: An object's behaviour is decided by functionality defined by the class definition. However, though there can be several instances of the class, i.e. several objects belonging to the same class, all of these instances will have the same behaviour defined by the class. Hence, we have not shown behaviour, i.e. functions separately in Figure 2.3. The behaviour can be in terms of functions and methods that update and modify member data or general functions that serve as interfaces to implement some global functionality. These functions are invoked by other objects and receive the arguments and provide service to other objects.

Object name: When an object is created, it bears a name. For example, Std is the name of the instance of the class called student. This is the identity of the object.

   Student Std; // C++ Std is an object of class Student
   Student Std = new Student(757,“Anand”)// Java

Object's identity: Each instance of the object is different from the other instances of the object, even though they contain the same data values. When an object is created, memory resources are allocated. Therefore, unique id is the memory address of the object.

Encapsulation: Objects’ member data are declared private and hence the details are hidden and are not available to the outside world. In OO languages like C++ and Java, the definitions for attributes and methods are placed inside a class definition and hence are not available to the outside world, thus hiding internal details from the outside world. This is known as encapsulation.

2.6 Links

We have learnt that object modelling is not simply a collection of isolated objects. As shown in Figure 2.4, the system requires that links between data belonging to different objects need to be defined to achieve some global functionality. There are three distinct objects belonging to three different classes: batch, student and Registration. The class called batch will have instances of students registered for all courses for that batch, say mechanical third semester. Here, only the student's number is held and in order to get full details of the student, we need to refer to another object called Registration. This means that for each student object, we need to maintain a link to Registration object so that we can refer to it for full details of the student. Similarly, we need to maintain a link class called batch so that we can get batch details of the student.

We will show the implementation of link in C++ and Java and UML as usual. However, as a cautionary note, the examples in C++ and Java require understanding of the rudiments of programming skills in these languages. Hence, first-time readers can skip examples in C++ and Java and concentrate only on UML; they can then revisit these topics after acquiring the necessary C++ or Java skills.

 

Example 2.8:   Links in C++

class StudentMaster
{  public: StudentMaster(char *p, int n){name=p, rollNo=n;}
     char * GetName() const { return name;}
     int GetRollNo() const { return rollNo;}
   private: int rollNo; char *name; // pointer to name}; // end of 
class
class Student
    { public: Student(StudentMaster *std ){ sm=std;}
      char *GetName() const { return Sm->GetName();}
      private: StudentMaster *sm; };
void main()
{StudentMaster *sm = new StudentMaster ( “Anand”,757);
  Student * S1 = new Student(sm);
  cout<<s1->GetName();}

 

Example 2.9:   Links in Java

public class StudentMaster
 {    private String name;
          private long rollNo;
        public StudentMaster ( String n , long r)
        {name=n; rollNo=r; }
        public String GetName() { return name;}
        public long GetRollNo() { return rollNo;}
   }// end of class
   public class Student
   { private StudentMaster sm;
          public Student( StudentMaster std)
          { sm= std; }
 }// end of Student class

From the above, you can clearly see that whenever an object of student, i.e. sm, is created, we also create a reference to it in StudentMaster. We will show you how these links are created:

     StudentMaster sm = new StudentMaster(“Anand”,757);
      Student S1 = new Student(sm);
     Student S2 = new Student(sm);
     ……………………………………………… and so on for five subjects
      Student S5 = new Student(sm);

 

Example 2.10:   Links in UML

A link is shown in UML by an arrow pointing to the object:

 

Links in UML

 

Figure 2.5 Links in UML

2.7 Class Diagrams

Class diagrams provide system overview. They show classes and their relationships with other classes. They are static diagrams. This means that they tell us about the interactions amongst classes, but they do not tell us the response to these interactions. We will explain the class diagrams and various terms involved with the modelling of library book ordering systems. Class diagrams show a librarian making a purchase from a supplier by raising an order and afterwards making a payment. Payment can be either by cash, cheque or credit. Order, on the other hand, contains book details (see Figure 2.6). A detailed explanation of the above class diagrams and concepts are explained in the succeeding sections.

 

Class diagram for library procurement system in a college

 

Figure 2.6 Class diagram for library procurement system in a college

2.8 Class Hierarchy

In order to bring out relationships and dependencies amongst classes, we need to arrange classes into hierarchies.

2.8.1 Associations

Associations are a type of hierarchy that show relationships between classes that are independent and are of equal standing. These relationships are shown by lines between classes; they are usually labelled with the name of the association. Classes in an association usually occupy equal places within a hierarchy. For example, in Figure 2.6, librarian and order are independent classes.

Multiplicity: 0..* at order class means that librarian can place an unlimited number of orders, i.e. from 0 to unspecified number of orders. 0…1 means 0 to 1. Similarly, we can also state the maximum number of books that can be borrowed by a student as 0…4, as shown in Figure 2.8.

 

Association between classes. Association is named : ordering

 

Figure 2.7 Association between classes. Association is named : ordering

 

Association 0…1

 

Figure 2.8 Association 0…1

 

A student can borrow 0 to many books.

A book can be borrowed by at most 1 student (see Figure 2.6). The navigability shown above implies the direction of the association and who owns the association's implementation. The navigability tells us that we can find out details of books by querying DeptOrder. Also make a note that DeptOrder is the owner of the association.

2.8.2 Hierarchies with Interdependent Classes

There are two types of hierarchies:

  • Whole/part of hierarchy. In this type of hierarchy, one class is included in the other class. This relationship is called has a relationship. Further, there are two subtypes in a whole/part hierarchy. They are as follows:
  • Aggregation. In this type, objects are not fixed. They can be included or removed. For example, consider the example in Figure 2.9. A department has some classes that have some students. Aggregation means “part of” relation.

 

Aggregation relationship. Student is a part of class, i.e. part of a department

 

Figure 2.9 Aggregation relationship. Student is a part of class, i.e. part of a department

 

In a class hierarchy involving whole/part, parts are essential, like in the case of a book. A book cannot exist without pages, a passenger train cannot exist without compartments, etc. In Figure 2.9, a class cannot exist without students. Hence, it is shown as a black diamond. A class contains 1 to many students, whereas a department can have no class, i.e. empty department. Hence, it is shown as a white diamond.

  • Composition. This is another type of whole/part type of hierarchy. In this type, the object is a whole or integral part of the whole. Examples are human has a heart; a computer has a microprocessor in it. Composition means “has a” relationship, i.e. the whole object contains a part.

Aggregation is shown by a line with a hollow diamond pointing to the whole class, while composition uses a solid diamond instead. One way to implement the whole/part hierarchy is to simply declare an instance of the aggregate object as an attribute, while defining the container class:

 

Example 2.11:   Example of Container Class in C++ and Java

class Date
{public:
     Date ( int d=0, int m=0,int y=0) :dd(d),mm(m),yy(y) { } // 
constructor
    void SetDate( int d, int m, int y){ dd=d,mm=m,yy=y;}
     int GetDD() const { return dd;}; int GetMM() const { return 
mm;};
    int GetYY() const { return yy;};
  private: int dd; int mm; int yy;
};
class Student
{ public:
   Student();
   ~Student(){}; // Default destructor
    int GetRollNo() const { return rollNo;}
    void SetRollNo ( int n) { rollNo=n;}
    Date GetDOB() const { return dateDOB;}
    Date GetDOJ() const { return dateDOJ;}
   void SetDOB( Date dtb) { dateDOB=dtb;}
   void SetDOJ( Date dtj) { dateDOJ=dtj;}
  private: int rollNo; Date dateDOB; Date dateDOJ; };

In the above example, observe that we have defined a class called date, with its own data members and member functions. The date class has dd, mm, yy as private attributes. Now a class called student has declared instances of date for date of birth and date of joining as dateDOB and dateDOJ.

2.8.3 Hierarchies with Independent Classes

The classes under this category have an is a relationship and are also referred to as generalization/specialization. In OOPS programming language, the implementing mechanism for this feature is called inheritance. For example, we have modelled Human Beings hierarchy in Figure 1.5. UML uses open arrow head image to show inheritance relationship.

We have shown an inheritance relationship in Figure 2.10 where in Four wheeler and two wheeler inherit from an Automobile. A base class is known as Super class in the literature. We will use base class in C++ and Super while dealing with java & UML. The derived class can extend the Super by adding new properties, and by selectively overriding existing properties of the super class. For example Father(Super) Moves from place A to B by Road, while the Son( sub class) Moves from place A to B by Air.

 

Inheritance hierarchy

 

Figure 2.10 Inheritance hierarchy

 

Multiple inheritance – more than one super class

 

Figure 2.11 Multiple inheritance – more than one super class

 

If a class derives from more than one superclass, it is called multiple inheritance. Java does not support multiple inheritances, while C++ supports it. There can be confusion and ambiguity regarding inheriting attributes and qualities from more than one parent class. Java solves this problem of ambiguity by introducing a concept called interface and C++ by resorting to explicit function calls. In Java's interface, class will use a Java's specification called implements that will allow implementing class to implement the code required by an interface. You will learn more about this aspect in language specifics.

 

Example 2.12:   Example of Multiple Inheritance in C++

class Student
{ public:
      ……….. class declaration here………..
  private:
 };
class Athlet
{  public:
      …….. class declaration here.: };
class Professional : public Student, public Athlet
{ public:
      …….. class declaration here. ……………..
 private:
};

2.9 Object Diagrams

Object diagrams show instances of class diagrams. For this example consider the college department organization. A class can have several departments under it. A department in an engineering college can have many departments under it. Figure 2.12a shows a class diagram called department. A department can also contain many subdepartments. For example, a department like electrical engineering can have subdepartments like electronics department and communications department. We can say that a department is a collection of subdepartments. This in UML is a representation of aggregation and is symbolized by placing a diamond.

 

Department contains 0 to many sub departments – Aggregation

 

Figure 2.12a Department contains 0 to many sub departments – Aggregation

 

The self-loop shown in Figure 2.12a depicts aggregation. It denotes that department can contain 0 to * , means 0 to many subdepartments. These departments are contained in 1 instance of the department. Hence, observe the diamond shown to indicate aggregation. The object diagram shown in Figure 2.12b instantiates the class diagram, replacing it by a concrete example.

 

Object diagram showing department containing several subdepartments

 

Figure 2.12b Object diagram showing department containing several subdepartments

2.10 Communications and Message Passing

Executing a program in today's technology is no more the execution of a sequence of logical programming steps. Most of the GUI programs have to respond to signals from external units such as mouse, sensors, etc. But the question is who will monitor trigger from external units? Is it the program written by the user or the operating system? The answer is an OS monitors the signals from external units and sends a signal to our program. Our program will have an interface to receive and interpret and take necessary actions on the signals sent by the OS. Indeed entire Windows and other modern operating systems achieve their tasks through this message passing mechanism.

There are also other sets of communications by cooperating objects. Objects send and receive messages. Sending a message is accomplished by invoking a function belonging to another object and passing parameters. Receiving a message would imply that a function belonging to the current object has been invoked by another object. Objects can thus respond to messages sent by OS as well as other objects.

2.11 Polymorphism

We have understood that in inheritance relationship subclass derives from superclass both methods and attributes. In inheritance, the subclass can extend and simply use the functionality of the base class or use its own functionality with totally different implementations of the same function. We have called this feature overriding base class functions.

We have shown in Figure 2.13 a human hierarchy. All humans have to work, so the human class defines a function called Work(). All Humans Work(), but a specialized class like student, which is a subclass of human, must work in a college and in libraries and laboratories. Hence, you will see that student has overridden the base class function called Work() with its own specialized Work() method. Also observe that CSESTUDENT would like use (inherit) the same Work() as that of its base class student. Hence, he has not defined its own version and hence base class function Work() is available to it. On the other hand, ECESTUDENT probably has to work in hardware laboratories and hence has overridden base class Students Work() with its own implementation.

 

Polymorphism at work: ECE students has own work(): CSE student works with Student Work() because CSESTUDENT has not overridden.

 

Figure 2.13 Polymorphism at work: ECE students has own work(): CSE student works with Student Work() because CSESTUDENT has not overridden.

 

When there are several subclasses implementing a function, it is critical that the correct function of the correct derived class be invoked. That is, at run-time, the correct function has to be invoked through a function call. The feature that ensures this correct run-time binding of function is called dynamic binding. Polymorphism means to ensure that objects of base class behave correctly and as planned. For example, CSESTUDENT must use Students Work() and ECESTUDENT must use its own Work() and so on.

2.12 Abstract Class

While designing an OO system, we may need a base class with no function implementation at all. The base class will have only the names of the functions and no implementation of these functions. However, the derived classes from this base class will override the base class function in which each of the overridden function will have its own implementation. Polymorphism and the feature of dynamic binding would ensure that depending on the user's requirement, the correct method belonging to the appropriate object would be linked to a function's call. The interface feature of Java is an abstract class. In C++, a base class with pure virtual functions can be considered as an abstract class. An abstract class, since it has no implemented functions, cannot have an object instantiated.

2.13 Concrete Class

As opposed to abstract classes, the concrete class implements all the functions and methods declared in it. Hence, a concrete class is a class that can have an object or instance of object instantiated. In Figure 2.13, the student class is a concrete class.

2.13.1 OOPS – A Case Study in Object Modelling

We will carry out a detailed modelling exercise to fortify what we have learnt about relationships. We will study the college administration system as it pertains to college, departments, and students and relationships, as shown in Figure 2.14. There are two main classes: Person and Dept. These two are superclasses (base classes) and hold attributes common to all persons and departments such as the name of a person or department, college code, and all other relevant common data. Student and staff are subclasses derived from super person and is a special kind of person. It contains specialized attributes such as identification number, address, dept code and other relevant data. This relationship is inheritance and is shown with the symbol.

 

Relationships between college and students

 

Figure 2.14 Relationships between college and students

 

Aggregation is shown by a line with a hollow diamond pointing to the whole class. A department has some classes that have some students. Aggregation means “part of” relation and is shown as image.

Composition uses a solid diamond instead. In this type, object is a whole or integral part of the whole. Composition means “has a” relationship, i.e. the whole object contains a part and is shown in UML as image In Figure 2.14, the class has 0…60 students. The class is composed of students. Any method a class needs to manipulate students will be implemented in class, and these methods are available to any derived class(sub class) of a class.

You can observe that EngineeringDept is derived from Super Dept and is a specialized department. It will have additional information peculiar to an engineering department such as specialization, resource persons, and link to students belonging to the department. Look at the CourseOffering class. It implements the course offering relationship that exists between a student and an EngineeringDept. Also observe that CourseOffering is also a part of college which is a list of all engineering departments offering courses. A college needs some other objects to keep track of courses that have been offered. Further class called CourseOffering can have additional functionality such as tracking which course a student has been offered. The class CourseOffering can also track courses that are currently running and/or courses that have concluded. Make a note that we have defined a class called CourseOffering to manage the association relationship between EngineeringDept and student. The dashed line ------------ in UML indicates the relation just described. The above association shown in Figure 2.14 means that a student can be offered 1…6 courses by an EngineeringDept and these courses are tracked and monitored by CourseOffering class. The relationship between college and EngineeringDept is aggregation. We can say that EngineeringDept is part of college.

2.14 Summary

  1. Module: The basic building block in an OO language is a module. Modules can be defined as logical collections of classes and objects. A module is not a function or a procedure or an algorithm, but rather the classes and objects and messages passing between them.
  2. Object orientation is a technique of modelling a real-world system in software based on objects.
  3. OO programming is a collection of cooperating objects which are in turn instances of a class. The cooperating classes have a hierarchy of relationships defined by inheritance relationship.
  4. OOP language means: objects, classes, inheritance.
  5. Class: A class is a collection of a set of objects that share a common behaviour and attributes. Class definition describes attributes as well as functions that implement the behaviour.
  6. Mandatory profile: This defines minimum services, i.e. functionality to be provided by a class.
  7. Object is an entity that contains attributes and member functions and an identity. Objects of common attributes and functionality are grouped together in a class. An object's behaviour is decided by functionality defined by the class definition.
  8. The unique identification for an object is the memory address of that object.
  9. Encapsulation is the process of hiding all the internal details of an object from the outside world through access specifiers such as private and protected.
  10. Link connects data belonging to different objects to achieve some global functionality. A link is shown in UML by an arrow pointing to the object. A link is also an instance of an association.
  11. Class diagrams provide system overview. They show classes and their relationships with other classes. They are static diagrams. This means that they tell us about the interactions amongst classes, but they do not tell us the responses to these interactions.
  12. Hierarchy: An ordering of classes. The most common OO hierarchies are inheritance and aggregation.
  13. Association is a type of hierarchy that shows relationships between classes that are independent and are of equal standing. An association is a relationship between two or more classes.
  14. Multiplicity: An attribute that quantifies an association between objects.
  15. Whole/part of hierarchy: In this type of hierarchy one class is included in the other classes. This relationship is called has a relationship.
  16. Aggregation means “part of” relation. An aggregate object has other objects. Other objects are part of aggregate objects. Aggregation is shown by a line with a hollow diamond pointing to the whole class image.
  17. Composition: This is a special case of aggregation where whole cannot exist without parts. Composition uses a solid diamond image.
  18. Hierarchies with independent classes: The classes under this category have an is a relationship and are also referred to as generalization/specialization.
  19. Inheritance specifies is type of relation. A base class is known as superclass. A derived class is also known as a subclass. Inheritance is shown in UML with the symbol image.
  20. Derived subclass not only inherits the properties of the superclass, but it can also extend and modify its behaviours and attributes.
  21. Object diagram shows instances of class diagrams.
  22. Dynamic binding: When there are several subclasses implementing a function, it is critical that the correct function of the correct derived class be invoked. That is, at run-time, the correct function has to be invoked through a function call. The feature that ensures this correct run-time binding of function is called dynamic binding.
  23. Polymorphism means to ensure that objects of a base class behave correctly and as planned.
  24. Abstract class: This is a base class with no function implementation at all. However, the derived classes from this base class will override the base class function in which each of the overridden functions will have its own implementations. The interface feature of Java is an abstract class. In C++, a base class with pure virtual functions can be considered as an abstract class.
  25. An abstract class, since it has no implemented functions, cannot have an object instantiated.
  26. Concrete class is a class that can have an object or instance of object instantiated.

Exercise Questions

Objective Questions

  1. Which of the following statements are true regarding OO language?
    1. OO language comprises objects, classes and inheritance.
    2. Modules are logical collections of classes and objects.
    3. Modules are a collection of functions.
    4. Class definition describes attributes as well as functions.
    1. i
    2. i and ii
    3. i, ii and iv
    4. i, ii and iv
  2. Which of the following statements are true regarding objects and classes?
    1. Objects contain member functions and attributes.
    2. Objects contain member functions and attributes and an identity.
    3. The unique identification for an object is the memory address of the object.
    4. Mandatory profiles define maximum service provided by the class.
    1. i
    2. i and ii
    3. ii and iv
    4. i, ii and iv
  3. Which of the following statements are true regarding class diagrams?
    1. Class diagrams provide system overview.
    2. Class diagrams indicate the interactions amongst classes.
    3. Class diagrams indicate the responses to interactions.
    4. Class diagrams show classes and their relationships with other classes.
    1. i
    2. i and ii
    3. ii and iv
    4. i, ii and iv
  4. Which of the following statements are true regarding object methodology?
    1. A link connects data belonging to different objects.
    2. A link is shown in UML by an arrow to a class.
    3. A link is also an instance of an association.
    4. Association shows relationships between classes that are independent and are of equal standing.
    1. i, iii and iv
    2. i, ii and iii
    3. ii and iv
    4. i, ii and iv
  5. Which of the following statements are true regarding association?
    1. An association is a relationship between two or more classes.
    2. Multiplicity is an attribute that quantifies an association between objects.
    3. Whole/part of hierarchy means “is” type of hierarchy.
    4. Aggregation means “part of” relation.
    1. i, iii and iv
    2. i, ii and iii
    3. ii and iv
    4. i, ii and iv
  6. Which of the following statements are true regarding aggregation?
    1. Aggregation means “whole of” relation.
    2. Other objects are part of aggregate objects.
    3. Aggregation is shown by a line with a hollow diamond pointing to the whole class.
    4. Aggregation means “is” relation.
    1. i, iii and iv
    2. i, ii and iii
    3. ii and iii
    4. i, ii and iv
  7. Which of the following statements are true regarding composition?
    1. Composition means whole cannot exist without parts.
    2. Composition uses a solid diamond.
    3. Composition is shown by a line with a hollow diamond pointing to the whole class.
    4. Composition means “is” relation.
    1. i and ii
    2. i, ii and iii
    3. ii and iii
    4. i, ii and iv
  8. Which of the following statements are true regarding inheritance?
    1. Inheritance means “is” type of relationship.
    2. Inheritance means a hierarchy with an independent class.
    3. A base class is known asasuperclass.
    4. Inheritance is shown in UML with symbol image
    1. i and ii
    2. i, ii, iii and iv
    3. ii and iii
    4. i, ii and iv
  9. Which of the following statements are true regarding object-oriented methodology?
    1. Dynamic binding ensures correct run-time binding of classes.
    2. Dynamic binding ensures correct run-time binding of functions.
    3. Polymorphism ensures correct behaviour of base classes.
    4. Polymorphism ensures correct behaviour of derived classes.
    1. i and ii
    2. i, ii, iii and iv
    3. ii and iv
    4. i, ii and iv
  10. Which of the following statements are true regarding object-oriented methodology?
    1. An abstract class can have an object instantiated.
    2. Concrete class is a class that can have an object or instance of object instantiated.
    3. An abstract class is a base class with no function implementation at all.
    4. In C++, a base class with pure virtual functions can be considered as an abstract class.
    1. i and ii
    2. i, ii, iii and iv
    3. ii, iii and iv
    4. i, ii and iv

    Short-answer Questions

  11. Define object-oriented programming.
  12. Define class and object.
  13. What is garbage collection as it applies to object-oriented methodology?
  14. What is an abstract class?
  15. What is a concrete class?
  16. Explain aggregation.
  17. Explain composition.
  18. Explain multiplicity of an association with examples.
  19. Distinguish class diagrams and object diagrams.

    Long-answer Questions

  20. Explain object modelling.
  21. What are mandatory profile, meta model and meta data?
  22. Why are specifying constraints important?
  23. How are objects created in C++ and Java? State the properties of objects.
  24. What are links in object modelling? Give examples of link implementations in Java, C++, and UML.
  25. Explain the class diagram with an example and explain the various features involved.
  26. Explain the object diagram with an example and explain the various features involved.
  27. Explain class hierarchies with examples.
  28. Explain the polymorphism with an example and explain the various features involved.

    Assignment Questions

  29. Develop a class diagram for the inventory control department of an industry.
  30. Develop a class diagram catalogue display for a large departmental store wherein customers look into models available and place orders online using credit cards.
  31. Carry out object modelling for an automatic teller machine of a bank.
  32. Draw a class diagram for a fire alarm system. Assume that the central monitor monitors fire alarms and when fire incidents occur, informs the fire station.

Solutions to Objective Questions

  1. d
  2. c
  3. d
  4. a
  5. d
  6. c
  7. a
  8. b
  9. c
  10. c
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.79.147