Abstract Class

Download Report

Transcript Abstract Class

More Class Features
Java API
More Class Features








Inheritance
Access control modifiers
Static modifier
Final modifier
Abstract classes
Interfaces
Packages
Example Program
Inheritance


Reusability—one of the goals of OOP.
Suppose that you have developed, tested, and
used Account class.




You find that a SavingsAccount class and
CheckingAccount class would be useful.
Note that SavingsAccount and CheckingAccount are
more specialized cases of Account class.
We can create the two specialized classes by
extending the Account class.
This way, the two classes will inherit all the
instance variables and methods of the Account
class. New variables and methods can be added.
Terminology



subclass—e.g., SavingsAccount class and
CheckingAccount , which are extended
from Account, a superclass.
superclass—or parent class. Account is
the parent of SavingsAccount and
CheckingAccount
Inheritance—when a subclasse is
extended from a superclass, the former
inherits all variables and methods of the
latter.
Inheritance Hierarchy
Account
accountID
ownerName
balance
withdraw()
deposit()
showAccount()
SavingsAccount
CheckingAccount
interestRate
minimumBalance
addInterest()
getMinimumBalance()
Deriving a Subclass of Account

To derive SavingsAccount (subclass) from
Account (superclass):
class CheckingAccount extends Account {
// By inheritance all variables & methods of
// Account are available here.
double minimumBalance;
checkingAccount(String id, String owner,
double bal, double minBal){…}
double getMinimumBalance(){
return minimumBalance;
}
}
Constructor for Subclass
checkingAccount(String id, String owner,
double bal, double minBal){
super(id, owner, bal);
minimumBalance = minBal;
}
 Note super(), which is a call to the constructor of
the superclass. It is always a good idea to call
the superclass’ constructor in the subclass’
constructor.
Exercise

Specify a class named Employee
Draw a class diagram (attributes & methods)
 Write its Java code.


Develop subclasses named
HourlyEmployee and SalariedEmployee.

Write their Java code.
Access Control Modifier




To control accessibility of classes, methods, and
variables.
public

e.g., public void withdraw(double amt){…

Available to all classes
private

e.g., private String accountID;

Available only within the class
protected



e.g., protected withdraw(double amt){…
Available to subclasses only
default

Available to all classes in the same package
Static Modifier


Modifier static before a variable of method
indicates that it is a class variable or method.
A static method is needed to modify a static
variable from outside the class.
class Account{
String acctID;
// instance variables
String ownerName;
double balance;
private static int numberOfAccounts;
// class variable
. . .
public static void incrementAccount(…) // class method
. . .
}
Math Class (java api)
class Math {
public static
public static
. . .
public static
public static
. . .
}
final double E = 2.71828;
final double PI = 3.14149;
final double sin(double a){…};
final double cos(double a){…};
Math Class (cont.)
import java.util.Math;
class UseMath {
public static void main(Sting[] args) {
double r = 24.0;
double area = Math.PI * r * r;
System.out.println(“Area of circle of “ +
“radius “ + r “: “ + area)
}
}
Final Modifier

Modifier final means that no change can
be made.

final before a variable – same as constant.
E.g.,
final double PI = 3.1415926536;
 final int SECS_IN_MIN = 60;


final before a method – method cannot be
modified (overridden) in a subclass
 final before a class – class cannot be
extended (no subclasses possible)
Abstract classes

Consider classes like Sedan, Truck, Motorcycle.





Common attributes:
 make, engineSize, numberOfWheels,
Common methods:
 start(), accelerate(), stop(), turn()
Particular attributes:
 doors for Sedan, axles for Truck, etc.
To organize related classes, create a Vehicle class
with common attributes & methods. Then, create
subclasses with particular attributes and methods
Details of methods, like turn(), cannot be coded
until we know what type of vehicle we have.
Abstract Class (cont.)



Abstract class has one or more abstract
methods.
An abstract class cannot be instantiated.
(It cannot be used directly—it must first be
extended with a subclass)
Every abstract class must be overridden in
a subclass
Abstract Class (cont.)
Vehicle
make
engineSize
numberOfWheels
start()
stop()
turn()
Sedan
Truck
doors
axles
start()
stop()
turn()
start()
stop()
turn()
Motorcycle
start()
stop()
turn()
Abstract Class (cont.)
public abstract class Vehicle {
private String make;
private int engineSize;
private int numberOfWheels;
public Vehicle(String make, int size, int wheels){
. . .
}
public abstract start();
public abstract stop();
public abstract turn();
}
Interfaces



Interface is like an abstract class. I.e.,
all abstract methods must be implemented in a
derived module.
However, an interface does not contain any
instance variable or class variables—only
abstract methods.
Why interface? Java does not allow multiple
inheritance—i.e., more than one superclass for
any derived class. Interface takes care of the
function of multiple-inheritance.
Interface (cont.)
Horse
color
age
whinny()
trot()
Flying (interface)
fly() (abstract)
FlyingHorse
name
Fly()
display()
class FlyingHorse extends Horse
implements Flying {
. . .
}
Packages





Package—a structure to organize several
classes, constants, interfaces, etc., that are
related to each other
For example, Java standard library has a
package java.awt which contains classes Color,
Canvas, BorderLayout, etc.
Package javax.swing contains classes JButton,
JCheckbox, Jlabel, etc.
Packages must be imported to make its contents
visible.
Check Java API for more examples
Example Program




Develop class named Pet, with attributes like
name and sound. Include a couple of
constructors, as well as methods getName() and
getSound() which return name and sound,
respectively.
Derive a subclass named Dog, adding the breed
attribute. Add a constructor and the getBreed().
method
Derive a subclass named GuideDog from Dog
class. Add the license attribute. Add a
constructor and the getLicense() method.
Write a test driver to test GuideDog.