Object-Oriented Design & Patterns

Cay S. Horstmann

Chapter 6

Inheritance and Abstract Classes

Chapter Topics

Modeling Specialization

Modeling Specialization

Modeling Specialization

.

Manager Methods and Fields

The Super/Sub Terminology

The Super/Sub Terminology


.

Inheritance Hierarchies

Inheritance Hierarchies

.

The Substitution Principle

Invoking Superclass Methods

Invoking Superclass Methods

Invoking Superclass Constructors

Preconditions

Postconditions, Visibility, Exceptions

Graphic Programming with Inheritance

Mouse Listeners

Mouse Adapters

Car Mover Program

Car Mover Program

.

Scene Editor

Scene Editor



The SceneShape Interface Type

The SceneShape Interface Type

.

The SceneShape Interface Type

public interface SceneShape
{
   void setSelected(boolean b);
   boolean isSelected();
   void draw(Graphics2D g2);
   void drawSelection(Graphics2D g2);
   void translate(int dx, int dy);
   boolean contains(Point2D aPoint);
}


CarShape and HouseShape Classes

public class CarShape implements SceneShape 
{
...
public void setSelected(boolean b) { selected = b; }
public boolean isSelected() { return selected; }
private boolean selected;
}

public class HouseShape implements SceneShape
{
...
public void setSelected(boolean b) { selected = b; }
public boolean isSelected() { return selected; }
private boolean selected;
}

Abstract Classes

public class SelectableShape implements Item 
{
public void setSelected(boolean b) { selected = b; }
public boolean isSelected() { return selected; }
private boolean selected;
}

Abstract Classes

.

Abstract Classes

Abstract Classes and Interface Types

Scene Editor

Uniform Highlighting Technique

Uniform Highlighting Technique



Template Method

TEMPLATE METHOD Pattern

Context

  1. An algorithm is applicable for multiple types.
  2. The algorithm can be broken down into primitive operations. The primitive operations can be different for each type
  3. The order of the primitive operations doesn't depend on the type

TEMPLATE METHOD Pattern

Solution

  1. Define a superclass that has a method for the algorithm and abstract methods for the primitive operations.
  2. Implement the algorithm to call the primitive operations in the appropriate order.
  3. Do not define the primitive operations in the superclass, or define them to have appropriate default behavior.
  4. Each subclass defines the primitive operations but not the algorithm.

TEMPLATE METHOD Pattern

.

TEMPLATE METHOD Pattern

Name in Design Pattern
Actual Name (Selectable shapes)
AbstractClass
SelectableShape
ConcreteClass
CarShape, HouseShape
templateMethod()
drawSelection
primitiveOp1(), primitiveOp2()
translate, draw

Compound Shapes

Compound Shapes

.

Access to Superclass Features

Protected Access

Hierarchy of Swing Components

Hierarchy of Swing Components

.

Hierarchy of Swing Components

Look and Feel



Hierarchy of Swing Components

Hierarchy of Geometrical Shapes

Hierarchy of Geometrical Shapes

.


Rectangular Shapes

Float/Double Classes

Float/Double Classes

.

Float/Double Classes

public class Rectangle2D 
{
public static class Float extends Rectangle2D
{
public double getX() { return x; }
public double getY() { return y; }
public double getWidth() { return width; }
public double getHeight() { return height;}
public void setRect(float x, float y, float w, float h)
{
this.x = x; this.y = y;
this.width = w; this.height = h;
}
public void setRect(double x, double y,
double w, double h)
{
this.x = (float)x; this.y = (float)y;
this.width = (float)w; this.height = (float)h;
}
...
public float x;
public float y;
public float width;
public float height;
}
. . .

Float/Double Classes

   . . .
public static class Double extends Rectangle2D
public double getX() { return x; }
public double getY() { return y; }
public double getWidth() { return width; }
public double getHeight() { return height;}
public void setRect(double x, double y,
double w, double h)
{
this.x = x; this.y = y;
this.width = w; this.height = h;
}
...
public double x;
public double y;
public double width;
public double height;
}
...
}

Float/Double Classes

TEMPLATE METHOD Pattern

Name in Design Pattern
Actual Name (Rectangles)
AbstractClass
Rectangle
ConcreteClass
Rectangle2D.Double
templateMethod()
contains
primitiveOpn()
getX, getY, getWidth, getHeight

Hierarchy of Exception Classes

Hierarchy of Exception Classes

.

Catching Exceptions

Defining Exception Classes

When Not to Use Inheritance

public class Point 
{
public Point(int anX, int aY) { ... }
public void translate(int dx, int dy) { ... }
private int x;
private int y;
}

public class Circle extends Point // DON'T
{
public Circle(Point center, int radius) { ... }
public void draw(Graphics g) { ... }
private int radius;
}

When Not to Use Inheritance

When Not to Use Inheritance

When Not to Use Inheritance