Composition and Inheritance (of Classes)

Enhancing Class behavior

A Better Window Class

First Try

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 Would like to be able to write
	BetterWindow bw = new BetterWindow();

A Better Buffered Reader

Would like to replace the long cascaded constructor call:
BufferedReader br = 
	new BufferedReader(
		new InputStreamReader(
			new FileInputStream(filename)));
with
BetterBufferedReader br = new BetterBufferedReader(filename);

Our Collection Project

Class Composition

Involves having one class contain an instance variable of another class.

class NewClass
	...
	private ExistingClass ec;
	...
}

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

Class Inheritance

Inherited (extended) object

An Inherited BetterJWindow

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

BufferedReader
  • does not have a default constructor - must specify an InputStreamReader
  • our enhancement involves creating a BufferedReader object with the specified filename
    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 {
    }