SlideShare a Scribd company logo
3/25/2014
1
More Ruby programming
Iterators, duck typing, inheritance,
and mixins
Iterators
• 3.times { puts "hi" } # output “hi” 3 times
• [4,6,8].each { puts "hi" } # can "ignore" argument
• y = 7
[4,6,8].each { |x|
y = y + x
puts y
}
What is the value of y?
Iterators
• arr = [4,6,8,10]
arr2 = arr.map { |x| x + 1 }
puts arr2 # output another array,
# each element is the result of the block
• sum = arr.inject { |acc,elt| acc + elt }
puts sum # inject is like fold function
# acc is the initial value, elt is the array element
# result of the block is used as initial value
# to apply the block to the next array element
Iterators
• puts (arr.any? { |elt| elt < 0 })
# prints true if one element is negative
• def foo
eight = yield 4
twelve = yield 6
eight + twelve
end
• puts (foo { |x| x + x } ) # what is the output?
Closure
• cl = lambda {|z| z * y}
q = cl.call(9)
puts q
• def foo2 f
eight = f.call 4
twelve = bar f
eight + twelve
end
def bar f
f.call 6
end
puts (foo2 (lambda { |x| x + x })
Duck typing
• Use an instance that “behaves enough like”
the expected ones.
def double x
x + x
end
– double applies to any class of objects that has a +
method that takes self as argument
3/25/2014
2
Inheritance
• If a class C extends class D, then every
instance of C is also an instance of D
– C inherits the methods of D
– C can add new methods
– C can override inherited methods
– Unlike Java, Ruby fields are not part of a class
definition and therefore, not inherited
Point class
class Point
attr_reader :x, :y
attr_writer :x, :y
def initialize(x,y)
@x = x
@y = y
end
def distFromOrigin
Math.sqrt(x * x + y * y) # uses getter methods
end
end
ColorPoint extends Point
class ColorPoint < Point
attr_reader :color
attr_writer :color
# (say p.color = "green" rather than needing
# p.myColorSetter("green") which does @color="green" in its body)
def initialize(x,y,c="clear") # or could skip this and color starts unset
super(x,y)
@color = c
end
end
3D point extends point
class ThreeDPoint < Point
attr_reader :z
attr_writer :z
def initialize(x,y,z)
super(x,y)
@z = z
end
def distFromOrigin
d = super
Math.sqrt(d * d + z * z)
end
end
Polar point extends point
class PolarPoint < Point
def initialize (r, theta)
@r = r
@theta = theta
end
def x
@r * Math.cos(@theta)
end
def y
@r * Math.sin(@theta)
end
…
# distFromOrigin already works!!!
end
Polar point extends point
class PolarPoint < Point
…
def x= a
b = y # avoids multiple calls to y method
@theta = Math.atan (b / a)
@r = Math.sqrt(a*a + b*b)
self
end
def y= b
a = x # avoid multiple calls to x method
@theta = Math.atan (b / a)
@r = Math.sqrt (a*a + b*b)
self
end
# distFromOrigin already works!!!
end
3/25/2014
3
Dynamic dispatch
def distFromOrigin
Math.sqrt(x * x + y * y) # uses getter methods
end
Math.sqrt(self.x() * self.x() + self.y() * self.y())
This method inherited from Point class still works
because the method “x” and “y” are overridden
in the subclass PolarPoint
Multiple inheritance, interfaces, and
mixins
• Languages (C++) with multiple inheritance let one class
extend multiple other classes
– Most powerful option
– Has semantic problems
– Java and Ruby do not use it
• Interfaces: Java has single inheritance but a Java class can
implement multiple interfaces
– An interface defines method signatures but not implementation
• Mixins: Ruby allows a class to have only one super class but
can include any number of mixins
– Mixin is “just a pile of methods”
– Mixin provides behavior while interface only provides types,
which is not an issue for dynamic languages such as Ruby
Multiple Inheritance
• In some languages (such as C++) a class can
have more than one base class
• Seems simple at first: just inherit fields and
methods from all the base classes
• For example: a multifunction printer
MultiFunction
Printer Copier Scanner Fax
Collision Problem
• The different base classes are unrelated,
and may not have been designed to be
combined
• Scanner and Fax might both have a
method named transmit
• When MultiFunction.transmit is
called, what should happen?
MultiFunction
Printer Copier Scanner Fax
Diamond Problem
• A class may inherit from the same base
class through more than one path
• If A defines a field x, then B has one and so
does C
• Does D get two of them?
D
B C
A
Solvable, But…
• A language that supports multiple inheritance
must have mechanisms for handling these
problems
• Not all that tricky
• The question is, is the additional power worth
the additional language complexity?
• Java’s designers did not think so
3/25/2014
4
Living Without Multiple Inheritance
• One benefit of multiple inheritance is that a
class can have several unrelated types (like
Copier and Fax)
• This can be done in Java by using interfaces:
a class can implement any number of
interfaces
• Another benefit is inheriting
implementation from multiple base classes
• This is harder to accomplish with Java
public class MultiFunction {
private Printer myPrinter;
private Copier myCopier;
private Scanner myScanner;
private Fax myFax;
public void copy() {
myCopier.copy();
}
public void transmitScanned() {
myScanner.transmit();
}
public void sendFax() {
myFax.transmit();
}
…
}
Forwarding
Interfaces
• A method prototype just gives the method
name and type—no method body
• An interface in Java is a collection of
method prototypes
public interface Drawable {
void show(int xPos, int yPos);
void hide();
}
Implementing Interfaces
• A class can declare that it implements a
particular interface
• Then it must provide public method
definitions that match those in the interface
Examples
public class Icon implements Drawable {
public void show(int x, int y) {
… method body …
}
public void hide() {
… method body …
}
…more methods and fields…
}
public class Square implements Drawable, Scalable {
… all required methods of all interfaces implemented …
}
Why Use Interfaces?
• An interface can be implemented by many
classes:
• Interface name can be used as a reference
type:
public class Window implements Drawable …
public class MousePointer implements Drawable …
public class Oval implements Drawable …
Drawable d;
d = new Icon("i1.gif");
d.show(0,0);
d = new Oval(20,30);
d.show(0,0);
3/25/2014
5
Polymorphism With Interfaces
• Class of object referred to by d is not
known at compile time
• It is some class that implements
Drawable, so it has show and hide
methods that can be called
static void flashoff(Drawable d, int k) {
for (int i = 0; i < k; i++) {
d.show(0,0);
d.hide();
}
}
A More Complete Example
• A Worklist interface for a collection of
String objects
• Can be added to, removed from, and tested
for emptiness
public interface Worklist {
/**
* Add one String to the worklist.
* @param item the String to add
*/
void add(String item);
/**
* Test whether there are more elements in the
* worklist: that is, test whether more elements
* have been added than have been removed.
* @return true iff there are more elements
*/
boolean hasMore();
/**
* Remove one String from the worklist and return
* it. There must be at least one element in the
* worklist.
* @return the String item removed
*/
String remove();
}
Interface Documentation
• Comments are especially important in an
interface, since there is no code to help the
reader understand what each method is
supposed to do
• Worklist interface does not specify
ordering: could be a stack, a queue, or
something else
• We will do an implementation as a stack,
implemented using linked lists
/**
* A Node is an object that holds a String and a link
* to the next Node. It can be used to build linked
* lists of Strings.
*/
public class Node {
private String data; // Each node has a String...
private Node link; // and a link to the next Node
/**
* Node constructor.
* @param theData the String to store in this Node
* @param theLink a link to the next Node
*/
public Node(String theData, Node theLink) {
data = theData;
link = theLink;
}
3/25/2014
6
/**
* Accessor for the String data stored in this Node.
* @return our String item
*/
public String getData() {
return data;
}
/**
* Accessor for the link to the next Node.
* @return the next Node
*/
public Node getLink() {
return link;
}
}
/**
* A Stack is an object that holds a collection of
* Strings.
*/
public class Stack implements Worklist {
private Node top = null; // top Node in the stack
/**
* Push a String on top of this stack.
* @param data the String to add
*/
public void add(String data) {
top = new Node(data,top);
}
/**
* Test whether this stack has more elements.
* @return true if this stack is not empty
*/
public boolean hasMore() {
return (top!=null);
}
/**
* Pop the top String from this stack and return it.
* This should be called only if the stack is
* not empty.
* @return the popped String
*/
public String remove() {
Node n = top;
top = n.getLink();
return n.getData();
}
}
A Test
• Output: The cut worm forgives the plow.
• Other implementations of Worklist are
possible: Queue, PriorityQueue, etc.
Worklist w;
w = new Stack();
w.add("the plow.");
w.add("forgives ");
w.add("The cut worm ");
System.out.print(w.remove());
System.out.print(w.remove());
System.out.println(w.remove());
Mixins
• Ruby mixins are somewhere between multiple
inheritance and interfaces
– They provide actual code for classes to include them but
they are not classes themselves
– Do not have constructors or a separate notion of fields
module Color
attr_accessor :color
def darken
self.color = "dark " + self.color
end
end
Include a mixin in class definition
module Color
attr_accessor :color
def darken
self.color = "dark " + self.color
end
end
class ColorPoint < Point
include Color
end
3/25/2014
7
Method look up
• obj.m # obj is an instance of the class C
– Look in C for method m first
– Then look in the mixins included in C
• Later ones shadow earlier ones
– Look in C’s superclass
– Look in C’s superclass’ mixins
– Look in C’s super-superclass
– Continue until m is found or reach the Object class
Mixin may call hook methods
module Doubler
def double
self + self # uses self’s + method, not defined in Doubler
end
end
class AnotherPoint
attr_accessor :x, :y
include Doubler
def + other # add two points
ans = AnotherPoint.new
ans.x = self.x + other.x
ans.y = self.y + other.y
ans
end
end
class String
include Doubler
end
Convenient Ruby mixins
• Both Enumerable and Comparable are mixins in
Ruby
• Comparable provides =, !=, >, >=, <, and <=
– Assume the classes that include comparable has the
method <=>
• a <=> b < 0 if a < b
• a <=> b > 0 if a > b
• a <=> b = 0 if a == b
– A class like Integer only needs to define <=> method
and then include Comparable to have a bunch of
methods for comparison
An example use of Comparable
class Name
attr_accessor :first, :middle, :last
include Comparable
def initialize(first,last,middle="")
@first = first
@last = last
@middle = middle
end
def <=> other
l = @last <=> other.last # <=> defined on strings
return l if l != 0
f = @first <=> other.first
return f if f != 0
@middle <=> other.middle
end
end
Define Comparable methods
def > other
(self <=> other) > 0
end
Enumerable module
• Enumerable defines methods such as any?,
map, and inject
– Assume the enumerable classes have “each”
method
– Array class defines “each” method and includes
Enumerable mixin
3/25/2014
8
Enumerable example
class MyRange
include Enumerable
def initialize(low,high)
@low = low
@high = high
end
def each
i=@low
while i <= @high
yield i
i=i+1
end
end
end
Enumerable example
class MyRange
include Enumerable
def initialize(low,high)
@low = low
@high = high
end
def each
i=@low
while i <= @high
yield i
i=i+1
end
end
end
MyRange.new(4,8).inject{|x,y| x+y}
Enumerable example
class MyRange
include Enumerable
def initialize(low,high)
@low = low
@high = high
end
def each
i=@low
while i <= @high
yield i
i=i+1
end
end
end
MyRange.new(4,8).inject{|x,y| x+y}
def map
arr = []
each {|x| arr.push x }
arr
end

More Related Content

What's hot (19)

PDF
Scala Paradigms
Tom Flaherty
 
PDF
Demystifying functional programming with Scala
Denis
 
PPT
Comp102 lec 7
Fraz Bakhsh
 
PDF
First-Class Patterns
John De Goes
 
PPT
C# programming
umesh patil
 
PDF
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
PPTX
Java introduction
Samsung Electronics Egypt
 
PDF
Hey! There's OCaml in my Rust!
Kel Cecil
 
PPSX
String and string manipulation x
Shahjahan Samoon
 
PPTX
Python language data types
Hoang Nguyen
 
PPTX
Linq Introduction
Neeraj Kaushik
 
PPTX
Java Foundations: Methods
Svetlin Nakov
 
PDF
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
PPTX
Scala 3 Is Coming: Martin Odersky Shares What To Know
Lightbend
 
PDF
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh Waghela
 
PPT
OOP Core Concept
Rays Technologies
 
PDF
O caml2014 leroy-slides
OCaml
 
PPT
Java Generics
jeslie
 
Scala Paradigms
Tom Flaherty
 
Demystifying functional programming with Scala
Denis
 
Comp102 lec 7
Fraz Bakhsh
 
First-Class Patterns
John De Goes
 
C# programming
umesh patil
 
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
Java introduction
Samsung Electronics Egypt
 
Hey! There's OCaml in my Rust!
Kel Cecil
 
String and string manipulation x
Shahjahan Samoon
 
Python language data types
Hoang Nguyen
 
Linq Introduction
Neeraj Kaushik
 
Java Foundations: Methods
Svetlin Nakov
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Ganesh Samarthyam
 
Scala 3 Is Coming: Martin Odersky Shares What To Know
Lightbend
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh Waghela
 
OOP Core Concept
Rays Technologies
 
O caml2014 leroy-slides
OCaml
 
Java Generics
jeslie
 

Viewers also liked (16)

PDF
Elk Canada
fruitmax
 
PPT
Lan2
WPunyawong
 
PPT
Get Multiple Regression Assignment Help
HelpWithAssignment.com
 
PPTX
Satyam Scam
Dheeraj Singla
 
PPT
Organizational Change
Corey Vorthmann
 
PPT
Personal Memoir Slideshow
Josh Maroney
 
DOC
Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
fruitmax
 
PDF
Horario 2012 1_electronica_yopal (1)
andrea botia
 
PDF
"営業マン"な自社サイトを作る
Digical Media
 
PPT
Nuliv An Overview Of Nu Liv Science 2010 6 21
fruitmax
 
PDF
System Programming - Threading
HelpWithAssignment.com
 
PDF
Horario 2012_ Ingeniería Electrónica
andrea botia
 
PDF
Fundamentals of Transport Phenomena ChE 715
HelpWithAssignment.com
 
PPT
New England Business Expo 2009
Dennys_Catering
 
PDF
株式会社デジカルメディア事業部 サービス紹介 Ver1.0
Digical Media
 
PPTX
Server and Client side comparision
Stew Duncan
 
Elk Canada
fruitmax
 
Get Multiple Regression Assignment Help
HelpWithAssignment.com
 
Satyam Scam
Dheeraj Singla
 
Organizational Change
Corey Vorthmann
 
Personal Memoir Slideshow
Josh Maroney
 
Astra Gin, A Nu Liv Science Nutraceutical Ingredient For Increasing Nutrient ...
fruitmax
 
Horario 2012 1_electronica_yopal (1)
andrea botia
 
"営業マン"な自社サイトを作る
Digical Media
 
Nuliv An Overview Of Nu Liv Science 2010 6 21
fruitmax
 
System Programming - Threading
HelpWithAssignment.com
 
Horario 2012_ Ingeniería Electrónica
andrea botia
 
Fundamentals of Transport Phenomena ChE 715
HelpWithAssignment.com
 
New England Business Expo 2009
Dennys_Catering
 
株式会社デジカルメディア事業部 サービス紹介 Ver1.0
Digical Media
 
Server and Client side comparision
Stew Duncan
 
Ad

Similar to Ruby Programming Assignment Help (20)

PPT
04 inheritance
Pondugala Sowjanya
 
PPTX
01. design pattern
MD Sayem Ahmed
 
PPTX
L04 Software Design 2
Ólafur Andri Ragnarsson
 
PDF
Ruby — An introduction
Gonçalo Silva
 
PDF
L5
lksoo
 
DOC
Interface Vs Abstact
Anand Kumar Rajana
 
PDF
Ruby seen from a C# developer
Codemotion
 
PDF
Ruby seen by a C# developer
Emanuele DelBono
 
PPTX
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
PPT
9 abstract interface
Abhijit Gaikwad
 
PPTX
Advanced-java.pptx
MiltonMolla1
 
PDF
Ruby1_full
tutorialsruby
 
PDF
Ruby1_full
tutorialsruby
 
PPT
Polymorphism
Kumar
 
PDF
javainterface
Arjun Shanka
 
PPTX
Java interface
Md. Tanvir Hossain
 
PDF
advanced java.pdf
Ali Bozkurt
 
PDF
W java81
rasikow
 
PPT
Java interface
Arati Gadgil
 
PPTX
Java session2
Rajeev Kumar
 
04 inheritance
Pondugala Sowjanya
 
01. design pattern
MD Sayem Ahmed
 
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Ruby — An introduction
Gonçalo Silva
 
L5
lksoo
 
Interface Vs Abstact
Anand Kumar Rajana
 
Ruby seen from a C# developer
Codemotion
 
Ruby seen by a C# developer
Emanuele DelBono
 
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
9 abstract interface
Abhijit Gaikwad
 
Advanced-java.pptx
MiltonMolla1
 
Ruby1_full
tutorialsruby
 
Ruby1_full
tutorialsruby
 
Polymorphism
Kumar
 
javainterface
Arjun Shanka
 
Java interface
Md. Tanvir Hossain
 
advanced java.pdf
Ali Bozkurt
 
W java81
rasikow
 
Java interface
Arati Gadgil
 
Java session2
Rajeev Kumar
 
Ad

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 

Ruby Programming Assignment Help

  • 1. 3/25/2014 1 More Ruby programming Iterators, duck typing, inheritance, and mixins Iterators • 3.times { puts "hi" } # output “hi” 3 times • [4,6,8].each { puts "hi" } # can "ignore" argument • y = 7 [4,6,8].each { |x| y = y + x puts y } What is the value of y? Iterators • arr = [4,6,8,10] arr2 = arr.map { |x| x + 1 } puts arr2 # output another array, # each element is the result of the block • sum = arr.inject { |acc,elt| acc + elt } puts sum # inject is like fold function # acc is the initial value, elt is the array element # result of the block is used as initial value # to apply the block to the next array element Iterators • puts (arr.any? { |elt| elt < 0 }) # prints true if one element is negative • def foo eight = yield 4 twelve = yield 6 eight + twelve end • puts (foo { |x| x + x } ) # what is the output? Closure • cl = lambda {|z| z * y} q = cl.call(9) puts q • def foo2 f eight = f.call 4 twelve = bar f eight + twelve end def bar f f.call 6 end puts (foo2 (lambda { |x| x + x }) Duck typing • Use an instance that “behaves enough like” the expected ones. def double x x + x end – double applies to any class of objects that has a + method that takes self as argument
  • 2. 3/25/2014 2 Inheritance • If a class C extends class D, then every instance of C is also an instance of D – C inherits the methods of D – C can add new methods – C can override inherited methods – Unlike Java, Ruby fields are not part of a class definition and therefore, not inherited Point class class Point attr_reader :x, :y attr_writer :x, :y def initialize(x,y) @x = x @y = y end def distFromOrigin Math.sqrt(x * x + y * y) # uses getter methods end end ColorPoint extends Point class ColorPoint < Point attr_reader :color attr_writer :color # (say p.color = "green" rather than needing # p.myColorSetter("green") which does @color="green" in its body) def initialize(x,y,c="clear") # or could skip this and color starts unset super(x,y) @color = c end end 3D point extends point class ThreeDPoint < Point attr_reader :z attr_writer :z def initialize(x,y,z) super(x,y) @z = z end def distFromOrigin d = super Math.sqrt(d * d + z * z) end end Polar point extends point class PolarPoint < Point def initialize (r, theta) @r = r @theta = theta end def x @r * Math.cos(@theta) end def y @r * Math.sin(@theta) end … # distFromOrigin already works!!! end Polar point extends point class PolarPoint < Point … def x= a b = y # avoids multiple calls to y method @theta = Math.atan (b / a) @r = Math.sqrt(a*a + b*b) self end def y= b a = x # avoid multiple calls to x method @theta = Math.atan (b / a) @r = Math.sqrt (a*a + b*b) self end # distFromOrigin already works!!! end
  • 3. 3/25/2014 3 Dynamic dispatch def distFromOrigin Math.sqrt(x * x + y * y) # uses getter methods end Math.sqrt(self.x() * self.x() + self.y() * self.y()) This method inherited from Point class still works because the method “x” and “y” are overridden in the subclass PolarPoint Multiple inheritance, interfaces, and mixins • Languages (C++) with multiple inheritance let one class extend multiple other classes – Most powerful option – Has semantic problems – Java and Ruby do not use it • Interfaces: Java has single inheritance but a Java class can implement multiple interfaces – An interface defines method signatures but not implementation • Mixins: Ruby allows a class to have only one super class but can include any number of mixins – Mixin is “just a pile of methods” – Mixin provides behavior while interface only provides types, which is not an issue for dynamic languages such as Ruby Multiple Inheritance • In some languages (such as C++) a class can have more than one base class • Seems simple at first: just inherit fields and methods from all the base classes • For example: a multifunction printer MultiFunction Printer Copier Scanner Fax Collision Problem • The different base classes are unrelated, and may not have been designed to be combined • Scanner and Fax might both have a method named transmit • When MultiFunction.transmit is called, what should happen? MultiFunction Printer Copier Scanner Fax Diamond Problem • A class may inherit from the same base class through more than one path • If A defines a field x, then B has one and so does C • Does D get two of them? D B C A Solvable, But… • A language that supports multiple inheritance must have mechanisms for handling these problems • Not all that tricky • The question is, is the additional power worth the additional language complexity? • Java’s designers did not think so
  • 4. 3/25/2014 4 Living Without Multiple Inheritance • One benefit of multiple inheritance is that a class can have several unrelated types (like Copier and Fax) • This can be done in Java by using interfaces: a class can implement any number of interfaces • Another benefit is inheriting implementation from multiple base classes • This is harder to accomplish with Java public class MultiFunction { private Printer myPrinter; private Copier myCopier; private Scanner myScanner; private Fax myFax; public void copy() { myCopier.copy(); } public void transmitScanned() { myScanner.transmit(); } public void sendFax() { myFax.transmit(); } … } Forwarding Interfaces • A method prototype just gives the method name and type—no method body • An interface in Java is a collection of method prototypes public interface Drawable { void show(int xPos, int yPos); void hide(); } Implementing Interfaces • A class can declare that it implements a particular interface • Then it must provide public method definitions that match those in the interface Examples public class Icon implements Drawable { public void show(int x, int y) { … method body … } public void hide() { … method body … } …more methods and fields… } public class Square implements Drawable, Scalable { … all required methods of all interfaces implemented … } Why Use Interfaces? • An interface can be implemented by many classes: • Interface name can be used as a reference type: public class Window implements Drawable … public class MousePointer implements Drawable … public class Oval implements Drawable … Drawable d; d = new Icon("i1.gif"); d.show(0,0); d = new Oval(20,30); d.show(0,0);
  • 5. 3/25/2014 5 Polymorphism With Interfaces • Class of object referred to by d is not known at compile time • It is some class that implements Drawable, so it has show and hide methods that can be called static void flashoff(Drawable d, int k) { for (int i = 0; i < k; i++) { d.show(0,0); d.hide(); } } A More Complete Example • A Worklist interface for a collection of String objects • Can be added to, removed from, and tested for emptiness public interface Worklist { /** * Add one String to the worklist. * @param item the String to add */ void add(String item); /** * Test whether there are more elements in the * worklist: that is, test whether more elements * have been added than have been removed. * @return true iff there are more elements */ boolean hasMore(); /** * Remove one String from the worklist and return * it. There must be at least one element in the * worklist. * @return the String item removed */ String remove(); } Interface Documentation • Comments are especially important in an interface, since there is no code to help the reader understand what each method is supposed to do • Worklist interface does not specify ordering: could be a stack, a queue, or something else • We will do an implementation as a stack, implemented using linked lists /** * A Node is an object that holds a String and a link * to the next Node. It can be used to build linked * lists of Strings. */ public class Node { private String data; // Each node has a String... private Node link; // and a link to the next Node /** * Node constructor. * @param theData the String to store in this Node * @param theLink a link to the next Node */ public Node(String theData, Node theLink) { data = theData; link = theLink; }
  • 6. 3/25/2014 6 /** * Accessor for the String data stored in this Node. * @return our String item */ public String getData() { return data; } /** * Accessor for the link to the next Node. * @return the next Node */ public Node getLink() { return link; } } /** * A Stack is an object that holds a collection of * Strings. */ public class Stack implements Worklist { private Node top = null; // top Node in the stack /** * Push a String on top of this stack. * @param data the String to add */ public void add(String data) { top = new Node(data,top); } /** * Test whether this stack has more elements. * @return true if this stack is not empty */ public boolean hasMore() { return (top!=null); } /** * Pop the top String from this stack and return it. * This should be called only if the stack is * not empty. * @return the popped String */ public String remove() { Node n = top; top = n.getLink(); return n.getData(); } } A Test • Output: The cut worm forgives the plow. • Other implementations of Worklist are possible: Queue, PriorityQueue, etc. Worklist w; w = new Stack(); w.add("the plow."); w.add("forgives "); w.add("The cut worm "); System.out.print(w.remove()); System.out.print(w.remove()); System.out.println(w.remove()); Mixins • Ruby mixins are somewhere between multiple inheritance and interfaces – They provide actual code for classes to include them but they are not classes themselves – Do not have constructors or a separate notion of fields module Color attr_accessor :color def darken self.color = "dark " + self.color end end Include a mixin in class definition module Color attr_accessor :color def darken self.color = "dark " + self.color end end class ColorPoint < Point include Color end
  • 7. 3/25/2014 7 Method look up • obj.m # obj is an instance of the class C – Look in C for method m first – Then look in the mixins included in C • Later ones shadow earlier ones – Look in C’s superclass – Look in C’s superclass’ mixins – Look in C’s super-superclass – Continue until m is found or reach the Object class Mixin may call hook methods module Doubler def double self + self # uses self’s + method, not defined in Doubler end end class AnotherPoint attr_accessor :x, :y include Doubler def + other # add two points ans = AnotherPoint.new ans.x = self.x + other.x ans.y = self.y + other.y ans end end class String include Doubler end Convenient Ruby mixins • Both Enumerable and Comparable are mixins in Ruby • Comparable provides =, !=, >, >=, <, and <= – Assume the classes that include comparable has the method <=> • a <=> b < 0 if a < b • a <=> b > 0 if a > b • a <=> b = 0 if a == b – A class like Integer only needs to define <=> method and then include Comparable to have a bunch of methods for comparison An example use of Comparable class Name attr_accessor :first, :middle, :last include Comparable def initialize(first,last,middle="") @first = first @last = last @middle = middle end def <=> other l = @last <=> other.last # <=> defined on strings return l if l != 0 f = @first <=> other.first return f if f != 0 @middle <=> other.middle end end Define Comparable methods def > other (self <=> other) > 0 end Enumerable module • Enumerable defines methods such as any?, map, and inject – Assume the enumerable classes have “each” method – Array class defines “each” method and includes Enumerable mixin
  • 8. 3/25/2014 8 Enumerable example class MyRange include Enumerable def initialize(low,high) @low = low @high = high end def each i=@low while i <= @high yield i i=i+1 end end end Enumerable example class MyRange include Enumerable def initialize(low,high) @low = low @high = high end def each i=@low while i <= @high yield i i=i+1 end end end MyRange.new(4,8).inject{|x,y| x+y} Enumerable example class MyRange include Enumerable def initialize(low,high) @low = low @high = high end def each i=@low while i <= @high yield i i=i+1 end end end MyRange.new(4,8).inject{|x,y| x+y} def map arr = [] each {|x| arr.push x } arr end