Interface Type Variables

This section describes how to use an interface type variable to store references to objects of classes that implemented this interface.

Interface type variables are used in Java following these basic rules:

Here is a tutorial example program that shows you how to use interface type variables:

/* InterfaceTypeVariable.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
class InterfaceTypeVariable {
   public static void main(String[] a) {
      java.io.PrintStream o = System.out;

      // assigning the reference of a LinkedList object
      LinkedList<String> bookList = new LinkedList<String>();

      // assigning the reference to an implemented interface 
      // LinkedList<E> implements Queue<E>
      Queue<String> bookQueue = bookList;

      // accessing interface methods
      bookQueue.add("Java Tutorials");
      bookQueue.add("PHP Tutorials");
      String next = bookQueue.remove();

      o.println("Next book in the queue = "+next);
   }
}

Compile and run the example, you will get:

Next book in the queue = Java Tutorials

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

Reference Data Types and Variables

 Reference Types Supported in Java

 Creating Class Type Objects

 Class Type Variables Storing References

Interface Type Variables

 Class and Interface Hierarchy

 Supertype and Subtype

 Explicit and Implicit Type Casting

 Type Casting Example Program

 Type Casting Compile and Runtime Error

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version