inheritance horstmann ch. 6-6.6, 6.9. inheritance: basic concepts subclass specializes super class:...

62
Inheritance Horstmann ch. 6-6.6, 6.9

Upload: mariah-baldwin

Post on 12-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance

Horstmann ch. 6-6.6, 6.9

Page 2: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: basic concepts

• subclass specializes super class: … extends … • inheritance hierarchies• overriding method• invoke super class method: super.methodname()• invoke super class constructor: super(…)• substitution principle• overridden methods and pre/postconditions

Page 3: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Modeling specialization

New methodNew field

public class Manager extends Employee {  public Manager(String aName) {...}  public void setBonus(double aBonus) { bonus = aBonus; }   public double getSalary() {...}   private double bonus;}

Page 4: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Manager is subclass of Employee

• Why is Manager a subclass?• Isn't a Manager superior? • Doesn't a Manager object have more fields? • The set of managers is a subset of the set of employees

Page 5: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance Hierarchy

• Real world: Hierarchies describe general/specific relationships – General concept at root of tree – More specific concepts are children

• Programming: Inheritance hierarchy – General superclass at root of tree – More specific subclasses are children

Page 6: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

What relation is there between Volunteers and Employees?

1. Volunteer is subtype of Employee

2. Employee is subtype of Volunteer

3. There is no sub/super relation between Employee and Volunteer

4. I don’t know

Employees

Volunteers

Page 7: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZWhich lines result in errors?

1. none

2. A

3. B

4. A,B

5. I don’t know

public class Volunteer extends Employee {

private String nameCopy;

public Volunteer() {

nameCopy = getName();

nameCopy = name;

}

}

B

A

Page 8: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

The Substitution Principle

• You can use a subclass object whenever a superclass object is expected

• Example:Employee e; ...

System.out.println("salary=" + e.getSalary()); • Can set e to Manager reference • Polymorphism: Correct getSalary method is

invoked

Page 9: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZWhich lines result in errors?

1.none

2.a

3.b

4.c

5.a+b

6.a+c

7.b+c

8.a+b+c

9. I don’t know

a) Employee e = new Volunteer(”Jørgen”));

b) e.setSalary(200);

c) e.setCharity(100);

Page 10: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

What is printed?

1. 1.0 and 1.0

2. 1.0 and 2.0

3. 2.0 and 1.0

4. 2.0 and 2.0

5. I don’t know

getInterest() in class Account:

return getBalance() * 0.01;

getInterest() in class ShareHolderAccount

return getBalance() * 0.02;

Application:

Account ac = new ShareHolderAccount(100);

ShareHolderAccount sh =

(ShareHolderAccount) ac;

System.out.println(ac.getinterest());

System.out.println(sh.getinterest());

Page 11: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Invoking Superclass Methods

• Can't access private fields of superclasspublic class Manager extends Employee {   public double getSalary() {      return salary + bonus;   }   ...}

• Be careful when calling superclass method   public double getSalary() {      return getSalary() + bonus;   }

Error: salary is private

Error: recursive call

Page 12: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Invoking Superclass Methods

• Use super keywordpublic double getSalary() {  return super.getSalary() + bonus;}

• super is not a reference • super turns off polymorphic call mechanism

Page 13: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Invoking Superclass Constructors

• Use super keyword in subclass constructor:public Manager(String aName) {   super(aName);   bonus = 0;}

• Call to super must be first statement in subclass constructor

• If subclass constructor doesn't call super, superclass must have constructor without parameters

Calls superclass constructor:public Employee(String na) { name = na;}

Page 14: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

Which line should replace ?

1.None of a,b,c,d

2.a

3.b

4.c

5.d

6.More than one of a,b,c,d could be used

7. I don’t know

public class Volunteer extends Employee {

public int getSalary() {

return ;

}

…}

a) salary - charity

b) super.salary – charity

c) getSalary() – charity

