OOPortal OOPortal

User Defined Types  «Prev  Next»
Lesson 3 Classes as data types
Objective Describe how Object-Oriented Languages allow Programmers to define New Data Types as needed.

Describe how Object-Oriented Languages allow Programmers to define New Data Types

Traditional computer programming languages have a finite set of data types, typically several variations of integers, a couple of different kinds of floating-point numbers, a string and/or character type, and maybe a boolean type.
These types are fine as long as that's the sort of data you are working with, but what do you do when you need a more complicated type, such as an employee type?
Object-oriented languages allow programmers to define new data types as needed. These types are called classes. Initially, classes are built up out of the primitive types of the language.
For example, a Point class might be composed of two primitive floating-point numbers that represent the coordinates of the point on a plane, possibly named x and y. This Point class can be represented in a reasonably language-independent fashion using a class diagram, as shown here:
1 2

Class Diagram Elements
This diagram indicates that this Point class has two attributes, also known as fields or member variables, called x and y, and that each of these is a floating-point number.
The three terms, attribute, field, and variable, are interchangeable, though different versions are preferred in different languages. For this course, will stick to the most generic term, attribute.

UML diagramming

The diagram style we'll be using here is taken from the Unified Modeling Language, UML for short. This is one of the most common diagramming styles used.
The above diagram can be implemented in any object-oriented language such as Java or C++.
In fact, a good CASE (Computer-Aided Software Engineering) tool can convert diagrams into source code in various languages and vice versa.