1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes:...

74
1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition

Transcript of 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes:...

Page 1: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 1

94.204* Winter 1999

Part 6

Reusing Classes:

Inheritance and Composition

Page 2: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 2

UML Diagram of the Counter Class Hierarchy (From Part 5)

Object

Counter

LimitedCounterRollOverCounter

Page 3: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 3

Aside

The diagrams that we use to distinguish between references and objects are not part of the UML. Using this notation to depict an entire OO program would result in a cluttered diagram! They are intended for helping visualize low-level details.

In UML, a class or an object

Our object reference/object notation

Page 4: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 4

Inheritance: Important Concepts

• Generalization: common behaviour and attributes are moved up the class hierarchy, where they can be inherited by subclasses

Page 5: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 5

Inheritance: Important Concepts

• Specialization: behaviour and attributes that apply to a subset of the subclasses (or only one subclass) are moved down the class hierarchy– also: behaviours inherited from a superclass

can be overridden by a subclass

Page 6: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 6

Inheritance: Important Concepts

• an is-a relationship exists between superclasses and subclasses; e.g., – a Counter is-a kind of Object

– a RolloverCounter is-a kind of Counter

• inheritance provides many benefits, but is inheritance always the best approach to developing new classes?

Page 7: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 7

Inheritance Is Not Always the Best Approach...

• suppose we define a class for representing points:

class Point { // see project 1!

// Coordinates of the point   private double x, y;

   public Point() {…}

public Point(double x, double y)    {..} 

Page 8: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 8

The Point Class, Cont’d

   public void setX(double x) {…} public void setY(double x) {…}

public double getX() {…}

public double getY() {…} public String toString() {…}    public void moveto(double x,

double y)   {      this.x = x;      this.y = y;   }

Page 9: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 9

Inheritance Is Not Always the Best Approach...

• now suppose we want to define a class for representing circles

• a circle is similar to a point – its center is a point on the x-y plane

• a circle is different than a point – it has a radius

• we decide to define class Circle as a subclass of class Point

Page 10: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 10

Circle Is a Kind-of Point

class Circle extends Point {

    private double radius;

   public Circle()   {      super();      radius = 0;   }

Page 11: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 11

Circle Is a Kind-of Point

 public Circle(double x, double y,

double r)   {      super(x, y);      radius = r;   }

   public double getRadius()   {      return radius;   }

Page 12: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 12

Circle Is a Kind-of Point

   public void setRadius(double r)   {      if (r > 0.0)         radius = r;      else         radius = 0.0;   }

Page 13: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 13

Circle Is a Kind-of Point

