and other languages…. get out a piece of paper you’ll be tracing some code (quick exercises)...

17
Ruby Inheritance and other languages…

Upload: ginger-cox

Post on 17-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1
  • and other languages
  • Slide 2
  • Get out a piece of paper Youll be tracing some code (quick exercises) as we go along
  • Slide 3
  • May create subclasses (inheritance) May include/inherit methods from modules (mix-ins) Clients of class may also extend: Classes are open; any program can add a method Can add a singleton method to an individual object Modules covered next lecture
  • Slide 4
  • Object is the superclass if none specified Every class has a single superclass May have multiple subclasses a hierarchy Syntax: class Student < Person End BasicObject is parent of Object Can create completely separate hierarchy (e.g., BasicObject doesnt include Kernel so puts etc.) Few methods, useful for wrapper classes
  • Slide 5
  • We know: Every Ruby object has a set of instance variables These are not defined by the class! Instance variables are created when a value is assigned But instance variables may be created by other methods! (Since methods are inherited, may still appear to inherit variables but may not!) Therefore, instance variables have nothing to do with inheritance. BUT, if all variables are defined in initialize, inheritance appears to work as expected What are some pros and cons? What about shadowing? On your paper: write a few lines of code to show shadowing in Java.
  • Slide 6
  • class Person def initialize(name) @name = name puts "initializing" end class Student < Person def to_s puts "Name: #{@name}" end s = Student.new("Cyndi") puts s Technically @name is not inherited BUT, initialize is called, so @name is created for the Student object It appears that the variable is inherited An instance variable created in a parent method that is not called by the child will not exist
  • Slide 7
  • class Person def initialize(name) @name = name puts "initializing" end def setupEmail(email) @email = email end def letsEmail() puts "Emailing #{@email}" end class Student < Person def to_s puts "Name: #{@name}" end End p = Person.new("Peter") p.setupEmail("peter@mine s.edu") s = Student.new("Cyndi") p.letsEmail s.letsEmail But we can effectively treat them as if they are by calling the methods TRACE
  • Slide 8
  • Can override methods Methods are bound dynamically (when executed) not statically (when parsed) Methods like to_s and initialize are automatically inherited Caution: if you dont know all methods of a class youre subclassing, you may override a private method accidentally! Caution: class methods can be overridden, so its best to invoke class method with name of class that defines it. Compare to Java what is initialize? Is it inherited?
  • Slide 9
  • class Person def initialize(name) @name = name end def greeting puts "Hi, my name is #{@name}" end class Student < Person def greeting puts "Hi, I'm a student and my name is #{@name}" end me = Person.new("Cyndi") me.greeting you = Student.new("Suzie") you.greeting TRACE
  • Slide 10
  • public. methods are public by default. initialize is implicitly private (called by new) private. only visible to other methods of the class (or subclass) implicitly invoked on self (but cant write self.fn) protected like private, but can be invoked on any instance of the class (e.g., if pass in parameter allows objects of same type to share state) used infrequently Applies only to methods! Variables are automatically encapsulated (private) Constants are public Compare to Java/C++
  • Slide 11
  • class X # public methods def fn #stuff end protected :fn def helper #stuff end private :helper end can override visibility, e.g., private_class_method :new private and protected guard against inadvertent use BUT, with metaprogramming its possible to invoke these methods. Compare to Java/C++
  • Slide 12
  • class Person def initialize(name) @name = name puts "initializing" end def setupEmail(email) @email = email end def letsEmail() puts "Emailing #{@email}" end private :letsEmail end p = Person.new("Peter") p.setupEmail("[email protected]") p.letsEmail Can also put private keyword before function definitions. Will apply to multiple functions.
  • Slide 13
  • class AbstractGreeter def greet puts "#{greeting} #{who}" end def say_hi puts "hi" end class WorldGreeter < AbstractGreeter def greeting; "Hello"; end def who; "World"; end end WorldGreeter.new.greet # AbstractGreeter.new.greet (error!) AbstractGreeter.new.say_hi concrete class: defines all abstract methods of ancesters abstract methods TRACE
  • Slide 14
  • class Person def initialize(name) @name = name end def long_greeting puts "Hi, my name is #{@name}." end class Student < Person def initialize(name, major) super(name) # could do just super @major = major end def long_greeting super puts "I am studying #{@major}." end me = Person.new("Cyndi") me.long_greeting you = Student.new("Suzie", "CS") you.long_greeting super is a little different from Java how? TRACE
  • Slide 15
  • When did we use static (class) variables in Java/C++? Well use class variables in Ruby for similar purposes. But syntax is different.
  • Slide 16
  • class Person def initialize(name) @name = name @@what = 12 end def show puts "Person: #{@@something}" end class Student < Person def make_something @@something = 15 end def show puts "Student: #{@@something} and #{@@what}" end me = Person.new("Cyndi") you = Student.new("Suzie") # creates class variable something you.make_something you.show who = Student.new("Joe") # both Students can access something who.show # parent cannot access something # me.show # error
  • Slide 17
  • http://stackoverflow.com/questions/3802 540/difference-between-class-variables- and-class-instance-variables http://stackoverflow.com/questions/3802 540/difference-between-class-variables- and-class-instance-variables http://blog.codegram.com/2011/4/unde rstanding-class-instance-variables-in- ruby http://blog.codegram.com/2011/4/unde rstanding-class-instance-variables-in- ruby Explore on your own