Introduction to User Defined Data Types
Traditional programming languages provide a small set of standard data types such as integer, floating-point number, and string.
But these types do not fully represent real-world objects. For instance, an employee is not just an integer, a floating-point number,
or a string. An employee does have a name that is a string, a salary that is a floating-point number, and an ID that is an integer.
Classes combine primitive data types such as integer, floating-point number, and string into new programmer-defined data types such as employee. Specific instances of a class (for example, name: Joe Smith, salary: $36,500.75, ID: 834) are objects.
In this module, you will learn:
- How classes allow programmers to create new data types
- That classes have attributes that define the state of an object in the class
- That objects are specific instances of classes with particular values for the classes's attributes
- How to read and draw class diagrams
- How to read and draw object diagrams
(ADT) abstract data type
The abstract data type (ADT), the generalization of primitive data types such as integers and characters, is an
example of applying encapsulation. The programmer specifies the collection of operations on the data type and the data structures that are needed for data storage. Users of the ADT perform the operations without concerning themselves with the implementation.
The concept of a class is fundamental to the object-oriented paradigm. As we have discussed,
it is based on the notion of an abstract data type and one can trace its origins to the Simula programming language. This chapter also discussed some of the UML notation used for describing classes. In the next chapter we look at how classes interconnect to form
a system, and the use of UML to denote these relationships. The Java syntax and concepts that we have described in this chapter are quite similar to the ones in C++; so the reader should have little difficulty getting introduced to that
language. A fundamental difference between Java and C++ is in the availability of pointers in C++, which can be manipulated using pointer arithmetic in ways that add considerable flexibility and power to the language. However, pointer arithmetic and other features in the
language also make C++ more challenging to someone new to this concept.
Dangers of Multiple Inheritance
Some languages (such as C++) allow a class to extend more than one other class. This capability is known as "multiple inheritance."
The reason that Java's creators chose not to allow multiple inheritance is that it can become complicated.
In a nutshell, the problem is that if a class extended two other classes, and both superclasses had, say, a
doStuff() method, which version of
doStuff() would the subclass inherit?
This issue can lead to a scenario known as the Deadly Diamond of Death, because of the shape of the class diagram that can be created in a multiple inheritance design.
The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A.
If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory,
inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond.