Java Reference
In-Depth Information
public String getColor() {
return this.color;
}
 
public void setColor(String color) {
this.color = color;
}
 
public abstract double calculateArea();
}
 
public class Circle extends Shape {
private double radius;
private final double PI = 3.14159;
 
public Circle(String color, double radius) {
super(color);
this.setRadius(radius);
}
 
public double getRadius() {
return this.radius;
}
 
private void setRadius(double radius) {
this.radius = radius;
}
 
@Override
public double calculateArea() {
return PI * this.getRadius() * this.getRadius();
}
 
}
The following exercise will help you practice some of the main concepts associated with inheritance.
Inheritance, Superclasses, and Subclasses
try it out
In this exercise, you continue with the Bank Account application from an earlier exercise and adapt it
to apply the principles of inheritance.
1.
Start from the Bank Account application from an earlier exercise. You may want to copy the files
into a new package to keep both versions separate.
2.
Create two new classes called SavingsAccount and CheckingAccount , which should be subclasses
of the Account class.
3.
Create a constructor in the SavingsAccount class that uses the superclass constructor. Assume
when customers open a SavingsAccount , they receive a bonus of 10 dollars.
4.
Add a private instance variable to the CheckingAccount class that indicates how many signatories
are on the account. Add a getter and setter for it.
Search WWH ::




Custom Search