How to Use Objects to Execute Methods Belonging to Abstract Data Types in Java

In computer science, an Abstract Data Type is a data structure that can perform any of a given set of operations on the data it stores. Those operations (and the conditions under which each of them can be called) are completely specified for a given ADT, including parameter lists and return values. The ADT does not specify anything, however, about the underlying implementation -- thus allowing that implementation to change without having to propagate any change to code that uses the ADT. In object-oriented programming languages like Java, an ADT is equivalent to the public interface of a class.

Instructions

    • 1

      Import the packages required by the class at the beginning of your Java code, as in the following example:

      import java.util.GregorianCalendar;

      The class in question may be one of the pre-defined Java library classes (as in the example), or defined by your own code.

    • 2

      Create an instance of the ADT by creating an object that instantiates the class, as in the following sample code:

      GregorianCalendar myDate = new GregorianCalendar(2011,Calendar.JULY,5);

      The example creates an object and initializes it by calling the GregorianCalendar constructor that takes a month, day and year as input, and returns a GregorianCalendar object with the specified date. This particular date is 7/5/2011. The example also stores a reference to the new object in variable "myDate".

    • 3

      Execute a method belonging to the ADT by calling the corresponding method on the object created in Step 2, as in the following sample code:

      long millisecondsElapsed;

      millisecondsElapsed = myDate.getTimeInMillis();

      The example executes method "getTimeInMillis()" on object "myDate". The method returns, by convention, the number of milliseconds elapsed between the date represented by the ADT and the beginning of January 1, 1970.

Related Searches:

References

Resources

Comments

Related Ads

Featured