2009:January:16(Fri)
r is defined as follows:
JCheckBoxMenuItem x = new JCheckBoxMenuItem("Enable/disable something");
if( )"s,
true or false.
Check Java documentation for the inheritance patterns.
if ( x instanceof JCheckBoxMenuItem ) . . .
if ( x instanceof MenuElement ) . . .
if ( x instanceof JTextField ) . . .
if ( x instanceof AbstractButton ) . . .
if ( x instanceof JComponent ) . . .
if ( x instanceof JMenu ) . . .
if ( x instanceof Container ) . . .
if ( x instanceof Object ) . . .
class base {
private int m1() { return 30; }
public int m2() { return 40; }
public int m3() { return m1() + m2(); }
}
class derived extends base {
public int m2() { return 50; }
}
public class bd_demo {
public static void main(String[] args) {
base b = new base();
derived d = new derived();
System.out.println("b.m1(): " + b.m1());
System.out.println("d.m1(): " + d.m1());
System.out.println("b.m2(): " + b.m2());
System.out.println("d.m2(): " + d.m2());
System.out.println("b.m3(): " + b.m3());
System.out.println("d.m3(): " + d.m3());
b = d;
System.out.println("after b=d, b.m1(): " + b.m1());
System.out.println("after b=d, b.m2(): " + b.m2());
System.out.println("after b=d, b.m3(): " + b.m3());
}
}
> javac bd_demo.java
> java bd_demo
b.m1(): 30
d.m1(): 30
b.m2(): 40
d.m2(): 50
b.m3(): 70
d.m3(): 80
after b=d, b.m1(): 30
after b=d, b.m2(): 50
after b=d, b.m3(): 80
>
The following terms describe relevant concepts:
- dynamic dispatch
- inheritance
- composition
- information hiding
- encapsulation
- polymorphism
- overriding
- static binding
derived object d's
possession of a method m1()?
class derived
to `extend'
class base
yet have its own version of method m2()?
derived object (d)
in base variable b?
b.m2()
sometimes does the m2() of class base
and
other times does the m2() of class derived?
m2()
inside m3()
sometimes does the m2() of class base
and
other times does the m2() of class derived?
protected (nor nothing) for
field(s) i.e. instance-variable(s) of
the basic
account-class).
Each of these particular
account classes should provide
a method that is invoked to calculate the interest;
for the money-market account,
this method should
accept the current month's interest-rate as an argument.
The CD account should
override the basic account's withdrawal method,
doing nothing to the balance
but outputting an error-message
via System.out.println().
main() does the following:
ArrayList<>
of
two basic bank-accounts
and
stores those two accounts in it.
System.out.println()
to output
the balance of each element of the
array or ArrayList<>
of bank-accounts.
ArrayList<> —
not from the original two variables
above —
back in
a new money-market account variable
(different from the original one above)
and a new CD account variable
(different from the original one above).
Remember to cast.