Docsity

Unbounded vs. Subtype Polymorphism and Mixins in Object-Oriented Design and Development, Lecture notes of Object Oriented Programming

Toggle sidebar
bg1
Week 2 Object-Oriented Design and Development 1
1
CSC 517
Unbounded vs. Subtype Polymorphism
In a statically typed o-o language like Java or C++, you can declare a
variable in a superclass, then assign a subclass object to that type:
public class Bicycle {
protected int gear;
public void setGear(int nextGear) {
gear = nextGear;
}
}
public class MountainBike extends Bicycle {
protected int seatHeight;
public void setHeight(int newSeatHeight) {
seatHeight = newSeatHeight;
}
}
public class BikeSim {
public static void main() {
...
Bicycle myBike = new MountainBike();
...
myBike.setGear(3);
myBike.setHeight(5);
}
}
Which statement is illegal in the code above? Why?
In most dynamically typed o-o languages, including Ruby, that statement
would be legal. In Ruby, if a method is defined on an object, the object can
respond to the message.
It doesn’t matter what class the object is declared as … in fact, the object
isn’t declared!
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Unbounded vs. Subtype Polymorphism and Mixins in Object-Oriented Design and Development and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Unbounded vs. Subtype Polymorphism

In a statically typed o-o language like Java or C++, you can declare a variable in a superclass, then assign a subclass object to that type: public class Bicycle { protected int gear; public void setGear(int nextGear) { gear = nextGear; } } public class MountainBike extends Bicycle { protected int seatHeight; public void setHeight(int newSeatHeight) { seatHeight = newSeatHeight; } } public class BikeSim { public static void main() { ... Bicycle myBike = new MountainBike(); ... myBike.setGear(3); myBike.setHeight(5); } } Which statement is illegal in the code above? Why? In most dynamically typed o-o languages, including Ruby, that statement would be legal. In Ruby, if a method is defined on an object, the object can respond to the message. It doesn’t matter what class the object is declared as … in fact, the object isn’t declared!

This is called unbounded polymorphism —the polymorphism is not limited by the declared class of the object. In contrast, statically typed o-o languages usually have subtype polymorphism —the compiler checks that the invoked method is defined in the type that the object is declared as. Unbounded polymorphism is related to duck typing , which is discussed in next week’s online lectures.

Modules and Mixins

Modules are a way to group together methods, classes and constants. Modules were introduced to programming languages in the 1970s, to segregate data and operations into different pieces. Ruby’s modules are are similar to namespaces in languages such as C++. What’s a namespace? Of course, whenever you mix namespaces, you have the possibility of name clashes. Say you have a graphics library that contains classes for Window and

(window.rb) and Border (border.rb).

Both window.rb and border.rb have a top method, which gives

the position of the top of the Window and the top of the Border, respectively. An application programmer wants to lay out windows with borders,

and thus loads both window.rb and border.rb into the program.

What’s the problem here? The window functions can go into one module

module Window

def Window.top

end

  • load loads and executes the code every time it is encountered.] Let’s contrast mixins with multiple inheritance in (what languages?). Consider the following code:

module Introspect

def kind

puts "This object is a #{self.class.name}"

end

end

class Animal

include Introspect

def initialize(name)

@name = name

end

end

class Car

include Introspect

def initialize(model)

@model = model

end

end

d = Animal.new("Cat")

c = Car.new("Ferrari")

d.kind # kind method is available through …

c.kind # .. the mixin Introspect

>>This object is a Animal

>>This object is a Car

Exercise: Select the correct method lookup order.

In addition to including modules, classes can also be extended by

modules.

So, what’s the difference between including a module and extending a

class with it?

Note: It is also possible to extend instances, which will add methods of

the module to specific objects (not classes). Comparable A good example of the power of modules is Comparable, from the Ruby library. To use comparable in a class, the class needs to define a method

called <=> (sometimes called “rocket”).

Once we define this method, we get a lot of useful comparison

functions, such as <, >, <=, >=, == and the method between? for

free. What is this like in Java?

Here is an example. Suppose that we have a Line class:

class Line

def initialize(x1, y1, x2, y2)

@x1, @y1, @x2, @y2 = x1, y1, x2, y

end

end

Exercise: Battle of Minions Composing Modules

Enumerable [SAAS §3.7] is a standard mixin, which can be included in

any class.

It has a very useful method inject, which can be used to repeatedly

apply an operation to adjacent elements in a set:

[1, 2, 3, 4, 5].inject {|v, n| v+n }

Many built-in classes include Enumerable, including Array and Range.

('a' .. 'z').inject {|v, n| v+n }

Exercise: Use inject to define a factorial method.

Let’s define a VowelFinder class that includes Enumerable. It will

have an each method for returning successive vowels from a string. This

method yields each time it encounters a vowel.

class VowelFinder

include Enumerable

def initialize(string)

@string = string

end

def each

@string.scan(/[aeiou]/) do |vowel|

yield vowel

end

end

end

Here’s an example of its use:

VowelFinder.new("abacadabra").inject {|v, n| v+n}

Exercise: Use yield to return every third element of an Array.

Reflection

We’ve already seen a few examples of where Ruby programs can discover things about themselves at run time. For example, we have seen calls like

3.14159.methods

Why do we call this “discovering things about [3.14159] at run time”? Reflection allows program entities to discover things about themselves through introspection. For example, an object can ask what its methods are, and a class can tell what its ancestors are. While Java also provides reflection, it does so much more verbosely than Ruby. The related technique of metaprogramming allows one to create new program entities, such as methods or classes, at run time. Why is this called meta programming?

puts [1, 2, 3, 4, 5].length

puts "Hey".class

>> String

Exercise: Test your knowledge of collection operators. Now, time to put it all together and work on a more involved program, a Blackjack game. Exercise: Deck and Player