An object is defined by its class; the class determines everything about an object. An object is an instance of its class.
For example, you may create an object called Bulldog from the class dog. The Dog class defines what it are to be a Dog object, and all the "dog-related" messages a Dog object can act upon. All object-oriented languages have some means, usually called a factory, to "manufacture" object instances from a class definition
The reasons for the object oriented approach in general are objected oriented approaches use modern software engineering technology. This kind of approach also encourages software re-usability. If done correctly object oriented approaches produces solutions which resemble the original problem. The software that is created can also be more easily modified, extended and maintained. Reasons for choosing the object oriented approach in regards to software creation are traceability will be improved, integration problems can be reduced, the conceptual integrity of the product and process are both improved and also the need for objectification and de objectification is minimised. Modern software creation consists of many different concepts so I will focus on four specific concepts which I feel are important. These concepts are information hiding, data abstraction, encapsulation and concurrency. By using the object oriented approach to creating software it makes it easier to address these concepts. Information hiding means that certain details of an item are not accessible. By using this method two things can be accomplished. The interactions between items are kept simple and to a minimum and the chance of system corruption is decreased. Objects are referred to as “black boxes”. The details of the implementation of an object are hidden to the users of an object and any interactions take place through a user friendly interface e.g. a bank customer knows that they can open an account and make withdraws and balance enquires however the customers are not likely to be concerned with how these operations take place. The key to object oriented thinking is information hiding. Objects hide more information than functions or blocks of data. Abstraction is an important process, this is where you concentrate on unimportant details and concentrate on the more important aspects of an item e.g. as you move through lower levels of abstraction you concentrate on how an item was created. The three most important types of abstraction that software engineers have to deal with are functional abstraction, data abstraction and process abstraction. In functional abstraction, the function performed becomes a high-level concept. While we know about the interface we don’t know a lot about how it is accomplished.
Functional abstraction hides implementation details from users so that they cannot alter an object.
Data abstraction is where both the data and implementation are hidden from the user. The person who is creating the software has the option to hide any implementation from the user and the data may only be accessed by a series of operations. This means the data or information is now abstracted, the information can only be accessed from a well designed interface which exhibits functional abstraction.
One of the differences between a conventional system and an object oriented system is how they handle concurrency. In a conventional system concurrency is taken care of by a master routine whereas in an object oriented system the responsibility is on the objects themselves.
Localisation is the process of putting things close together. Encapsulation is the process where things are physically put together so that they can be treated as one or as a unit.
During the 1940’s and 50’s programmers had to deal with each individual line of code which could be thousands of lines long, this made it very difficult and tedious for the programmers. However when sub routines were introduced the programmers were able to encapsulate lines of code together and treat the sub routines as individual units. This made a programmers life a lot easier and allowed them to deal with large complex systems.
Object oriented programmer introduced the use of classes which is a more powerful method of encapsulation than sub routines. This approach meant that a class could be seen as the blue print or DNA of an object.
Conclusion
I have mentioned what I feel are the reasons behind the popularity of the object oriented approach in general. This is meant to show why the object oriented approach is considered when creating software not to show that it is superior to any other method of creating software.
Question 2
New object classes may be created which make use of existing ones, through two approaches – aggregation and inheritance.
Aggregation
Aggregation is where objects are made up of other objects e.g. a car is made up of different parts like an engine, transmission and wheels. Aggregation hierarchies are used to describe this. There is no inheritance in an aggregation hierarchy. To find aggregation you have to break complicated objects into separate less complicated parts and build objects out of existing objects. Operations that happen within an object can be completed by objects within an object e.g. starting a car are taken care of by the engine. This process is known as delegation. Delegation is a form of sharing. Objects with common components can share the function of those components. There are three main forms of aggregation. The first is fixed aggregation which is where the number and type of objects are defined before it’s used. The next type of aggregation is variable aggregation which is where the types of object are predefined but the number of objects can change. The last type of aggregation is recursive aggregation which is where the class only contains objects of the same type. One of the main advantages of using aggregation to design classes is to create object oriented systems which are open. The aim is to create classes which can be re-used. High-level objects should be made from lower level objects and the application is the highest level.
Inheritance
Classes can be arranged in inheritance hierarchies. A class gets all of its attributes and operations from a parent class. This structure allows code and designs to be re-used. A special diagram is used to show this on the class diagram. For example:
Student
Course
Modules taken
Person
Name
Address
Lecturer
Modules taught
Suppose Person has already been identified as an object. We now want to add a new kind of person – a student. So we make use of the existing person definition and add an inheritance relationship. The students now have the same attributes as person and they also have new attributes of their own. Another form of inheritance is multiple
Inheritance. This is where an object can get attributes and operations from two or more objects. However there are various problems that are associated with this type of inheritance. This type of inheritance is generally more complex because of the amount of objects involved; inherited attributes and methods can often conflict with each other and may not be compatible. It is best to avoid multiple inheritances; you could avoid it by using delegation. To find inheritance you have see if two things are similar and see whether they share certain things e.g. employee and customer. Also if you want a new object to be created you have to see if there is an object that is similar to the one that you want to create e.g. web customer could be a special type of customer. There are two different types of inheritance which are known as single and multiple inheritances. Single inheritance happens when there is a single base class and multiple inheritances happen when there is more than one base class. An example of single inheritance are credit cards, each credit card is a little different but is primarily the same. An example of multiple inheritances is voicemail because voicemail has features of both mail and phone.
Single and multiple inheritances
Suppose you have defined a shape class to describe and operate on geometric shapes. Now suppose you want to define a circle class. Because you have existing code that operates on the shape class, you can use inheritance to create the circle class. You can redefine operations in the derived circle class that were originally defined in the shape base class. When you manipulate an object of the circle class, these redefined function implementations are used.
For example:
class shape
{
char* name;
int xpoint, ypoint;
public:
virtual void rotate(int);
virtual void draw();
void display() const;
};
class circle: public shape // derive class circle from
class shape
{
int xorigin, yorigin;
int radius;
public:
void rotate(int);
void draw();
void display() const;
};
In the example, class circle inherits the data members name , xpoint and ypoint, as well as the member functions display() , rotate(), and draw() from class shape. Because the member functions rotate() and draw() are declared in class shape with the
Keyword virtual, you can provide an alternative implementation for them in class circle. You can also provide an alternative implementation for the non virtual member function display() in class circle. When you manipulate an argument of type circle using a pointer to shape, and call a virtual member function, the member function defined in the derived class overrides the base-class member function. A similar call to a non virtual member function will call the member function defined in the base class. In addition to inheriting the members of class shape, class circle has declared its own data members, xorigin, yorigin, and radius. The key difference between virtual and nonvirtual member functions is that , when you treat the circle class as if it were a shape, the implementations of the virtual functions rotate() and draw() defined in class circle are used, rather than those originally defined in class shape. Because display() is a non virtual member function, the original implementation of display() defined in class shape is used.
Multiple inheritances allow you to create a derived class that inherits properties from more than one base class.
For example, in addition to the shape class, described above, you could also have a symbol class. Because a circle is both a shape and a symbol, you can use multiple inheritances to reflect this relationship. If the circle class is derived from both the shape and symbol classes, the circle class inherits properties from both classes.
class symbol
{
char* language;
char letter;
int number;
public:
virtual void write();
virtual void meaning();
};
class shape
{
char* name;
int xpoint, ypoint;
public:
virtual void rotate(int);
virtual void draw();
void display() const;
};
class circle: public symbol, public shape
{
int xorigin, yorigin;
int radius;
public:
void rotate(int);
void draw ();
void write();
void meaning();
void display() const;
};
In the above example, class circle inherits the members name, xpoint, ypoint, display(), rotate(), and draw() from class shape and also inherits the members language, letter, number , write(), and meaning() from class symbol
Conclusion
Inheritance and aggregation are special types of relationship. Inheritance gives objects their attributes and operations whereas aggregation doesn’t pass attributes and operations. Aggregated objects pass functions to parts within the object. Inheritance allows designs and implementation to be used more than once. Both Aggregation and inheritance describe trees or hierarchies. Aggregation tree describes a part of relationship which is also known as an “and” relationship. Inheritance tree describes a kind of relationship which is also known as an “or” relationship. The main difference between the two is Aggregation relates instances and inheritance relates classes.
Having said this there are things which are similar between aggregation and inheritance and when you want to create a new class it is not always clear which one that you should choose. Aggregation is an associative relationship so a new class is composed of a number of diverse objects. Inheritance is a classification relationship and hence a new class will inherit its attributes and methods from the base class. Inheritance is usually more difficult to implement than aggregation.