   public String toString()   {      return “Center = ” + “[” +

getX() + “, ” + getY()  + ”]” + “; Radius = ” + getRadius();   }

}

Page 14: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 14

Using the Circle Class

// Create a Circle object with center

// [0,0] and radius 0

Circle c1;

C = new Circle();

// Create a Circle object with center

// [5.1, 3.7] and radius 12.2

Circle c2 = new Circle(5.1, 3.7,

12.2);

Page 15: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 15

Using the Circle Class

// Invoke methods defined in Circle

C1.setRadius(4.3);

double area = c2.area();

// Invoke methods that Circle inherits

// From Point

C1.moveTo(-1.2, 8.6);

double xCoord = c2.getX();

double yCoord = c2.getY();

Page 16: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 16

Using the Circle Class

// Circle overrides Point's toString()

// method

System.out.print(c1);

// The string ”Center = [-1.2, 8.6];

// Radius = 4.3” is displayed

Page 17: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 17

UML Diagram of the Class Hierarchy

Object

Point

Circle

a Point is-a kind of Object

a Circle is-a kind of Point

Page 18: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 18

A Hierarchy of Classes That Represent Different 2-D Shapes

Object

Point

Circle Rectangle Triangle

Page 19: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 19

Inheritance Is Not Always the Best Approach...

• is Point the best superclass for Rectangle and Triangle?

• a rectangle is normally defined by 2 points (opposite corners) or 4 points (the 4 corners)

• a triangle is normally defined by 3 points (the vertices)

• does it make sense to say that a Rectangle is-a Point?

Page 20: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 20

Evaluating Is-a Relationships

• does it model a real-world relationship?– a car is-a vehicle

•Car could be a subclass of Vehicle

– a car has an engine, but a car is not an engine•Car should not be a subclass of Engine

Page 21: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 21

Evaluating Is-a Relationships

• is the class a reasonable superclass for a group of conceptually similar subclasses?– Circle, Triangle and Rectangle are

all 2-D shapes

– is the is-a relationship with Point equally good for all of these classes?

Page 22: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 22

Evaluating Is-a Relationships

• Principle of substitution– is it reasonable to use an instance of the

subclass anywhere you would use an instance of the superclass?

– if not, maybe is-a is the wrong relationship for the classes

Page 23: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 23

Class Design by Composition

• Instead of designing Rectangle, Circle, and Triangle as subclasses of Point, design the classes so that: – a Circle object will contain a reference to a Point object

– a Rectangle object will contain references to two Point objects

– a Triangle object will contain references to three Point objects

Page 24: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 24

Has-a Relationships

• there is now a has-a relationship between Circle and Point, Rectangle and Point, Triangle and Point

• we say that: – a Circle has-a Point– a Rectangle has-two Points– a Triangle has-three Points

Page 25: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 25

Class Reuse Through Composition

• in the code for class Circle (see next slide), notice that:– instance variable center is a reference to a Point object

– the constructors allocate a Point object

– toString() sends getx() and gety() messages to the Point object to obtain the coordinates of the circle’s center

Page 26: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 26

Circle Has-a Point

class Circle {

private Point center; private double radius;

   public Circle()   {      center = new Point(0.0, 0.0);      radius = 0.0;   }

Page 27: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 27

Circle Has-a Point

public Circle(double x, double y, double r)   {      center = new Point(x, y);      radius = r;   }

   public double getRadius()   {      return radius;   }

Page 28: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 28

Circle Has-a Point

public void setRadius(double r)

   {

      if (r > 0.0)

         radius = r;

      else

         radius = 0.0;

   }

Page 29: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 29

Circle Has-a Point

public String toString()

   {

      return ”Center = " + "[" +

center.getX() + ", " +

center.getY() + "]" +

"; Radius = " + getRadius();

   }

}

Page 30: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 30

UML Diagrams

Circle Point1

• the line with the black diamond denotes composite aggregation (a.k.a. has-a):

• a Circle object has 1 Point object• the Point object is part-of exactly one Circle object• when the Circle object is destroyed, the Point object is destroyed

Page 31: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 31

UML Diagrams

Rectangle Point2

Triangle Point3

Page 32: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 32

What About getX(), getY(), setX(), setY() and moveTo()?

• the first (is-a) version of Circle inherited these methods from Point

• if we want these methods in the second (has-a) version of Circle, we need to add these methods to the Circle class

Page 33: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 33

Circle.moveTo()

• the moveTo() method forwards the “move-to” message to the Point object – moving a circle’s center point moves the

entire circle

public void moveTo(double x, double y)   {      center.moveTo(x, y);   }

Page 34: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 34

Setters and Getters

• Circle.setX(), Circle.setY(), Circle.getX(), and Circle.getY() would be similar to Circle.moveTo()– they would forward the message to the Point object

Page 35: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 35

Designing Class Hierarchies

• let’s continue the development of the classes that represent 2-D shapes (Circle, Rectangle, Triangle) by specifying that 2-D objects must be able to compute and return their area and perimeter– all 2-D shape classes must respond to area() and perimeter() messages

Page 36: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 36

Designing Class Hierarchies

• how do we ensure that all the 2-D shape classes provide these behaviours?

• create a common superclass called TwoDimensionalShape that has area() and perimeter() methods?– but these methods must be overridden in all

2-D shape subclasses - there is no common behaviour that can be inherited

Page 37: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 37

Abstract Classes

abstract class TwoDimensionalShape {   abstract public double perimeter(); abstract public double area();}

• abstract class TwoDimensionalShape cannot be instantiated

• the abstract methods do not have implementations

Page 38: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 38

Abstract Classes

• the intent is that one or more concrete subclasses will be derived from TwoDimensionalShape

• each concrete subclass of TwoDimensionalShape must define the area() and perimeter() methods

• we can create instances of concrete subclasses

Page 39: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 39

Hierarchy of 2-d-shape Classes

Object

TwoDimensionalShape

Circle Rectangle Triangle

Italicized name denotes abstract class

Page 40: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 40

Class Circle

class Circle extends TwoDimensionalShape{ // The body of this class is identical // to the has-a version of Circle, // but also defines the perimeter() // and area() methods.}

Page 41: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 41

Concrete Implementations of Abstract Methods (in Circle)

   public double area()   {    return java.lang.Math.PI *

getRadius() * getRadius();   }

    public double perimeter()  {

       return 2 * java.lang.Math.PI * getRadius();    }

Page 42: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 42

Class Circle

• if we don’t define perimeter() and area() methods, the compiler will remind us to define them or redeclare Circle as an abstract class (in which case we can’t create Circle objects)

• similarly, the Triangle and Rectangle classes must define perimeter() and area() methods

Page 43: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 43

Designing Class Hierarchies

• suppose we want to develop a group of classes that represent 3-D shapes (Cylinder, Cone, Cube, Pyramid)

• all 3-D objects must be able to compute and return their surface area and volume– all 3-D shape classes must respond to area() and volume() messages

Page 44: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 44

Designing Class Hierarchies

• make the 3-D shape classes subclasses of the corresponding 2-D shape classes

• the 2-D shape classes are unchanged (each class is composed of at least one Point)

• add an abstract method perimeter() to abstract class Shape (formerly TwoDimensionalShape)

Page 45: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 45

UML Diagram

Shape

Circle Rectangle Triangle

Cylinder Cone PyramidCube

Page 46: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 46

Abstract Class Shape

abstract class Shape {   abstract public double perimeter(); abstract public double area(); abstract public double volume();}

• all 2-D and 3-D classes must implement these methods– for 2-D shapes, volume() will always

return 0– but, what is the perimeter of a 3-D shape?

Page 47: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 47

Class Cylinder

  class Cylinder extends Circle {

   private double height;

   public Cylinder() { super(); height = 0.0; }

Page 48: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 48

Class Cylinder

public Cylinder(double x, double y, double r, double h) { super(x, y, r); height = h; }   public double getHeight()   {      return height;   }

Page 49: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 49

Class Cylinder

 public void setHeight(double h)

