Object Roles the Importance of Polymorphism

  • Published on
    21-Mar-2017

  • View
    24

  • Download
    1

Embed Size (px)

Transcript

  • Object Roles and the Importance ofPolymorphism

    (tkorinp@hotmail.com)

  • object Polymorphism

    polymorphism Class

    polymorphism Java

    overridden methods

    polymorphism

  • Object Roles and the Importance ofPolymorphism

    1) Class Types2) Substitutability3) Polymorphism4) Extensibility5) Interfaces6) Extensibility Again7) Distinguishing Subclass8) Summary

  • Class Types

    = =

    type Class

    subclass superclass 'supertype'

    Publication p = new Book();Publication p = new DiscMag();Magazine m = new DiscMag();DiscMag dm = new Magazine();Publication p = new Publication();

  • Substitutability

    Class Subclass

    Superclass

    overriding methods subclass superclass

    subclass

    subclass superclass

    subclass superclass

    Extends

  • Polymorphism

    Subclass

    Superclass superclass subclass

    superclass

    object p sellCopy()

    p

  • Extensibility

    subclass

    sellBook (Book pBook) sellMagazine (Magazine pMagazine) sellDiscMag (DiscMag pDiscMag)

    Polymorphism Superclass sellItem (Publication pPub)

    Subclass Superclass subclass superclass Subclass Superclass

  • Interfaces

    2 subclass superclass (public members)

    subclass superclass ()

    superclass Java

    Extends (inheritance) Implementation (Interfaces)

    subclass is a subtype. plug and socket

  • Interfaces

  • Extensibility Again

    polymorphism

    Interface

    Saleable() without a single change to its code! Class

    Interface

  • Distinguishing Subclasses

    Class object instanceof class

    ( subclass)

    Subclass public void saleType (SaleableItem pSelb){if (pSelb instanceof Publication){ System.out.println("This is a Publication"); }else if (pSelb instanceof Ticket){ System.out.println("This is a Ticket"); }else{ System.out.println("This is a an unknown sale type"); }}

  • Summary

    polymorphism superclass polymorphism

    Class Class object

    superclass subclass Java object

    class class

Recommended

View more >