d) super.getSalary() - charity

Page 15: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Preconditions• Precondition of redefined method at most as strong • public class Employee {

/** Sets the employee salary to a given value. @param aSalary the new salary @precondition aSalary > 0 */ public void setSalary(double aSalary) { ... }}

• Can we redefine Manager.setSalary with precondition salary > 100000?

• No--Could be defeated:Manager m = new Manager();Employee e = m;e.setSalary(50000);

Page 16: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Postconditions, Visibility, Exceptions

• Postcondition of redefined method at least as strong

• Example: Employee.setSalary promises not to decrease salary

• Then Manager.setSalary must fulfill postcondition

• Redefined method cannot be more private. (Common error: omit public when redefining)

• Redefined method cannot throw more checked exceptions

Page 17: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

Is Volunteer implementation legal?

1.yes

2.No

3. I don’t know

public class Volunteer extends Employee {

private void setSalary(int s) {

super.setSalary(s);

}

public Volunteer(String name) {

super(name);

setSalary(0);

}

. . .

}

Page 18: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener

• Refactoring

• Abstract class

• Template Pattern

• Protected access

• Class hierarchy examples

• When not to use inheritance

Page 19: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Graphic Programming with Inheritance

• Chapter 4: Create drawings by implementing Icon interface type

• Now: Form subclass of JComponentpublic class MyComponent extends JComponent {   public void paintComponent(Graphics g) {     drawing instructions go here   }   ... }

• Advantage: Inherit behavior from JComponent • Example: Can attach mouse listener to JComponent

Page 20: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Overriding paintComponent

• Draw a car:public class CarComponent extends

JComponent {   public void paintComponent(Graphics g) {     Graphics2D g2 = (Graphics2D)g;    car.draw(g2);  }   ...

private CarShape car; }

Page 21: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming

• Mouse(Motion)Listener• Refactoring• Abstract class• Template Pattern• Protected access• Class hierarchy examples• When not to use inheritance

Page 22: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Mouse Listeners• Attach mouse listener to component • Can listen to mouse events (clicks) or mouse motion events

public interface MouseListener { void mouseClicked(MouseEvent event); void mousePressed(MouseEvent event); void mouseReleased(MouseEvent event); void mouseEntered(MouseEvent event); void mouseExited(MouseEvent event); }

public interface MouseMotionListener { void mouseMoved(MouseEvent event); void mouseDragged(MouseEvent event); }

Page 23: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Mouse Adapters• What if you just want to listen to mousePressed? • Extend MouseAdapter

public class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent event) {} public void mousePressed(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} }

• Component constructor adds listener:

addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { mouse action goes here } });

Page 24: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Car Mover Program• Use the mouse to drag a car shape• Car panel has mouse + mouse motion listeners

JComponent

CarComponent

Page 25: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

• mousePressed remembers point of mouse press addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { mousePoint = event.getPoint(); if (!car.contains(mousePoint)) mousePoint = null; } }); • mouseDragged translates car shape addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { if (mousePoint == null) return; Point lastMousePoint = mousePoint; mousePoint = event.getPoint(); double dx = mousePoint.getX() - lastMousePoint.getX(); double dy = mousePoint.getY() - lastMousePoint.getY(); car.translate( (int) dx, (int) dy ); repaint(); } });

Page 26: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

paintComponent is misspelled with small ”C” in class MyComponent. What happens?

1. Compiler error

2. Runtime error/exception

3. No error messages, but nothing is painted

4. No error messages, and component is painted as desired

5. I don’t know

Page 27: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener

• Refactoring• Abstract class• Template Pattern• Protected access• Class hierarchy examples• When not to use inheritance

Page 28: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Scene Editor

• Draws various shapes • User can add, delete,

move shapes • User selects shape

with mouse • Selected shape is

highlighted (filled in)

Page 29: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

The SceneShape Interface Type

• keep track of selection state