   {

      if (h > 0.0)

         height = h;

      else

         height = 0.0;

   }

Page 50: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 50

Class Cylinder

public double area()   {      return 2 * super.area() + height * super.perimeter();   }

public double volume()   {    return super.area() * height; }

Page 51: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 51

Class Cylinder

// Inherits perimeter from Circle.   // Should we define a perimeter // method that throws an exception? // What is the perimeter of a 3-D // Shape?    public String toString()   {      return super.toString() + "; Height = " + height;   }

Page 52: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 52

Discussion

• look at the the is-a relationships between the 2-D and 3-D shape classes – does “a cylinder is-a circle” accurately model

the real world?

– we should also consider has-a relationships

• perimeter() is easy to implement in the 2-D shape classes, but how should we override it in the 3-D shape classes?

Page 53: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 53

Second Attempt at a Class Hierarchy

• split the abstract methods among three abstract classes

• develop two parallel concrete class hierarchies– one for 2-D shape classes

– one for 3-D shape classes• 3-D objects will be composed of one or more

2-D objects

Page 54: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 54

UML Diagram for the Abstract ClassesShape

TwoDimensionalShape ThreeDimensionalShape

Page 55: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 55

Abstract Classes for the Shapes Hierarchy

abstract class shape { // These will be implemented by all // classes that represent shapes

abstract public double area(); abstract public double volume();}

Page 56: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 56

Abstract Classes for the Shapes Hierarchy

abstract class TwoDimensionalShape extends Shape { // This will be implemented by all // classes that represent 2-D shapes abstract public double perimeter();}

Page 57: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 57

Abstract Classes for the Shapes Hierarchy

abstract class ThreeDimensionalShape extends Shape{ // Any abstract methods?? // Is it just a placeholder for now?}

Page 58: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 58

UML Diagram for the 2-D Shape Classes

TwoDimensionalShape

Circle Rectangle Triangle

• each 2-D shape object contains one or more Point objects

Page 59: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 59

UML Diagram for the 3-D Shape Classes

ThreeDimensionalShape

Cylinder PyramidCone

Rectangle

Cube

Circle RectangleCircle

Triangle

Page 60: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 60

More About Abstract Classes

• an abstract class can have instance variables and instance methods

• an abstract class can have class (static) variables and methods

Page 61: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 61

More About Abstract Classes

• for example, we could define class Shape this way:

abstract class Shape { abstract public double area(); public double volume() { return 0.0; }}

Page 62: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 62

More About Abstract Classes

• all 2-D classes inherit volume() – makes sense - the volume of a 2-D object is

always 0

• all 3-D classes override volume()

• exercise for the student: implement the complete class hierarchy that has just been presented

Page 63: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 63

More About Abstract Classes

• an abstract class does not need to have any abstract methods (although it usually will)

• if a subclass of an abstract class does not override all of the abstract methods in the abstract class, then the subclass must also be declared abstract

Page 64: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 64

Interfaces

• interface - a collection of constants and abstract methods

• unlike abstract classes, an interface cannot have instance variables or methods, class variables or methods

• interfaces are not classes – we cannot create instances of an interface

Page 65: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 65

Interfaces

• an interface is used to specify the interface (the methods) that a class must implement

• an interface can also be used to define constants that a class (hierarchy) can use

Page 66: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 66

Interfaces

interface Shape

{   double area();   double volume();}

• each of the methods is automatically public and abstract – no other modifiers are permitted

Page 67: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 67

Interface Inheritance

• interfaces can be arranged in an interface hierarchy:

interface TwoDimensionalShapeextends Shape{ double perimeter();}

• TwoDimensionalShape inherits the abstract methods defined by Shape

Page 68: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 68

Interface Implementation

class Circle implements TwoDimensionalShape { ...}• Circle must implement (provide bodies

for) perimeter(), area(), and volume()

Page 69: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 69

UML Notation for Interfaces

Shape

TwoDimensionalShape ThreeDimensionalShape

Page 70: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 70

UML Notation for Classes That Implement the Shape Interfaces

TwoDimensionalShape

Circle Rectangle Triangle

• each 2-D object contains one or more Point objects (not depicted here)

Page 71: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 71

UML Notation for Classes That Implement the Shape Interfaces

ThreeDimensionalShape

Cylinder PyramidCone

Rectangle

Cube

Circle RectangleCircle

Triangle

Page 72: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 72

Interfaces: General Form

interface I1 { // I2 defines constants and abstract // methods }interface I2 extends I1 { // I2 inherits constants and abstract // methods from I1 // I2 defines additional constants and // abstract methods} …also, interface I3 {…}

Page 73: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 73

Interface Implementation: General Form

class C1 {/* variables and methods */}

class C2 extends C1 implements I2, I3 {

// C2 inherits variables and methods

// from C1

// C2 defines additional variables and

// methods

// C2 defines all methods specified by

// I1, I2, and I3

}

Page 74: 1999 01 25Copyright (c) 1998, 1999 D.L. Bailey1 94.204* Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.

1999 01 25 Copyright (c) 1998, 1999 D.L. Bailey 74

Extending a Class vs. Implementing an Interface

• a class can directly extend at most one concrete superclass or one abstract superclass– no multiple inheritance in Java

• a class can implement any number of interfaces

• more on interfaces when we look at polymorphism and how classes can represent multiple types