BankAccount account = new CheckingAccout(1000); account.deposit(200); //calls the deposit method of CheckingAccount
account = new BankAccount() account.deposit(200); //deposit method of BankAccount
account = new SavingsAccount() account.deposit(200); //deposit method of SavingsAccount
account = new Rectangle(); // error //Rectangle is not a BankAccount
Object anObject; BankAccount anAccount; SavingsAccount collegeFund;
collegeFund = new SavingsAccount(10); anAccount = collegeFund; anObject = collegeFund;

anAccount.deposit(1000); // OK
anAccount.addInterest(); // No
Often useful in processing different subclasses of a superclass
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
Why was the second parameter of the transfer method of type
BankAccount?
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
That's an error—it should have been of type
SavingsAccountObjectSavingsAccount and CheckingAccount
objectsBankAccountWhy can't we change the second parameter of the transfer method to the type Object?
CheckingAccount aChecking = anAccount; //compile error
CheckingAccount aChecking = (CheckingAccount)anAccount;
instanceof operator
if (anAccount instanceof CheckingAccount)
{
CheckingAccount aChecking = (CheckingAccount)anAccount;
. . .
}
Object.toString prints class name and the hash code of
the object:
BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString();
Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString();
toString in BankAccount:
public String toString()
{
return "BankAccount[balance=" + balance + "]";
}
getClass().getName()
public class BankAccount
{
public String toString()
{
return getClass().getName()
+ "[balance=" + balance + "]";
}
}
public class SavingsAccount
{
public String toString()
{
return super.toString()
+ "[interestRate=" + interestRate + "]";
}
}
== tests for references to the same object:
if (coin1 == coin2) . . .
equals method uses ==
if (coin1.equals(coin2)) . . .
public boolean equals(Object otherObject)
public class Coin
{
. . .
public boolean equals(Object otherObject)
{
...
}
}
public class Coin
{
. . .
public boolean equals(Object otherObject)
{
Coin other = (Coin) otherObject;
return name.equals(other.name)
&& value == other.value;
}
}
equals tests for same contents:
== still tests for the same identical objects
coin1 == coin2 is true public class CollectibleCoin extends Coin
{
. . .
public boolean equals(Object otherObject)
{
CollectibleCoin other = (CollectibleCoin) otherObject;
return getName().equals(other.getName()) && getValue == other.getValue()
&& year == other.getYear();
}
}
Coin c1 = new Coin("quarter", 0.25);
Coin c2 = new CollectibleCoin("quarter", 0.25, 1993);
What do c1.equals(c2) and c2.equals(c1) return?
truefalsetrue, the second one returns
falsetrue, the second one throws an
exceptionBut that is not logical.
To fix the problem, equals should test that objects are of the same class
public class Coin
{
public boolean equals(Object otherObject)
{
if (getClass() != otherObject.getClass()) return false;
...
}
}public class CollectableCoin
{
public boolean equals(Object otherObject)
{
if (!super.equals(otherObject)) return false;
//...cast and test new variables
}
}
coin1.equals(coin2) is false and coin2.equals(coin1) is falseSpecial topic 10.1
averageLifeSpan
eat()
public abstract returnType someMethod();
By convention, abstract class usually start withthe word abstract in Java
public abstract class AbstractAnimal
{ private int averageLifeSpan; public AbstractAnimal(int span) {averageLifeSpan = span; } public int getAverageLifeSpan() {return averageLifeSpan; } public abstract String eats(); }
public class Cat
{
public Cat(int span)
{
super(span);
}
public String eats()
{
return "mice";
}
}
Cat overrides the abstract eats method so it is a concrete classnew AbstractAnimal(); //illegal
Cat corky = new Cat(20); AbstractAnimal myAnimal; //legal myAnimal = corky; //legal
More about Java.