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:

 * Copyright (c) 2012, All Rights Reserved.
import java.util.*;
class InterfaceTypeVariable {
   public static void main(String[] a) { 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

 JDK - Java Development Kit

 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

 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

 Outdated Tutorials


 Full Version in PDF/EPUB