From charlesreid1

(Created page with "Can use Java Generics to define classes that wrap generic types. Can also define methods that take generic types of arguments, slightly different syntax. Let's look at each....")
 
Line 6: Line 6:


==Generic Classes==
==Generic Classes==
===Applications===


One common application of generic classes is when building data structures like linked lists or trees. These objects use node structures to store data under the hood, and these node structures often take the form of objects storing an element of data of arbitrary type, along with pointers.
One common application of generic classes is when building data structures like linked lists or trees. These objects use node structures to store data under the hood, and these node structures often take the form of objects storing an element of data of arbitrary type, along with pointers.
If you use the built-in container types, you also see generic class types. It is the concept that the container can deal with objects of any type, because the container just needs to hold them, or set and get container elements, but never interact with the elements themselves. That's the reason behind the <code>LinkedList<String></code> syntax.
The important part here is the diamond notation <code><E></code> - the letter that comes after the diamond is arbitrary but it is the keyword indicating the generic type of object that this Node class wraps. Later in the class, the object is referred to using a pointer of generic type E.
===Example===


This is an example of a Node class found in a linked binary tree class (see [[Binary Trees/LinkedBinTree]] for actual implementation):
This is an example of a Node class found in a linked binary tree class (see [[Binary Trees/LinkedBinTree]] for actual implementation):
Line 42: Line 50:
}
}
</pre>
</pre>
The important part here is the diamond notation <code><E></code> - the letter that comes after the diamond is arbitrary but it is the keyword indicating the generic type of object that this Node class wraps. Later in the class, the object is referred to using a pointer of generic type E.

Revision as of 18:57, 18 June 2017

Can use Java Generics to define classes that wrap generic types.

Can also define methods that take generic types of arguments, slightly different syntax.

Let's look at each.

Generic Classes

Applications

One common application of generic classes is when building data structures like linked lists or trees. These objects use node structures to store data under the hood, and these node structures often take the form of objects storing an element of data of arbitrary type, along with pointers.

If you use the built-in container types, you also see generic class types. It is the concept that the container can deal with objects of any type, because the container just needs to hold them, or set and get container elements, but never interact with the elements themselves. That's the reason behind the LinkedList<String> syntax.

The important part here is the diamond notation <E> - the letter that comes after the diamond is arbitrary but it is the keyword indicating the generic type of object that this Node class wraps. Later in the class, the object is referred to using a pointer of generic type E.

Example

This is an example of a Node class found in a linked binary tree class (see Binary Trees/LinkedBinTree for actual implementation):

git.charlesreid1.com link: https://charlesreid1.com:3000/cs/java/src/master/trees/oop-correct-tree/LinkedBinTree.java

public class LinkedBinaryTree<E> {

	protected static class Node<E> {
		// No need to expose things.
		private E element;
		private Node<E> parent; // pointer to parent node
		private Node<E> left;  // pointer to left child
		private Node<E> right; // pointer to right child
		// constructor with element and neighbors
		public Node(E e, Node<E> above, Node<E> lefty, Node<E> righty) { 
			element = e;
			parent = above;
			left = lefty;
			right = righty;
		}
		// get methods - one for each attribute
		public E getElement() { return element; }
		public Node<E> getParent() { return parent; }
		public Node<E> getLeft() { return left; }
		public Node<E> getRight() { return right; }
		// update methods
		public void setElement(E e) { element = e; }
		public void setParent(Node<E> up) { parent = up; }
		public void setLeft(Node<E> lefty) { left = lefty; }
		public void setRight(Node<E> righty) { right = righty; }

	}