Abstract Classes and Methods

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon)

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Note: All of the methods in an interface (see the Interfaces section) are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).

In our bank account example, we can think of a savings account, checking (or current) account, and long-term deposit account all being physical examples of subclasses of a generalized account class. The account class is generalized, or abstract, in the sense that it makes no sense to have a corresponding instantiated object: whenever a physical account is opened, it must be a savings, checking, or long-term deposit. Nevertheless, we wish to have an account class that will have defined variables and methods common to all subclasses. Such a class is defined using the abstract keyword in Java. An abstract class cannot be instantiated. The abstract class can define variables and methods that subclasses can use or,if required, can override.

An abstract class can optionally consist of abstract methods. An abstract method consists of the method declaration only; the method body is omitted. For any subclass of the abstract class, a method must be declared with the same number and types of arguments (or signature) as the abstract method, or the subclass will fail to compile. This is of practical use in large software engineering projects where we want all subclasses of an abstract class to use the same method signature.
The Abstract Account example shows the code for an abstract Account class.

Abstract Account
abstract class Account
{
int accountNo;
string accountName;
double balance;
public Account(int no, String name, double bal)
{
accountNo = no;
accountName = name;
balance = bal;
}

public abstract void deposit(double amount);

}


accountNo, accountName, and balance are variables that are inherited by any subclass of Account. Note the abstract class does have a constructor, but it cannot be directly instantiated. We would need to instantiate a subclass, and the subclass constructor would, in turn, invoke the abstract class constructor using the super keyword. Deposit is an example of an abstract method. We assume that all subclasses will have a deposit method with one double argument, amount. We assume that not all subclasses will have a withdraw method; LongTermDeposit, for example, may not allow any withdrawals.

CheckingAccount
class CheckingAccount extends Account
{
double minBalance;

public CheckingAccount(int no, String name, double balance)
{
super(no, name, balance);
minBalance = 100;
}

public void deposit(double amount)
{
balance = balance + amount;
}

public double withdraw(double amount)
{
if (balance - amount < 0)
{
System.out.println(''Insufficient Funds");
}
else
{
balance = balance - amount;
}
return balance;
}
}



The point to note is that CheckingAccount must contain a public void deposit method with a single double argument since deposit has been defined as an abstract method in the parent abstract Account class. If the deposit method is not present, the CheckingAccount class will not compile.

No comments:

Post a Comment