• draw plain or selected shape

• move shape

• hit testing: is a point (e.g. mouse position) inside?

• 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); }

Page 30: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

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; }

Page 31: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener• Refactoring

• Abstract class• Template Pattern• Protected access• Class hierarchy examples• When not to use inheritance

Page 32: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Abstract Classes• Factor out common behavior

(setSelected, isSelected)

• Subclasses inherit common behavior

• Some methods still undefined (draw, drawSelection, translate, contains)

public abstract class SelectableShape implements SceneShape { public void setSelected(boolean b) { selected = b; }

public boolean isSelected() { return selected; }

private boolean selected; }

Page 33: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

• HouseShape and CarShape are concrete

• Can't instantiate abstract class:SelectableShape s =

new SelectableShape(); // NO

• Ok to have variables of abstract class type:SelectableShape s =

new HouseShape(); // OK

Page 34: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Abstract Classes and Interface Types

• Abstract classes can have fields  • Interface types can only have constants

(public static final) • Abstract classes can define methods • Interface types can only declare methods • A class can implement any number of

interface types • In Java, a class can extend only one other

class

Page 35: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

Are implementations of SavingsAccount and Account legal?

1. yes

2. no

3. Maybe – depends on context

4. I don’t know

public abstract class Account {

private int balance;

public abstract void deductFee();

public int getInterest() { return 0; }

}

public class SavingsAccount extends Account

{ public void deductFee() {} }

Page 36: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Scene Editor

• Mouse listener selects/unselects item addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent event) { mousePoint = event.getPoint(); for (SceneShape s : shapes) { if (s.contains(mousePoint))

s.setSelected(!s.isSelected()); } repaint(); } });

Page 37: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Scene Editor

• Mouse motion listener drags selected items addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { Point lastMousePoint = mousePoint; mousePoint = event.getPoint(); for (SceneShape s : shapes) if (s.isSelected()) { double dx = mousePoint.getX() - lastMousePoint.getX(); double dy = mousePoint.getY() - lastMousePoint.getY(); s.translate((int)dx,(int)dy); } repaint(); } });

Page 38: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Scene Editor

• Remove button removes selected items

removeButton.addActionListener(new

ActionListener() {

public void actionPerformed(ActionEvent event) {

scene.removeSelected();

}

}

);

Page 39: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener• Refactoring• Abstract class

• Template Pattern• Protected access• Class hierarchy examples• When not to use inheritance

Page 40: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Uniform Highlighting Technique

Page 41: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Uniform Highlighting Techniquepublic void drawSelection(Graphics2D g2) {  translate(1, 1);

draw(g2);translate(1, 1);draw(g2);translate(-2, -2)

}

• drawSelection calls draw • draw not implemented yet!

• Declare as abstract methodpublic abstract void draw(Graphics2D g2);

• Defined in CarShape, HouseShape

• drawSelection method calls draw, translate • drawSelection doesn't know which methods --

polymorphism • drawSelection is a template method

Page 42: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

TEMPLATE METHOD PatternContext• An algorithm is applicable for multiple types.

• The algorithm can be broken down into primitive operations. The primitive operations can be different for each type

• The order of the primitive operations doesn't depend on the type

Solution

• Define a superclass that has a method for the algorithm and abstract methods for the primitive operations.

• Implement the algorithm to call the primitive operations in the appropriate order.

• Do not define the primitive operations in the superclass, or define them to have appropriate default behavior.

• Each subclass defines the primitive operations but not the algorithm.

Page 43: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

TEMPLATE METHOD Pattern

Page 44: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

TEMPLATE METHOD Pattern

drawSelection()draw()

translate()

SelectableShape

draw()translate()

CarShape

EXAMPLE

Page 45: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZWhich exemplifies

Template Method pattern?

Instance variable in class Client:

Board b;

Method playGame in class Client:

b.init();

while (!b.done()) b.move();

a) Method playGame in class Game:

init();

while (!done()) move();

b)

1. None

2. a

3. b

4. a+b

5. I don’t know

Page 46: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener• Refactoring• Abstract class• Template Pattern

• Protected access• Class hierarchy examples• When not to use inheritance

Page 47: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Compound Shapes

• House = rectangle + triangle• Car = rectangle + circles + lines• CompoundShape uses GeneralPath• java.awt.geom.GeneralPath: sequence of

shapesGeneralPath path = new GeneralPath(); path.append(new Rectangle(...), false); path.append(new Triangle(...), false); g2.draw(path); 

• Advantage: Containment test is freepath.contains(aPoint);

Page 48: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Access to Superclass Features

• Why does the HouseShape constructor call add?public HouseShape() { add(new Rectangle(...)); add(new Triangle(...));}

• Why not justpath.append(new Rectangle(...));

• HouseShape inherits path field

• HouseShape can't access path • path is private to superclass

Page 49: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Protected Access

• Make CompoundShape.add method protected • Protects HouseShape: other classes can't add graffiti • Protected features can be accessed by subclass methods... • ...and by methods in the same package • Bad idea to make fields protected

protected GeneralPath path; // DON'T

• Ok to make methods protectedprotected void add(Shape s) // GOOD

• Protected interface separate from public interface

Page 50: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

Should this structure be refactored?

1. Yes, implement listCourses in common superclass

2. Yes, define abstract method listCourses in common superclass

3. No, keep as it is

4. Depends …

5. I don’t know

Page 51: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener• Refactoring• Abstract class• Template Pattern• Protected access

• Class hierarchy examples• When not to use inheritance

Page 52: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Hierarchy of Swing

Components

Page 53: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Hierarchy of Geometrical Shapes

Page 54: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Hierarchy of Exception Classes

Page 55: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Inheritance: Examples

• Graphics programming• Mouse(Motion)Listener• Refactoring• Abstract class• Template Pattern• Protected access• Class hierarchy examples

• When not to use inheritance

Page 56: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

When Not to Use Inheritancepublic 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; }

• A circle isn't a point• By accident, inherited translate works for circles

Page 57: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

When Not to Use Inheritance• public class Rectangle extends Point { // DON'T

public Rectangle(Point corner1, Point corner2) { ... }

public void draw(Graphics g) { ... } public void translate(int dx, int dy) { ... } private Point other; }

• That's even weirder:public void translate(int dx, int dy) { super.translate(dx, dy); other.translate(dx, dy);}

• Remedy: Use aggregation.  • Circle, Rectangle classes have points

Page 58: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Stack: examples

• Plates in a cafeteria

• "last hired, first fired"

• Stack of changes in text editor. "undo" reverts topmost change

• Stack of URL's in web browser. Back button returns to topmost URL on stack.

• Stack of books

Page 59: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Stack

• a stack consists of some objects placed on top of each other (ordered)

• a new object may be placed on top of those already in the stack

• the topmost object may be removed from the stack

• LIFO = Last In First Out

Page 60: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

Stack

• java.util.Stack<E> is concrete implementation of a stack

• most important operations:boolean empty()E peek()E pop()E push(E item)

peeks returns topmost element without removing it

Page 61: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

When Not to Use Inheritance

• Java standard library:public class Stack<T> extends Vector { // DON'T T pop() { ... } T push(T item) { ... } ...}

• Bad idea: Inherit all Vector methods • Can insert/remove in the middle of the stack • Remedy: Use aggregationpublic class Stack<T> { ... private ArrayList<T> elements;}

Page 62: Inheritance Horstmann ch. 6-6.6, 6.9. Inheritance: basic concepts subclass specializes super class: … extends … inheritance hierarchies overriding method

QUIZ

Which UML figure is most appropriate?

1. a

2. b

3. c

4. d

5. I don’t know

a)

b)

d)

c)

Queue