import javax.swing.*;
class JWindowUser1 {
public static void main(String [] args) {
JWindow window = new JWindow();
}
}
Don't see anything!
Take 2
import javax.swing.*;
class JWindowUser2 {
public static void main(String [] args) {
JWindow window = new JWindow();
window.setVisible(true);
}
}
Creates window of 0 height and width!
Third Attempt
import javax.swing.*;
class JWindowUser3 {
public static void main(String [] args) {
JWindow window = new JWindow();
window.setSize(300, 300);
window.setLocation(100, 100);
window.setVisible(true);
}
}
OK, but how can we avoid having to do this
BetterWindow bw = new BetterWindow();
BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(filename)));with
BetterBufferedReader br = new BetterBufferedReader(filename);
DynArray class that gives us the functionality of a resizeable array
Involves having one class contain an instance variable of another class.
class NewClass ... private ExistingClass ec; ... }
f(g(x)); f(g(x)+2); f(g(x), h(y))
f uses the result of invoking g
NewClass's functionality is partly composed of using the functionality
provided by ExistingClass
Revisting the BetterWindow Class
import javax.swing.*;
class ComposedJWindow {
ComposedJWindow() {
jwindow = new JWindow();
jwindow.setSize(300, 300);
jwindow.setLocation(100, 100);
jwindow.setVisible(true);
}
private JWindow jwindow;
}
class ComposedJWindowUser {
public static void main(String [] args) {
ComposedJWindow window = new ComposedJWindow();
}
}
Revisting the BetterBufferedReader Class
import java.io.*;
class {
ComposedBufferedReader(String filename) {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
}
private BufferedReader br;
}
class ComposedBufferedReaderUser {
public static void main(String [] args) {
ComposedBufferedReader cbr = new ComposedBufferedReader("file.txt");
}
}
Our Collection Project -- An Array List Class
/
class ArrList {
ArrList() {
da = new DynArr();
}
...
DynArr da;
}
Issues with Composition
move, setColor, etc methods of
JWindow?
readLine method of BufferedReader?
get, length, etc methods of
DynArr?
private
JWindows, not ComposedJWindow
BufferedReader not ComposedBufferReader
Inherited (extended) object
override its superclass' behavior
import javax.swing.*;
class BetterJWindow extends JWindow {
BetterJWindow() {
setSize(300, 300);
setLocation(100, 100);
setVisible(true);
}
}
class BetterJWindowUser {
public static void main(String [] args) throws Exception {
BetterJWindow window = new BetterJWindow();
window.setSize(i * 100, i * 100);
}
}
An Inherited BetterBufferedReader
There's a difference between this class and the previous BetterJWindow
JWindow
setSize, setLocation, etc)
import java.io.*;
class BetterBufferedReader extends BufferedReader {
BetterBufferedReader(String filename) {
super(new InputStreamReader(new FileInputStream(filename)));
}
}
class BetterBufferedReaderUser {
public static void main(String [] args) throws Exception {
BetterBufferedReader window = new BetterBufferedReader("file.txt");
}
}
Our Collection Project - An Array List Inherited From DynArr
class ArrList extends DynArr {
}