CSCE 314 Lecture 27
« previous | Wednesday, November 2, 2011 | next »
Java History Lessen
- Originally known as "Oak" (first prototype at Sun in 1992 by the Green Team)
- Client-side execution; development of virtual machine (FAIL)
- meant for distributed, hetergeneous network of consumer electronic devices
- This was developed independently of the WWW
- Über Commecial FAILURE!
- Officially released on May 23, 1995 and incorporated into Netscape Navigator
Goals
- Platform independence (write once, run anywhere)
- built-in support for computer networks
- execute code from remate sources
- Object-Oriented
Hello, Stack!
public class Stack {
protected class Node {
int data;
Node next;
Node (int v, Node n) { data = q; next = n; }
}
public Stack() { stk = null; }
public boolean empty() { return stk == null; }
public int pop() {
int result = stk.data;
stk = stk.next;
return result;
}
public int top() { return stk.data; }
public void push(int i) { stk = new Node(i, stk); }
private Node stk;
}
Yay for garbage collection and references to everything (no pointers)!
{{note|Garbage collection only deals with memory; don't forget to close file handles
Access Control
Modifiers to who can view what:
- public (entire world has access)
- protected (inherited classes have access)
- (no modifier; package has access)
- private (only class can access)
Inheritance
Because objects represent ADTs, they encapsulate a state with internal variables. This begs notion of an interface to an object's internal state:
An Interface means that an object can act like an interface (requires implementation of public methods), whereas inheritance means that a class is its parent class (code reuse allowed here).
Interface Example
public interface Measurable {
double getMeasure();
}
class Coin implements Measurable {
double getMeasure() { return getValue(); }
}
class File implements Measurable {
double getMeasure() { return getSize(); }
}
Inheritance (Subclassing) Example
class SavingsAccount extends BankAccount
{
// new and overridden methods
// new instance variables
// all protected+ methods of BankAccount are inherited
}
With sublcassing, programmer only has to encode the differences (delta) between two class types.
All Java classes are extensions of Object class. C++ has no "root" class.
Generally useful to modify hierarchy if developing gradually or to design it all at once before implementing it (ensuring that it is extensible for future growth)
Aggregation
"weak inheritance"
Keep an instance object as a member of a class; delegate behavior to that instance:
public class Stack {
private LinkedList stack = new LinkedList();
}