Inheritance Inheritance: basic concepts subclass specializes super - - PDF document

inheritance inheritance basic concepts
SMART_READER_LITE
LIVE PREVIEW

Inheritance Inheritance: basic concepts subclass specializes super - - PDF document

Horstmann ch. 6-6.6, 6.9 Inheritance Inheritance: basic concepts subclass specializes super class: extends inheritance hierarchies overriding method invoke super class method: super.methodname() invoke super class


slide-1
SLIDE 1

Inheritance

Horstmann ch. 6-6.6, 6.9

slide-2
SLIDE 2

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
slide-3
SLIDE 3

Modeling specialization

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

slide-4
SLIDE 4

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
slide-5
SLIDE 5

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

slide-6
SLIDE 6

Employee e = new Employee(); e.setSalary(100); e.setCharity(50); Volunteer v = new Volunteer(); v.setSalary(10); v.setCharity(5);

QUIZ

Which lines are legal? 1. none 2. A 3. B 4. A,B 5. I don’t know Inheritance B A

public class Employee { private int salary; public void setSalary(int n) { salary = n; } } public class Volunteer extends Employee { private int charity; public void setCharity(int n) { charity = n; } }

slide-7
SLIDE 7

The Substitution Principle

  • You can use a subclass object whenever a

superclass object is expected

  • Example:

Employee e; ... System.out.println("salary=" + e.getSalary());

  • Polymorphism: Correct getSalary method is

invoked

e = new Employee(”Axel”);

  • r

e = new Manager(”Bent”);

  • r

...

slide-8
SLIDE 8

QUIZ

getInterest() in class Account: return getBalance() * 0.01; getInterest() in class ShareHolderAccount return getBalance() * 0.02; Application: ShareHolderAccount sh = new ShareHolderAccount(100); System.out.println(sh.getinterest()); Account ac = new ShareHolderAccount(100); System.out.println(ac.getinterest()); Inheritance and Polymorphism 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

slide-9
SLIDE 9

Invoking Superclass Methods

  • Can't access private fields of superclass

public 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

slide-10
SLIDE 10

Invoking Superclass Methods

  • Use super keyword

public double getSalary() { return super.getSalary() + bonus; }

  • super is not a reference
  • super turns off polymorphic call mechanism
slide-11
SLIDE 11

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

slide-12
SLIDE 12

QUIZ

public class Volunteer extends Employee { public int getSalary() { return ; } …} a) salary - charity b) getSalary() – charity c) super.getSalary() - charity Which lines may replace if we want to return salary-charity? 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 Turn off polymorphism

slide-13
SLIDE 13

Inheritance: more concepts

  • Refactoring
  • Abstract class
  • Template Pattern
  • Protected access
slide-14
SLIDE 14

Scene Editor

  • Draws various shapes
  • User can add, delete,

move shapes

  • User selects shape

with mouse

  • Selected shape is

highlighted (filled in)

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

Inheritance: more concepts

  • Refactoring
  • Abstract class
  • Template Pattern
  • Protected access
slide-18
SLIDE 18

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

slide-19
SLIDE 19
  • 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

slide-20
SLIDE 20

Abstract Classes and Interface Types

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

– public static final int MAXIMUM = 1000;

  • Abstract classes can both define and declare methods

– Define: public int getValue() { return value; } – Declare: public abstract int getValue();

  • Interface types can only declare methods

– public int getValue();

  • A class can implement any number of interface types
  • In Java, a class can extend only one other class
slide-21
SLIDE 21

QUIZ

public abstract class Account { private int balance; public abstract int computeFee(); public int getInterest() { return 0; } } public class SavingsAccount extends Account { public int computeFee(); } public class SavingsAccount extends Account { } public class SavingsAccount extends Account { public int computeFee() { return getInterest()/2; } }

Which implementations

  • f SavingsAccount

are legal? 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 B C Abstract class/method

slide-22
SLIDE 22

Inheritance: more concepts

  • Refactoring
  • Abstract class
  • Template Pattern
  • Protected access
slide-23
SLIDE 23

Uniform Highlighting Technique

slide-24
SLIDE 24

Uniform Highlighting Technique

public 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 method

public 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
slide-25
SLIDE 25

TEMPLATE METHOD Pattern

Context

  • 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.
slide-26
SLIDE 26

TEMPLATE METHOD Pattern

slide-27
SLIDE 27

TEMPLATE METHOD Pattern

drawSelection() draw() translate() SelectableShape draw() translate() CarShape EXAMPLE

slide-28
SLIDE 28

QUIZ

a) b) Template method Which exemplifies Template Method pattern?

1. a 2. b 3. a+b 4. None 5. I don’t know

Method playGame: b.init(); while (!b.done()) b.move(); Method playGame: init(); while (!done()) move();

slide-29
SLIDE 29

Inheritance: more concepts

  • Refactoring
  • Abstract class
  • Template Pattern
  • Protected access
slide-30
SLIDE 30

Compound Shapes

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

shapes

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

  • Advantage: Containment test is free

path.contains(aPoint);

slide-31
SLIDE 31

Access to Superclass Features

  • Why does the HouseShape constructor call add?

public HouseShape() { add(new Rectangle(...)); add(new Triangle(...)); }

  • Why not just

path.append(new Rectangle(...));

  • HouseShape inherits path field
  • HouseShape can't access path
  • path is private to superclass
slide-32
SLIDE 32

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 protected

protected void add(Shape s) // GOOD

  • Protected interface separate from public interface
slide-33
SLIDE 33

Inheritance: Examples

  • Graphics programming
  • Mouse(Motion)Listener
  • Class hierarchy examples
slide-34
SLIDE 34

Graphic Programming with Inheritance

  • Chapter 4: Create drawings by implementing

Icon interface type

  • Now: Form subclass of JComponent

public 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

slide-35
SLIDE 35

Overriding paintComponent

public class SceneComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; for (SceneShape s : shapes) { s.draw(g2); if (s.selected()) s.drawSelection(g2); } } ... private ArrayList<SceneShape> shapes; }

slide-36
SLIDE 36

Inheritance: Examples

  • Graphics programming
  • Mouse(Motion)Listener
  • Class hierarchy examples
slide-37
SLIDE 37

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

slide-38
SLIDE 38

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

slide-39
SLIDE 39

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

slide-40
SLIDE 40

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

slide-41
SLIDE 41

Scene Editor

  • Remove button removes selected items

removeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { scene.removeSelected(); } } );

slide-42
SLIDE 42

QUIZ

paintComponent is misspelled with small ”c” in stead of capital ”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 Overriding?

slide-43
SLIDE 43

Inheritance: Examples

  • Graphics programming
  • Mouse(Motion)Listener
  • Class hierarchy examples
slide-44
SLIDE 44

Hierarchy of Swing Components

slide-45
SLIDE 45

Hierarchy of Geometrical Shapes

slide-46
SLIDE 46

Hierarchy of Exception Classes

slide-47
SLIDE 47

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

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

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

  • ther.translate(dx, dy);

}

  • Remedy: Use aggregation.
  • Circle, Rectangle classes have points
slide-49
SLIDE 49

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
slide-50
SLIDE 50

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
slide-51
SLIDE 51

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

slide-52
SLIDE 52

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 aggregation

public class Stack<T> { ... private ArrayList<T> elements; }

slide-53
SLIDE 53

QUIZ

a) b) d) c)

Queue

is-a versus has-a Operations:

  • Add (at rear)
  • Remove (from front)

We want to implement a queue using an ArrayList. Which UML figure is most appropriate? 1.a 2.b 3.c 4.d 5.I don’t know