Abstract Data Types in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java: Enumerated Type

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:00 Creating a Data Type
  • 1:19 Abstraction
  • 4:04 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed Audio mode

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Benjamin Blanchard

Ben has taught ESL and web programming and has a M.S. in education.

Do you want to define your own data types? Learn about why abstract data types are useful in programming, and how to create very basic ones in Java using a Java interface in this lesson.

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:


import java.time.*;
public class Time {
 private int hour;
 private int minute;
 private int second;
 private LocalDateTime time;
 public Time(LocalDateTime time) {
  this.time = time;
 }
 public int getHour() {
  return time.getHour();
 }
 public int getMinute() {
  return time.getMinute();
 }
 public int getSecond() {
  return time.getSecond();
 }
 public static void main(String[] args) {
  Time time_logged = new Time(LocalDateTime.now());
  int this_hour = time_logged.getHour();
  System.out.println(this_hour);
 }
}


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. Here's how the code would look for the Time interface:


interface Time {
 public int getHour();
 public int getMinute();
 public int getSecond();
}


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support