Download presentation
Presentation is loading. Please wait.
1
Ruby Inheritance and other languages…
2
Class Participation Get out a piece of paper
You’ll be tracing some code (quick exercises) as we go along Also: have RubyInheritance-n.rb files ready to be opened – you can run those to verify your trace is correct
3
Extending Class Behavior
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
4
Inheritance 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 doesn’t include Kernel… so puts etc.) Few methods, useful for wrapper classes
5
Inheritance and instance variables
We know: Every Ruby object has a set of instance variables Instance variables are created when a value is assigned (within a method, often initialize) Instance variables are not just defined as part of the class (i.e., don’t outside any method) 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.
6
Simple Example Technically @name is not inherited
class Person def = name puts "initializing" end class Student < Person def to_s puts "Name: s = Student.new("Cyndi") puts s is not inherited BUT, initialize is called, 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 Ruby-Inheritance-1.rb
7
Instance variables not inherited
TRACE – what is displayed? class Student < Person def to_s puts "Name: end End p = Person.new("Peter") s = Student.new("Cyndi") p.lets s.lets class Person def = name puts "initializing" end def = def lets () puts " ing But we can effectively treat them as if they are – by calling the methods Ruby-Inheritance-1a.rb
8
Inheriting and Overriding
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 don’t know all methods of a class you’re subclassing, you may override a private method accidentally!
9
Language Comparison run RubyInheritance-1.rb
Does Java automatically call the parent ctor? explicitly-call-a-superclass-constructor Compare to C++: superclass-constructor-calling-rules Class participation (add to your paper): In Java when do you need to explicitly call the parent ctor? In C++, why don’t they just use a keyword like super to all the parent ctor? Interesting info – but not exam topics
10
Language Comparison Assume you’re writing a C++ program which has:
parent named Bug, child named Mosquito method named bite exists in parent & child What do you need to do to make sure this is bound dynamically? What happens if this is not bound dynamically? Write a few lines of C++ (just on paper) to illustrate. In case you need a reminder:
11
Big Picture When dealing with an OO language
Inheritance should be part of the language There is typically some way to ensure parent and child variables get initialized, but syntax varies Child classes can usually call parent class methods (otherwise what’s the point) Child classes can usually override the behavior of parent class methods – but you need to think about whether that happens at runtime (late binding) or compile time (early binding)
12
Override parent method
TRACE – what is displayed? me = Person.new("Cyndi") me.greeting you = Student.new("Suzie") you.greeting class Person def = name end def greeting puts "Hi, my name is class Student < Person puts "Hi, I'm a student and my name is Ruby-Inheritance-2.rb
13
Method Visibility public. private. protected Applies only to methods!
methods are public by default. initialize is implicitly private (called by new, error if you try to call directly) private. only visible to other methods of the class (or subclass) implicitly invoked on self (but can’t 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++ Why does protected not have the same meaning as in C++?
14
Method visibility class X # public methods def fn #stuff end
protected :fn def helper private :helper can override visibility, e.g., private_class_method :new private and protected guard against inadvertent use – BUT, with metaprogramming it’s possible to invoke these methods. Compare to Java/C++
15
Simple example p = Person.new("Peter") p.setupEmail("p@x.edu")
class Person def initialize(name) @name = name puts "initializing" end def setup ( ) @ = def lets () puts " ing private :lets p = Person.new("Peter") p.lets Can also put private keyword before function definitions. Will apply to multiple functions. Ruby-Inheritance-2a.rb
16
Abstract Class TRACE class AbstractGreeter def greet puts "#{greeting} #{who}" end def say_hi puts "hi" class WorldGreeter < AbstractGreeter def greeting; "Hello"; end def who; "World"; end WorldGreeter.new.greet # AbstractGreeter.new.greet (error!) AbstractGreeter.new.say_hi abstract methods concrete class: defines all abstract methods of ancesters On your paper: What makes AbstractGreeter an abstract class? How does this compare to Java? C++? Ruby-Inheritance-3.rb
17
Chaining methods TRACE
me = Person.new("Cyndi") me.long_greeting you = Student.new("Suzie", "CS") you.long_greeting class Person def = name end def long_greeting puts "Hi, my name is class Student < Person def initialize(name, major) super(name) # could do just = major super puts "I am studying Super syntax is a little different from Java – how? Ruby-Inheritance-4.rb
18
Class variables - review
When did we use static (class) variables in Java/C++? We’ll use class variables in Ruby for similar purposes. But syntax is different.
19
Class variable example
TRACE 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 class Person def = name = 12 end def show puts "Person: class Student < Person def make_something = 15 puts "Student: and Ruby-Inheritance-5.rb
20
Class Instance Variables
Not covered in csci400, but you may want to explore on your own
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.