Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
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
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
Class Type Variables Storing References
Explicit and Implicit Type Casting
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
Assert Statements and -ea" Option