Notes 06: Abstract Classes

Download Report

Transcript Notes 06: Abstract Classes

Abstract Classes
Lecture Objectives
• To learn about abstract classes
• To understand how to inherit abstract classes
• To understand how to override abstract
methods inherited from an abstract
superclass
Abstract Classes
• An abstract class is not intended to be used to create
objects.
• Abstract classes:
 Classes that are too general to create real objects
 Used only as abstract superclasses for concrete subclasses and to
declare reference variables
 Many inheritance hierarchies have abstract superclasses occupying
the top few levels
 Keyword abstract
• Use to declare a class abstract
• Also use to declare a method abstract
– Abstract classes normally contain one or more abstract methods
– All concrete subclasses must override all inherited abstract methods
Abstract Classes (Cont’d)
• By declaring one or more methods to be abstract and by
omitting the method body, only objects of concrete derived
classes, which override the method(s), can be instantiated.
• Example:
public abstract void drawHere();
• A class that has at least one abstract method must be
declared abstract.
• However, a class could be always declared abstract
without having any abstract method!
Abstract Classes (Cont’d)
• An abstract class declares common attributes and
behaviors of the various classes in a class
hierarchy. An abstract class typically contains one
or more abstract methods that subclasses must
override if the subclasses are to be concrete. The
instance variables and concrete methods of an
abstract class are subject to the normal rules of
inheritance.
Abstract Classes (Cont’d)
• Attempting to instantiate an object of an abstract
class is a compilation error.
• Example:
abstract class Shape {
…
}
Shape sh = new Shape();
Abstract Classes (Cont’d)
• Failure to implement a superclass’s abstract
methods in a subclass is a compilation error
unless the subclass is also declared abstract.
• Example:
abstract class Shape {
public abstract void drawHere();
}
class Rectangle extends Shape {
// The method drawHere() is NOT implemented!
}
Abstract Classes (Cont’d)
Example:
public abstract class AbstractClassExample {
protected int x;
public void abstract print();
public void setX(int a)
x = a;
}
{
public AbstractClassExample()
x = 0;
}
}
{
UML Inheritance Diagrams for
Abstract Classes
Creating Abstract Superclass:
Employee class
• abstract superclass Employee
 earnings is declared abstract
• No implementation can be given for earnings in
the Employee abstract class
 An array of Employee variables will store references
to subclass objects
• earnings method calls from these variables will
call the appropriate version of the earnings
method
The Employee Class Hierarchy
Abstract Class Employee: Outline
// Employee abstract superclass.
public abstract class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
Declare abstract class Employee
Attributes common to all employees
// three-argument constructor
public Employee( String first, String last, String ssn ) {
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
Abstract Class Employee: Outline
(Cont’d)
// set first name
public void setFirstName( String first ) {
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName() {
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last ) {
lastName = last;
} // end method setLastName
// return last name
public String getLastName() {
return lastName;
} // end method getLastName
Abstract Class Employee: Outline
(Cont’d)
abstract method earnings()
has no implementation
// set social security number
public void setSocialSecurityNumber( String ssn ) {
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber() {
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString() {
return String.format( "%s %s\nsocial security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
Implementing an Abstract Method
• Define the speak() method as abstract in the superclass Animal.
public abstract class Animal {
protected String kind; // Cow, pig,
cat, etc.
public Animal() { }
public String toString() {
return "I am a " + kind + " and I
go " + speak();
}
public abstract String speak(); //
• Implement
speak() differently in
Abstract
method
public class
Cat extends Animal {
each subclass.
}
public Cat() {
kind = "cat";
public class Cow extends Animal {
}
public Cow() {
public String speak() {
kind = "cow";
return "meow";
}
}
public String speak() {
}
return "moo";
}
Extending Concrete Classes
public class SalariedEmployee extends Employee {
private double weeklySalary;
…
// calculate earnings; override abstract method earnings in Employee
public double earnings() {
return getWeeklySalary();
} // end method earnings
…
}
Extending Concrete Classes (Cont’d)
public class CommissionEmployee extends Employee {
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage…
// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
return getCommissionRate() * getGrossSales();
} // end method earnings
…
}
Extending Concrete Classes (Cont’d)
public class HourlyEmployee extends Employee {
private double wage; // wage per hour
private double hours; // hours worked for week
// calculate earnings; override abstract method earnings in Employee
public double earnings() {
if ( getHours() <= 40 ) // no overtime
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
} // end method earnings