CSSE 220 Day 17 Inheritance Check out Inheritance from - - PowerPoint PPT Presentation

csse 220 day 17
SMART_READER_LITE
LIVE PREVIEW

CSSE 220 Day 17 Inheritance Check out Inheritance from - - PowerPoint PPT Presentation

CSSE 220 Day 17 Inheritance Check out Inheritance from SVN Discount Subclasses Work in pairs First look at my soluAon and understand how


slide-1
SLIDE 1

CSSE ¡220 ¡Day ¡17 ¡

Inheritance ¡

Check ¡out ¡Inheritance ¡from ¡SVN ¡

slide-2
SLIDE 2

Discount ¡Subclasses ¡

  • Work ¡in ¡pairs ¡
  • First ¡look ¡at ¡my ¡soluAon ¡and ¡understand ¡how ¡

it ¡works ¡

  • Then ¡draw ¡a ¡UML ¡diagram ¡of ¡it ¡
slide-3
SLIDE 3

DiscountSubclasses ¡live ¡coding ¡

slide-4
SLIDE 4

Inheritance ¡

  • SomeAmes ¡a ¡new ¡class ¡is ¡a ¡special ¡

case ¡of ¡the ¡concept ¡represented ¡ by ¡another ¡ ¡

  • Can ¡“borrow” ¡from ¡an ¡exisAng ¡

class, ¡changing ¡just ¡what ¡we ¡need ¡

  • The ¡new ¡class ¡inherits ¡from ¡the ¡

exisAng ¡one: ¡

– all ¡methods ¡ – all ¡instance ¡fields ¡

Q1 ¡

slide-5
SLIDE 5

Examples ¡

  • class SavingsAccount extends BankAccount

– adds ¡interest ¡earning, ¡keeps ¡other ¡traits ¡

  • class Employee extends Person

– adds ¡pay ¡informaAon ¡and ¡methods, ¡keeps ¡other ¡ traits ¡

  • class Manager extends Employee

– adds ¡informaAon ¡about ¡employees ¡managed, ¡ changes ¡the ¡pay ¡mechanism, ¡keeps ¡other ¡traits ¡

slide-6
SLIDE 6

