Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Creating a Data Type

Consider the types of data you use in your code. Whether it's a complicated class-based type or
a simple primitive type (such as int), they are defined by the characteristics they can have and
how they can be used. For example, you can add an int to another int by using the '+' function.
So, when you think about making your own type, you might consider creating a class and
defining the type's characteristics and behaviors using class members (for example, methods or
variables).

Let's say you're creating an application that monitors the usage of a server. You'd probably
want to keep track of the precise times users logged in and out. So, it would be useful to have a
data type representing time. In Java, you should see this as a prime candidate for a class, maybe
called Time. Time would have variables to store the day, hour, minute, and second. For
behaviors, you'd make methods that allow you to access those variables, methods to get the
time from somewhere, and maybe a method to convert the time from one format to another.
The code to create such a class would look something like this:

1. import java.time.*;
2. public class Time {
3.  private int hour;
4.  private int minute;
5.  private int second;
6.  private LocalDateTime time;
7.  public Time(LocalDateTime time) {
8.   this.time = time;
9.  }
10.  public int getHour() {
11.   return time.getHour();
12.  }
13.  public int getMinute() {
14.   return time.getMinute();
15.  }
16.  public int getSecond() {
17.   return time.getSecond();
18.  }
19.  public static void main(String[] args) {
20.   Time time_logged = new Time(LocalDateTime.now());
21.   int this_hour = time_logged.getHour();
22.   System.out.println(this_hour);
23.  }
24. }

For this example we're using the java.time library, which makes the LocalDateTime.now()
method available. If you compile and execute this code, you should see the hour printed out.
Abstraction

But what if you're going to be using this application in different contexts where there are
different ways of getting the time? For example, in one department it's been mandated that all
times must be synchronized to a company time server. In another department, there's no such
requirement and, in fact, your application doesn't even have access to that time server. In the
latter case, you'd likely have to get the time from a public server, perhaps by using the java.time
library. This would probably have a very different API and require a different kind of error
handling.

You still want your data type to behave the same, however. Calls to getHour() should behave
the same regardless of what's being done behind the scenes to accomplish the method's task.
Here's where you'd want to seriously consider making your data type abstract. An abstract data
type defines only the variables and methods, including the parameters and return types of
those methods, without determining how they are implemented. That's why it's abstract,
you're determining what the data type can do, but not how it does it.

In Java, this is usually accomplished using interfaces. The dashboard of a car is an interface. You
know you can see the current speed and do things like turn on the windshield wipers, but you
don't know how these things are done behind the panel. And, in fact, two cars might use the
same model speedometer but have different mechanisms for actually causing the speed to be
displayed. Java interfaces are created similarly to classes, but with the keyword interface.

You might also like