Changes

Jump to: navigation, search

Ruby Object Oriented Programming

931 bytes added, 19:50, 28 November 2007
Class Inheritance
== Class Inheritance ==
 
As we mentioned earlier in this chapter, Ruby support single inheritance. This means that a subclass can be created which inherits all the variables and methods of another class. The subclass is then extended to add new methods or variables not available in the superclass.
 
One class inherits from another using the < character. Say, for example, that we want a new kind of BankAccount class. This class needs all the same variables and methods as our original class, but also needs the customer's phone number. To do this we simply inherit from ''BankAccount'' and add the new instance variable:
 
<pre>
class NewBankAccount < BankAccount
 
def customerPhone
@customerPhone
end
 
def customerPhone=( value )
@customerPhone = value
end
 
end
 
We now have a new class, derived from BankAccount. This new subclass has everything the superclass had, plus a new property - the customer's phone number:
 
<pre>

Navigation menu