<< Chapter < Page Chapter >> Page >

Creating new types

One of the ways that individual programmers can extend the language is to create new types. When creating a new type, the programmer must define the set of values that can be stored in an instance of the type as well as the operations that can be performed on instances of the type.

No magic involved

While this might initially seem like magic, once you get to the heart of the matter, it is really pretty straightforward. New types are created by combining instances of primitive types along with instances ofother user-defined types. In other words, the process begins with the primitive types explained earlier and builds upward from there.

An example

For example, a String type, which can be used to represent a person's last name, is just a grouping of a bunch of instances of the primitive char or character type.

A user-defined Person type, which could be used to represent both a person's first name and their last name, might simply be a grouping of two instances of the user-defined String type. (The String type is part of the Java standard library. However, the standard library doesn't have a typenamed Person . If you need that type, you will have to define it yourself.)

Differences

The biggest conceptual difference between the String type and the Person type is that the String type is contained in thestandard Java library while the Person type isn't in that library. However, you could put it in a library of your own design if you chooseto do so.

Removing types

You could easily remove the String type from your copy of the standard Java library if you choose to do so, although that would probably be abad idea. However, you cannot remove the primitive double type from the core language without making major modifications to the language.

The company telephone book

A programmer responsible for producing the company telephone book might create a new type that can be used to store the first and last names along with the telephone number of an individual. That programmer might choose to give the new type the name Employee .

The programmer could create an instance of the Employee type to represent each employee in the company, populating each such instance withthe name and telephone number for an individual employee. (At this point, let me sneak a little jargon in and tell you that we will be referring to such instances as objects.)

A comparison operation

The programmer might define one of the allowable operations for the new Employee type to be a comparison between two objects of the new type to determine which is greater in an alphabetical sorting sense. This operation could be used to sort the set of objects representing all of the employees into alphabetical order. The set of sorted objects could then be used to print a new telephone book.

A name-change operation

Another allowable operation that the programmer might define would be the ability to change the name stored in an object representing a particular employee.For example when Suzie Smith marries Tom Jones, she might elect to thereafter be known as

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask