csse 220 day 17
play

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


  1. CSSE ¡220 ¡Day ¡17 ¡ Inheritance ¡ Check ¡out ¡ Inheritance ¡ from ¡SVN ¡

  2. Discount ¡Subclasses ¡ • Work ¡in ¡pairs ¡ • First ¡look ¡at ¡my ¡soluAon ¡and ¡understand ¡how ¡ it ¡works ¡ • Then ¡draw ¡a ¡UML ¡diagram ¡of ¡it ¡

  3. DiscountSubclasses ¡live ¡coding ¡

  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 ¡

  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 ¡

  6. NotaAon ¡and ¡Terminology ¡ • class SavingsAccount extends BankAccount { // added fields // added methods } • Say ¡“ SavingsAccount ¡ is ¡a ¡ BankAccount ” ¡ • Superclass : ¡ BankAccount • Subclass : ¡ SavingsAccount Q2 ¡

  7. Inheritance ¡in ¡UML ¡ The ¡“superest” ¡class ¡ in ¡Java ¡ Solid ¡line ¡ shows ¡ SAll ¡means ¡“is ¡ inheritance ¡ a” ¡ Q3 ¡

  8. Interfaces ¡vs. ¡Inheritance ¡ • class ClickHandler implements MouseListener ¡ – ClickHandler ¡ promises ¡to ¡implement ¡all ¡the ¡ methods ¡of ¡MouseListener ¡ For ¡ client ¡code ¡reuse ¡ • class CheckingAccount extends BankAccount ¡ – CheckingAccount ¡ inherits ¡(or ¡overrides) ¡all ¡the ¡ methods ¡of ¡BankAccount ¡ For ¡ implementa2on ¡ code ¡reuse ¡

  9. Inheritance ¡Run ¡Amok? ¡

  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 ¡

  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 ¡

  12. Super ¡Calls ¡ • Calling ¡superclass ¡ method : ¡ – super.methodName(args); • Calling ¡superclass ¡ constructor : ¡ – super(args); Must ¡be ¡the ¡first ¡line ¡of ¡ the ¡subclass ¡constructor ¡ Q6 ¡

  13. Polymorphism ¡and ¡Subclasses ¡ • A ¡subclass ¡instance ¡ is ¡a ¡superclass ¡instance ¡ – Polymorphism ¡sAll ¡works! ¡ – BankAccount ba = new CheckingAccount(); ba.deposit(100); For ¡ client ¡code ¡reuse ¡ • But ¡not ¡the ¡other ¡way ¡around! ¡ – CheckingAccount ca = new BankAccount(); ca.deductFees(); • Why ¡not? ¡ BOOM! ¡ Q7 ¡

  14. Another ¡Example ¡ • Can ¡use: ¡ – public void transfer(double amt, BankAccount o) { this.withdraw(amount); o.deposit(amount); } ¡ in ¡BankAccount ¡ • To ¡transfer ¡between ¡different ¡accounts: ¡ – SavingsAccount sa = …; – CheckingAccount ca = …; – sa.transfer(100, ca); ¡

  15. Also ¡look ¡at ¡the ¡ Abstract ¡Classes ¡ code ¡in ¡the ¡ shapes ¡package, ¡ especially ¡ ShapesDemo ¡ ¡ • Hybrid ¡of ¡superclasses ¡and ¡interfaces ¡ (during ¡or ¡aeer ¡ – Like ¡regular ¡superclasses: ¡ class) ¡ • 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 ¡

  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 ¡ Bad ¡for ¡ • good ¡choice ¡for ¡classes ¡ – protected —like ¡default, ¡but ¡ ¡ fields! ¡ subclasses ¡also ¡have ¡access ¡ • someAmes ¡useful ¡for ¡helper ¡methods ¡ Q8 ¡

  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 ¡ overrides ¡one ¡method ¡to ¡do ¡something ¡different ¡and ¡test ¡it ¡

  18. 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 ¡ WORK ¡TIME ¡ Q9-­‑Q10 ¡

  19. Demo ¡ UML ¡Design ¡QuesAons ¡ BALLWORLDS ¡INTRODUCTION ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend