Abstract Classes and Interfaces
The objectives of this chapter are:
To explore the concept of abstract classes
To understand interfaces
To understand the importance of both.
What is an Abstract class?
Superclasses are created through the process called
"generalization"
Common features (methods or variables) are factored out of object
classifications (ie. classes).
Those features are formalized in a class. This becomes the superclass
The classes from which the common features were taken become
subclasses to the newly created super class
Often, the superclass does not have a "meaning" or does not
directly relate to a "thing" in the real world
It is an artifact of the generalization process
Because of this, abstract classes cannot be instantiated
They act as place holders for abstraction
Abstract Class Example
In the following example, the subclasses represent objects
taken from the problem domain.
The superclass represents an abstract concept that does not
exist "as is" in the real world.
Abstract superclass: Vehicle Note: UML represents abstract
- make: String classes by displaying their name
- model: String in italics.
- tireCount: int
Car Truck
- trunkCapacity: int - bedCapacity: int
What Are Abstract Classes Used For?
Abstract classes are used heavily in Design Patterns
Creational Patterns: Abstract class provides interface for creating
objects. The subclasses do the actual object creation
Structural Patterns: How objects are structured is handled by an
abstract class. What the objects do is handled by the subclasses
Behavioural Patterns: Behavioural interface is declared in an abstract
superclass. Implementation of the interface is provided by subclasses.
Be careful not to over use abstract classes
Every abstract class increases the complexity of your
design
Every subclass increases the complexity of your design
Ensure that you receive acceptable return in terms of functionality given
the added complexity.
Defining Abstract Classes
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object
public abstract class Vehicle
{ Vehicle
private String make; - make: String
private String model; - model: String
private int tireCount; - tireCount: int
[...]
public class Car extends Vehicle
{
Car Truck
- trunkCapacity: int - bedCapacity: int
private int trunkCapacity;
[...]
public class Truck extends Vehicle
{
private int bedCapacity; Often referred to as "concrete" classes
[...]
Abstract Methods
Methods can also be abstracted
An abstract method is one to which a signature has been provided, but
no implementation for that method is given.
An Abstract method is a placeholder. It means that we declare that a
method must exist, but there is no meaningful implementation for that
methods within this class
Any class which contains an abstract method MUST also be
abstract
Any class which has an incomplete method definition cannot
be instantiated (ie. it is abstract)
Abstract classes can contain both concrete and abstract
methods.
If a method can be implemented within an abstract class, and
implementation should be provided.
Abstract Method Example
In the following example, a Transaction's value can be
computed, but there is no meaningful implementation that can
be defined within the Transaction class.
How a transaction is computed is dependent on the transaction's type
Note: This is polymorphism.
Transaction
- computeValue(): int
RetailSale StockTrade
- computeValue(): int - computeValue(): int
Defining Abstract Methods
Inheritance is declared using the "extends" keyword
If inheritance is not defined, the class extends a class called Object
Note: no implementation
public abstract class Transaction
{
public abstract int computeValue(); Transaction
- computeValue(): int
public class RetailSale extends Transaction
{
public int computeValue() RetailSale StockTrade
{ - computeValue(): int - computeValue(): int
[...]
public class StockTrade extends Transaction
{
public int computeValue()
{
[...]
abstract class Bike
{
abstract void run();
}
class Honda4 extends Bike
{
void run(){System.out.println("running safely");
}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
}
What is Interface
An interface is a named collection of method definitions and
constants ONLY.
An interface defines a protocol of behavior that can be implemented
by any class anywhere in the class hierarchy.
An interface defines a set of methods but does not implement them.
A class that implements the interface agrees to implement all the
methods defined in the interface, thereby agreeing to certain
behaviors.
What is an Interface?
An interface is similar to an abstract class with the following
exceptions:
All methods defined in an interface are abstract. Interfaces can contain
no implementation
Interfaces cannot contain instance variables. However, they can
contain public static final variables (ie. constant class variables)
• Interfaces are declared using the "interface" keyword
If an interface is public, it must be contained in a file which
has the same name.
• Interfaces are more abstract than abstract classes
• Interfaces are implemented by classes using the
"implements" keyword.
Declaring an Interface
In Steerable.java:
public interface Steerable
{
public void turnLeft(int degrees);
When a class "implements" an
public void turnRight(int degrees);
interface, the compiler ensures that
it provides an implementation for
}
all methods defined within the
In Car.java: interface.
public class Car extends Vehicle implements Steerable
{
public int turnLeft(int degrees)
{
[...]
}
public int turnRight(int degrees)
{
[...]
}
Defining Interfaces
Interface Declaration
Interface Body
The interface body contains method declarations
for ALL the methods included in the interface.
A method declaration within an interface is
followed by a semicolon (;) because an interface
does not provide implementations for the methods
declared within it.
All methods declared in an interface are implicitly
public and abstract.
Implementing Interfaces
A Class can only inherit from one super class. However, a
class may implement several Interfaces
The interfaces that a class implements are separated by commas
• Any class which implements an interface must provide an
implementation for all methods defined within the interface.
NOTE: if an abstract class implements an interface, it NEED NOT
implement all methods defined in the interface. HOWEVER, each
concrete subclass MUST implement the methods defined in the
interface.
Declaring an Interface
In Car.java:
public class Car extends Vehicle implements Steerable, Driveable
{
public int turnLeft(int degrees)
{
[...]
}
public int turnRight(int degrees)
{
[...]
}
// implement methods defined within the Driveable interface
Inheriting Interfaces
If a superclass implements an interface, it's subclasses also
implement the interface
public abstract class Vehicle implements Steerable
{ Vehicle
private String make; - make: String
[...] - model: String
- tireCount: int
public class Car extends Vehicle
{
Car Truck
- trunkCapacity: int - bedCapacity: int
private int trunkCapacity;
[...]
public class Truck extends Vehicle
{
private int bedCapacity;
[...]
Multiple Inheritance
Class A Class B Class C
Class ABC
Class ABC inherits all variables and
methods from Class A, Class B, and Class
C.
Java does NOT support multiple inheritances.
However, you can use interface to
implement the functionality of multiple
inheritance.
Multiple Inheritance?
Some people (and textbooks) have said that allowing classes
to implement multiple interfaces is the same thing as multiple
inheritance
This is NOT true. When you implement an interface:
The implementing class does not inherit instance variables
The implementing class does not inherit methods (none are defined)
Implementation of interfaces is not inheritance. An interface
defines a list of methods which must be implemented.
Abstract Classes Versus Interfaces
When should one use an Abstract class instead of an
interface?
If the subclass-superclass relationship is an "is a" relationship.
If the abstract class can provide an implementation at the appropriate
level of abstraction
• When should one use an interface in place of an Abstract
Class?
When the methods defined represent a small portion of a class
When the subclass needs to inherit from another class
When you cannot reasonably implement any of the methods
interface Polygon {
void getArea(int length, int breadth);
}
class Rectangle implements Polygon {
public void getArea(int length, int
breadth) {
System.out.println("The area of
the rectangle is " + (length * breadth));
}
}
class Main {
public static void main(String[]
args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}