# Classes and superclasses

**URL:** <https://www.ruby-forum.com/t/classes-and-superclasses/126680>\
**Category:** Ruby\
**Created:** [January 21, 2008, 11:01am UTC](https://www.ruby-forum.com/t/classes-and-superclasses/126680 "2008-01-21T11:01:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSSSSrg\_Hagmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@JSSSSrg\_Hagmann](https://www.ruby-forum.com/u/JSSSSrg_Hagmann)\
**Post date:** [January 21, 2008, 11:01am UTC](https://www.ruby-forum.com/t/classes-and-superclasses/126680/1 "2008-01-21T11:01:10Z")

</div>

Dear list members,

A beginner’s question:

I wrote a simple programme that assembles exams, choosing the questions  
from a database according to certain criteria. The programme does what I  
want, but I realised there are certain basic things I don’t understand,  
e.g.:

My programme has classes analogous to class Song and class SongList in  
the pickaxe book (class Question and class QuestionList).  
Class QuestionList includes the definition  
def length  
@questions.length  
end

and that, of course, works.

When, instead of that definition, I make class QuestionList a subclass  
of Array (QuestionList \< Array), asking for the length doesn’t give me  
an error, but returns a length of 0. So “length” doesn’t know what to do  
with an instance of QuestionList, although the latter class includes

def initialize  
@questions = Array.new  
end  
and is now a subclass of “Array”.

## Why not? Thanks for answers, Jörg

Prof.Dr.med. Jörg Hagmann-Zanolari  
Institute of Biochemistry and Genetics  
Centre of Biomedicine, University of Basel  
Mattenstrasse 28  
CH-4058 Basel  
Switzerland  
Phone +41 (0)61 267 3565

---

<div class="post-metadata">

**Author:** ![JSSSSrg\_Hagmann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@JSSSSrg\_Hagmann](https://www.ruby-forum.com/u/JSSSSrg_Hagmann)\
**Post date:** [January 21, 2008, 11:52am UTC](https://www.ruby-forum.com/t/classes-and-superclasses/126680/2 "2008-01-21T11:52:52Z")

</div>

2008/1/21, Jörg Hagmann [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> the pickaxe book (class Question and class QuestionList).  
> with an instance of QuestionList, although the latter class includes
> 
> def initialize  
> @questions = Array.new  
> end  
> and is now a subclass of “Array”.
> 
> Why not? Thanks for answers, Jörg

You mixed “inheritance” and “delegation”. The definition of  
#initialize you show above creates a member variable of class Array.  
If your class inherits from Array you get two arrays, i.e. via  
inheritance and via delegation. This is usually a bad idea - you  
either want one or the other. With Array and Hash most of the time  
delegation is better. If you use inheritance you should keep in mind  
that this relationship between classes is also called “is-a”  
relationship, i.e. you really get all the Array functionality. This  
is often not what is wanted, because client code can manipulate the  
object like an Array which might break your code in the subclass (for  
example if that code relies on elements being of a particular type).

Kind regards

robert
