Week 5: Inheritance and Interfaces

 

Quick Review

 

Class relationships

 

Inheritance

Examples:

// Superclass:
class Account
{
	private String name;
	private double balance;

	public String getName ()
	{
		return name;
	}

	public double getAvailable ()
	{
		// Minimum balance is 50.
		return balance - 50;
	}
}

// Subclass:
class CheckingAccount extends Account
{
	// Inherits all data and methods from Account.

	private int lastCheckNumber;

	public void writeCheck (double amount)
	{
		balance -= amount;
		++lastCheckNumber;
	}

	// Override the superclass.
	public double getAvailable ()
	{
		// Minimum balance is 5.
		return balance - 5;
	}
}

 

More on inheritance

Examples:

class Account
{
	private String name;
	private double balance;

	// Constructor:	
	public Account (String customerName)
	{
		name = customerName;
		balance = 0;
	}

	public double getInterestRate ()
	{
		// Most accounts get 5%.
		return 0.05;
	}

}

class CheckingAccount extends Account
{
	private int lastCheckNumber;

	// Constructor:	
	public CheckingAccount (String customerName,
					int initialCheckNumber)
	{
		// Call superclass's constructor.
		super (customerName);

		lastCheckNumber = initialCheckNumber;
	}

	public double getInterestRate ()
	{
		// Checking accounts get half the normal rate.
		// Call superclass's method to get the normal rate.
		double normalRate = super.getInterestRate ();
		return normalRate / 2;
	}

}

 

Inheritance Hierarchy

We can have the following parent/child class relationship

Employee <-- Manager <-- Executive

Account <-- CheckingAccount <-- NOWAccount

Hint: Only use inheritance when any object of the subclass must be useable in place of the superclass object

 

 

Can We Do This

// We have Employee <- Manager
Employee[] staff = new Employee[3];
Manager boss = new Manager("Richard Sinn", 100000, myDay);
staff[0] = boss;	// Can we do this
...
boss = staff[i]		// Is this right, which class will get more fields

 

Polymorhism

Examples:

// Initialize an array of accounts.  Some may actually be subclasses
// of account.
Account[] accountList = new Account[3];
accountList[0] = new Account ("Bob");
accountList[1] = new CheckingAccount ("John");
accountList[2] = new Account ("Betty");

// Iterate through the list generically.
double totalInterestRate = 0;

for (int i = 0; i < accountlist.length; ++i)
     totalInterestRate += accountList[i].getInterestRate (); 

double averageinterestrate=totalInterestRate / accountlist.length;

Casting

staff is an Employee array
staff[0] = new Manager("Richard Sinn");
staff[1] = new Employee("Joe IBMer");
Manager boss = (Manager) staff[0]; 	// Corrent ?
Manager boss = (Manager) staff[1];	// Error ?

// Can use the following code to check:

if (staff[i] instanceof Manager)
{
boss = (Manager) staff[1];
...
}

Casting can only be used in inheritance hierarchy

Use instanceof to check a hierarchy before casting from a parent to a child class

This is kind of complex ... why use it ...

 

Abstract classes

Examples:

abstract class Account
{
	// ... other definitions

	// Subclasses must define the format of the monthly
	// statement.  No implementation is provided.
	public abstract void printMonthlyStatement ();
}

class CheckingAccount extends Account
{
	// ... other definitions

	// Here we provide an implementation.
	public void printMonthlyStatement ()
	{
		// ... implementation
	}
}

 

Protected access

 

java.lang.Object - Cosmic Superclass

 

Java Control Visibility - Quick Review

  1. Visible to the class only (private)
  2. Visible to the world (public)
  3. Visible to the package and all subclasses (protected)
  4. Visible to the package (the default - no modifier needed)

 

Interfaces

Examples:

interface NumberGenerator
{
 	public int nextNumber ();
	public int prevNumber ();
}

class OddNumberGenerator
implements NumberGenerator
{
	int currentNumber = -1;

	public int nextNumber ()
	{
		currentNumber += 2;
		return currentNumber;
	}

	public int prevNumber ()
	{
		currentNumber -= 2;
		return currentNumber;
	}
}

class RandomNumberGenerator
implements NumberGenerator
{
	public int nextNumber ()
	{
		// ... generate new random number
	}

	public int prevNumber ()
	{
		// ... return previous random number
	}
}

More on interfaces

 

Lab

Part I

  1. Create a subclass of Hashtable called MyHashtable that overrides the put() method that enforces a limit to how many elements the Hashtable may contain. Add a constructor takes a parameter to specify this maximum and a getMaximum() method that returns the maximum.
  2. Create a class called Week5 that contains the main() method for an application. This main() method should create a MyHashtable, add several items (a few more than the maximum). It should then print out the some of the entries to verify that it only contains the maximum number of elements.

Part II

  1. Create a interface called Shape that defines the following methods:
  2. Create two implementations of the Shape interface called Rectangle and Circle.
  3. In Week5:

Copyright 1996-2001 OpenLoop Computing. All rights reserved.