01.11.2017 Views

book

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The Abstract Account example shows the code for an abstract Account class.<br />

Abstract Account<br />

1 abstract class Account {<br />

2<br />

3 int accountNo;<br />

4 String accountName;<br />

5 double balance;<br />

6<br />

7 public Account(int no, String name, double bal){<br />

8 accountNo = no;<br />

9 accountName = name;<br />

10 balance = bal;<br />

11 }<br />

12<br />

13 public abstract void deposit(double amount);<br />

14<br />

15 }<br />

Abstract Account<br />

accountNo, accountName, and balance are variables that are inherited by any<br />

subclass of Account. Note the abstract class does have a constructor, but it cannot be<br />

directly instantiated. We would need to instantiate a subclass, and the subclass constructor<br />

would, in turn, invoke the abstract class constructor using the super keyword. deposit<br />

is an example of an abstract method. We assume that all subclasses will have a deposit<br />

method with one double argument, amount. We assume that not all subclasses will have<br />

a withdraw method; LongTermDeposit, for example, may not allow any withdrawals.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!