Propagating Functionality with Inheritance Object-Oriented - - PowerPoint PPT Presentation

propagating functionality with inheritance
SMART_READER_LITE
LIVE PREVIEW

Propagating Functionality with Inheritance Object-Oriented - - PowerPoint PPT Presentation

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 Propagating Functionality with Inheritance Object-Oriented Programming in R: S3 & R6 Object-Oriented Programming in R: S3 & R6 thing_factory <- R6Class( "Thing",


slide-1
SLIDE 1 OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Propagating Functionality with Inheritance

slide-2
SLIDE 2 Object-Oriented Programming in R: S3 & R6

📌 ✂

slide-3
SLIDE 3 Object-Oriented Programming in R: S3 & R6 thing_factory <- R6Class( "Thing", private = list( a_field = "a value", another_field = 123 ), public = list( do_something = function(x, y, z) { # do something here } ) )
slide-4
SLIDE 4 Object-Oriented Programming in R: S3 & R6

parent child

the class you inherit from the class that inherits fields and methods

slide-5
SLIDE 5 Object-Oriented Programming in R: S3 & R6 child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory ) , public = list( do_something_else = function() { # more functionality } )
slide-6
SLIDE 6 Object-Oriented Programming in R: S3 & R6
slide-7
SLIDE 7 Object-Oriented Programming in R: S3 & R6

parent child is a

  • a fancy microwave is a microwave
  • not all microwaves are fancy microwaves
slide-8
SLIDE 8 Object-Oriented Programming in R: S3 & R6 > a_thing <- thing_factory$new() > class(a_thing) [1] "Thing" "R6" > inherits(a_thing, "Thing") [1] TRUE > inherits(a_thing, "R6") [1] TRUE
slide-9
SLIDE 9 Object-Oriented Programming in R: S3 & R6 > a_child_thing <- child_thing_factory$new() > class(a_child_thing) [1] "ChildThing" "Thing" "R6" > inherits(a_child_thing, "ChildThing") [1] TRUE > inherits(a_child_thing, "Thing") [1] TRUE > inherits(a_child_thing, "R6") [1] TRUE
slide-10
SLIDE 10 Object-Oriented Programming in R: S3 & R6

Summary

  • Propagate functionality using inheritance
  • Use the inherit arg to R6Class()
  • Children get their parent’s functionality
  • … but the converse is not true
slide-11
SLIDE 11 OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Let’s practice!

slide-12
SLIDE 12 OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Embrace, Extend, Override

slide-13
SLIDE 13 Object-Oriented Programming in R: S3 & R6 thing_factory <- R6Class( "Thing", public = list( do_something = function() { message("the parent do_something method") } ) )
slide-14
SLIDE 14 Object-Oriented Programming in R: S3 & R6 child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory, public = list( do_something = function() { message("the child do_something method") } ) ) , do_something_else = function() { message("the child do_something_else method") }
slide-15
SLIDE 15 Object-Oriented Programming in R: S3 & R6 > a_child_thing <- child_thing_factory$new() > a_child_thing$do_something() the child do_something method
slide-16
SLIDE 16 Object-Oriented Programming in R: S3 & R6

private$ accesses private fields self$ accesses public methods in self super$ accesses public methods in parent

slide-17
SLIDE 17 Object-Oriented Programming in R: S3 & R6 child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory, public = list( do_something = function() { message("the child do_something method") }, do_something_else = function() { message("the child do_something_else method") self$do_something() } ) ) super$do_something()
slide-18
SLIDE 18 Object-Oriented Programming in R: S3 & R6 > a_child_thing <- child_thing_factory$new() > a_child_thing$do_something_else() the child do_something_else method the child do_something method the parent do_something method
slide-19
SLIDE 19 Object-Oriented Programming in R: S3 & R6

Summary

  • Override by giving the same name
  • Extend by giving a new name
  • self$ accesses public methods in self
  • super$ accesses public methods in parent
slide-20
SLIDE 20 OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Let’s practice!

slide-21
SLIDE 21 OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Multiple Levels of Inheritance

slide-22
SLIDE 22 Object-Oriented Programming in R: S3 & R6
slide-23
SLIDE 23 Object-Oriented Programming in R: S3 & R6

fancy microwave microwave high-end microwave

slide-24
SLIDE 24 Object-Oriented Programming in R: S3 & R6 thing_factory <- R6Class( "Thing" ) child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory ) grand_child_thing_factory <- R6Class( "GrandChildThing", inherit = child_thing_factory )
slide-25
SLIDE 25 Object-Oriented Programming in R: S3 & R6 thing_factory <- R6Class( "Thing" ) , public = list( do_something = function() { message("the parent do_something method") } )
slide-26
SLIDE 26 Object-Oriented Programming in R: S3 & R6 child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory ) , public = list( do_something = function() { message("the child do_something method") } )
slide-27
SLIDE 27 Object-Oriented Programming in R: S3 & R6 grand_child_thing_factory <- R6Class( "GrandChildThing", inherit = child_thing_factory, public = list( do_something = function() { message("the grand-child do_something method”) super$do_something() } ) ) super$super$do_something()
slide-28
SLIDE 28 Object-Oriented Programming in R: S3 & R6 > a_grand_child_thing <- grand_child_thing_factory$new() > a_grand_child_thing$do_something() the grand-child do_something method the child do_something method Error in a_grand_child_thing$do_something(): attempt to apply non-function
slide-29
SLIDE 29 Object-Oriented Programming in R: S3 & R6 child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory, public = list( do_something = function() { message("the child do_something method") } ) ) , active = list( super_ = function() super )
slide-30
SLIDE 30 Object-Oriented Programming in R: S3 & R6 grand_child_thing_factory <- R6Class( "GrandChildThing", inherit = child_thing_factory, public = list( do_something = function() { message("the grand-child do_something method") super$do_something() } ) ) super$super_$do_something()
slide-31
SLIDE 31 Object-Oriented Programming in R: S3 & R6 > a_grand_child_thing <- grand_child_thing_factory$new() > a_grand_child_thing$do_something() the grand-child do_something method the child do_something method the parent do_something method
slide-32
SLIDE 32 Object-Oriented Programming in R: S3 & R6

Summary

  • R6 objects can only access their direct parent
  • Intermediate classes can expose their parent
  • Use an active binding named super_
  • super_ should simply return super
slide-33
SLIDE 33 OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Let’s practice!