NotaAon ¡and ¡Terminology ¡

  • class SavingsAccount extends BankAccount {

// added fields // added methods }

  • Say ¡“SavingsAccount ¡is ¡a ¡BankAccount” ¡
  • Superclass: ¡BankAccount
  • Subclass: ¡SavingsAccount

Q2 ¡

slide-7
SLIDE 7

Inheritance ¡in ¡UML ¡

The ¡“superest” ¡class ¡ in ¡Java ¡ SAll ¡means ¡“is ¡ a” ¡ Solid ¡line ¡ shows ¡ inheritance ¡

Q3 ¡

slide-8
SLIDE 8

Interfaces ¡vs. ¡Inheritance ¡

  • class ClickHandler implements MouseListener ¡

– ClickHandler ¡promises ¡to ¡implement ¡all ¡the ¡ methods ¡of ¡MouseListener ¡

  • class CheckingAccount extends BankAccount ¡

– CheckingAccount ¡inherits ¡(or ¡overrides) ¡all ¡the ¡ methods ¡of ¡BankAccount ¡

For ¡client ¡code ¡reuse ¡ For ¡implementa2on ¡ code ¡reuse ¡

slide-9
SLIDE 9

Inheritance ¡Run ¡Amok? ¡

slide-10
SLIDE 10

With ¡Methods, ¡Subclasses ¡can: ¡

  • Inherit ¡methods ¡unchanged ¡
  • Override ¡methods ¡

– Declare ¡a ¡new ¡method ¡with ¡same ¡signature ¡to ¡use ¡ instead ¡of ¡superclass ¡method ¡

  • Add ¡enArely ¡new ¡methods ¡not ¡in ¡superclass ¡

Q4 ¡

slide-11
SLIDE 11

With ¡Fields, ¡Subclasses: ¡

  • ALWAYS ¡inherit ¡all ¡fields ¡unchanged ¡
  • Can ¡add ¡enArely ¡new ¡fields ¡not ¡in ¡superclass ¡

DANGER! ¡ ¡Don’t ¡use ¡the ¡ same ¡name ¡as ¡a ¡superclass ¡ field! ¡

Q5 ¡

slide-12
SLIDE 12

Super ¡Calls ¡

  • Calling ¡superclass ¡method: ¡

– super.methodName(args);

  • Calling ¡superclass ¡constructor: ¡

– super(args);

Must ¡be ¡the ¡first ¡line ¡of ¡ the ¡subclass ¡constructor ¡

Q6 ¡

slide-13
SLIDE 13

Polymorphism ¡and ¡Subclasses ¡

  • A ¡subclass ¡instance ¡is ¡a ¡superclass ¡instance ¡

– Polymorphism ¡sAll ¡works! ¡ – BankAccount ba = new CheckingAccount(); ba.deposit(100);

  • But ¡not ¡the ¡other ¡way ¡around! ¡

– CheckingAccount ca = new BankAccount(); ca.deductFees();

  • Why ¡not? ¡

BOOM! ¡

For ¡client ¡code ¡reuse ¡

Q7 ¡

slide-14
SLIDE 14

Another ¡Example ¡

  • Can ¡use: ¡

– public void transfer(double amt, BankAccount o) { this.withdraw(amount);

  • .deposit(amount);

} ¡

in ¡BankAccount ¡

  • To ¡transfer ¡between ¡different ¡accounts: ¡

– SavingsAccount sa = …; – CheckingAccount ca = …; – sa.transfer(100, ca); ¡

slide-15
SLIDE 15

Abstract ¡Classes ¡

  • Hybrid ¡of ¡superclasses ¡and ¡interfaces ¡

– Like ¡regular ¡superclasses: ¡

  • Provide ¡implementaAon ¡of ¡some ¡methods ¡

– Like ¡interfaces ¡

  • Just ¡provide ¡signatures ¡and ¡docs ¡of ¡other ¡methods ¡
  • Can’t ¡be ¡instanAated ¡
  • Example: ¡

– public abstract class BankAccount { /** documentation here */ public abstract void deductFees(); … }

Elided ¡methods ¡as ¡before ¡

Also ¡look ¡at ¡the ¡ code ¡in ¡the ¡ shapes ¡package, ¡ especially ¡ ShapesDemo ¡ ¡ (during ¡or ¡aeer ¡ class) ¡

slide-16
SLIDE 16

Access ¡Modifiers ¡

  • Review ¡

– public—any ¡code ¡can ¡see ¡it ¡ – private—only ¡the ¡class ¡itself ¡can ¡see ¡it ¡

  • Others ¡

– default ¡(i.e., ¡no ¡modifier)—only ¡code ¡ ¡ in ¡the ¡same ¡package ¡can ¡see ¡it ¡

  • good ¡choice ¡for ¡classes ¡

– protected—like ¡default, ¡but ¡ ¡ subclasses ¡also ¡have ¡access ¡

  • someAmes ¡useful ¡for ¡helper ¡methods ¡

Bad ¡for ¡ fields! ¡

Q8 ¡

slide-17
SLIDE 17

Make ¡a ¡shape ¡hierarchy ¡

  • Work ¡in ¡pairs ¡
  • All ¡shapes ¡have ¡an ¡upper ¡lee ¡coordinate, ¡plus ¡width ¡and ¡

height ¡

  • They ¡all ¡have ¡a ¡method ¡to ¡compute ¡their ¡area ¡and ¡perimeter ¡

(hint ¡– ¡abstract) ¡

  • They ¡all ¡have ¡a ¡method ¡printData ¡that ¡prints ¡their ¡height ¡

width ¡area ¡and ¡perimeter ¡(hint ¡– ¡in ¡superclass) ¡

  • Write ¡code ¡for ¡Shape, ¡Rectangle, ¡Circle ¡and ¡test ¡it ¡
  • Then ¡make ¡CoolCircle ¡a ¡non-­‑abstract ¡subclass ¡of ¡Circle ¡which ¡
  • verrides ¡one ¡method ¡to ¡do ¡something ¡different ¡and ¡test ¡it ¡
slide-18
SLIDE 18

WORK ¡TIME ¡

Linear ¡Lights ¡Out ¡ ¡ It's ¡a ¡solo ¡project, ¡but ¡feel ¡free ¡to ¡talk ¡with ¡others ¡as ¡you ¡do ¡it. ¡ ¡ And ¡to ¡ask ¡instructor/assistants ¡for ¡help ¡

Q9-­‑Q10 ¡

slide-19
SLIDE 19

BALLWORLDS ¡INTRODUCTION ¡

Demo ¡ UML ¡Design ¡QuesAons ¡