Java Reference
In-Depth Information
441
S YNTAX 10.1 Inheritance
class SubclassName extends SuperclassName
{
methods
instance fields
}
Example:
public class SavingsAccount extends BankAccount
{
public SavingsAccount(double rate)
{
interestRate = rate;
}
public void addInterest()
{
double interest = getBalance() *
interestRate / 100;
deposit(interest);
}
private double interestRate;
}
Purpose:
To define a new class that inherits from an existing class, and define the methods
and instance fields that are added in the new class
public class SavingsAccount extends BankAccount
{
public SavingsAccount(double rate)
{
interestRate = rate;
}
public void addInterest()
{
double interest = getBalance() * interestRate
/ 100;
deposit(interest);
Search WWH ::




Custom Search