Abstract classes

Report
Inheritance and Polymorphism:
Abstract Classes
The “not quite” classes
SE-1020
Dr. Mark L. Hornick
1
What if some attributes and behaviors
are exactly the same in two (or more)
different classes?
Sometimes, instantiable classes may share common
behavior for some methods


For example: Consider classes that represent
Dogs and Cats. Basic behaviors like getName()
or getAge() might be identical…
..while other behaviors may differ, like eat() or speak()
In such cases, it would be nice to be able to
implement that common behavior in only one
place, to avoid unnecessary duplication…

Remember, interfaces only declare methods, but cannot define
them
SE-1020
Dr. Mark L. Hornick
2
Why not do this by having both Dog
and Cat extend another class like
Pet?
Maybe, but consider these issues:



Is Pet really a specific type of animal?
What if we’re only dealing with Dogs or Cats? Do we
really need a Pet class?
Can we prevent a Pet from being created?
Sometimes, a class represents the general aspects of
more specific (ie derived) classes, but it doesn’t make
sense to actually create objects of the general class.
This situation can be handled with a special type of a
class that is called an abstract class
SE-1020
Dr. Mark L. Hornick
3
An abstract class is defined with
the modifier abstract
public abstract class Pet {…}



An abstract class can define any number of
attributes and methods, like a regular class
No instances can be created of an abstract
class
An abstract class may extend another abstract
class
SE-1020
Dr. Mark L. Hornick
4
Example of an abstract class definition
public abstract class Pet {
protected String name; // attr defn
protected int age; // attr defn
…
// implementation of shared behavior
public void getName() { return name; } // defn
public void getAge() { return age; } // defn
…
}
SE-1020
Dr. Mark L. Hornick
5
Abstract classes can also inherit interface
behaviors, but do not have to implement
them – delegating the implementation to
subclasses
public abstract class Pet implements Animal {
protected String name; // attr defn
protected int age; // attr defn
…
// implementation of shared behavior in Pet-derived
subclasses:
public void getName() { return name; } // defn
public void getAge() { return age; } // defn
…
// Animal methods do not have to be implemented
here, although they can be if desired. If not
implemented here, they MUST be implemented in
regular classes that extend Pet.
SE-1020
}
Dr. Mark L. Hornick
6
The Java extends keyword is used to
declare that a class inherits behaviors (and
attributes) of another class, including abstract
classes
public class Dog extends Pet implements Mammal{
public void shedFur() { // defn of Mammal
method
}
// only behaviors and attributes that are not
implemented in Pet have to be defined here;
Pet-defined attributes and behaviors are
inherited by Dog
// defn of Animal method inherited by Pet
public void speak() {
System.out.println(“Woof”);
}
SE-1020
7
Dr. Mark L. Hornick
}
In UML the relationship between class,
abstract classes, and interfaces is
illustrated as follows:
The Generalization connector is used to illustrate that a class
extends either a regular class or an abstract class
Note the italics used for the abstract Pet class
SE-1020
Dr. Mark L. Hornick
8
Polymorphism again allows an interface
variable to refer to objects from different
classes that implement the interface
For example, if Cat and Dog both extend an abstract class
called Pet, then the following statements are valid
Pet myDog, myCat;
myCat = new Dog();
. . .
myCat = new Cat();
A Dog or a Cat can be used anyplace that expects a Pet
•
as a method argument: public void feed( Pet p )
•
In a collection (e.g. ArrayList<Pet>)
SE-1020
Dr. Mark L. Hornick
9
Inheritance versus Interface
Use the Java interface to declare a behavior that
must be implemented among related classes


Methods on related classes are used the same way
 Example: ArrayList & LinkedList both share common
behavior from the List interface
Use extension inheritance to share common
implementation

Dog, Cat, Goldfish share implementation of getName() &
getAge()
SE-1020
Dr. Mark L. Hornick
10
An abstract method is a
method of an abstract class…

…with the keyword abstract, and it ends with a
semicolon instead of a method body
public abstract void play();

Instantiable (non-abstract) subclasses of an abstract
superclass with abstract methods MUST implement
the inherited abstract methods

Similar to overriding, but in this case overriding an
unimplemented method
SE-1020
Dr. Mark L. Hornick
11
Abstract Pet class with an abstract play()
method:
Note that the abstract play() method is shown in italics in the
abstract Pet class, but shown in regular font where implemented in
the regular classes.
Portions adapted with permission from the textbook
author.
CS-1020
Dr. Mark L. Hornick
12
Why abstract methods?
An abstract class may declare abstract methods
that MUST be implemented in any (regular)
class extending the abstract class

In this way, an abstract method is like an interface
method, which means that the declared behavior
MUST be implemented at some point in the
inheritance heirarchy.
SE-1020
Dr. Mark L. Hornick
13
Rules for abstract methods…
abstract methods may not be declared
private or static

Since private and static methods cannot be overridden in
subclasses
SE-1020
Dr. Mark L. Hornick
14

similar documents