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) 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

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 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

 Enum Types and Enum Constants

 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

 Java Modules - Java Package Aggregation

 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

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB