Chapter 10 Object Inheritance

23
CHAPTER 10 OBJECT INHERITANCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al

description

Chapter 10 Object Inheritance. Introduction to Computer Science Using Ruby. Inheritance. Classes can be defined so as to have relationships with other classes The most basic of these relationships is called inheritance No need to redefine similar parts of classes - PowerPoint PPT Presentation

Transcript of Chapter 10 Object Inheritance

Page 1: Chapter 10  Object Inheritance

CHAPTER 10 OBJECT INHERITANCE Introduction to Computer Science Using Ruby

(c) 2012 Ophir Frieder et al

Page 2: Chapter 10  Object Inheritance

Inheritance

Classes can be defined so as to have relationships with other classes

The most basic of these relationships is called inheritance No need to redefine similar parts of

classes A class can inherit properties from

another class Inheritance can represent the

relationship between a generic ball, a baseball, a tennis ball, and a ping pong ball: they are all spherical objects(c) 2012 Ophir Frieder et al

Page 3: Chapter 10  Object Inheritance

Inheritance

We expand our view of accounts to create both a checking and a savings account

Analyze what they have in common

Have a balance Can withdraw money Can deposit money

Account class:

Attributes Shared:

(c) 2012 Ophir Frieder et al

Page 4: Chapter 10  Object Inheritance

Inheritance

Defines attributes that both types of accounts can use

Defines the similarities in the relationship

Eliminates the need to duplicate common data and methods

Defines the differences i.e., checking and

savings accounts Main differences:

Cannot withdraw beyond the minimum balance from a savings account

Savings account generates interest

Parent Class or Superclass: Child Class or Subclass:

(c) 2012 Ophir Frieder et al

Page 5: Chapter 10  Object Inheritance

Inheritance

The checking and savings account classes will define the differences These are the child class or

subclass The main differences are:

You cannot withdraw beyond the minimum balance from a savings account

A savings account generates interest

(c) 2012 Ophir Frieder et al

Page 6: Chapter 10  Object Inheritance

Example 10.1: Savings Account Version #1

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

(c) 2012 Ophir Frieder et al

Page 7: Chapter 10  Object Inheritance

class Account def initialize(balance, name,

phone_number) @balance = balance @name = name @phone_number = phone_number

end

def deposit(amount) @balance += amount

end

def withdraw(amount) @balance -= amount

end (c) 2012 Ophir Frieder et al

Page 8: Chapter 10  Object Inheritance

def display puts "Name: " + @name puts "Phone number: " +

@phone_number.to_s puts "Balance: " + @balance.to_s

end

def transfer(amount, target_account) @balance -= amount target_account.deposit(amount)

end

def status return @balance

end

end

(c) 2012 Ophir Frieder et al

Page 9: Chapter 10  Object Inheritance

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

< defines inheritance in Ruby

(c) 2012 Ophir Frieder et al

Page 10: Chapter 10  Object Inheritance

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

Account class is the

parent class

(c) 2012 Ophir Frieder et al

Page 11: Chapter 10  Object Inheritance

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

SavingsAccount is the child

class

(c) 2012 Ophir Frieder et al

Page 12: Chapter 10  Object Inheritance

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

“super” is a call to the

parent class’ constructor

(c) 2012 Ophir Frieder et al

Page 13: Chapter 10  Object Inheritance

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

The variables are unique to

the SavingsAccount

class

(c) 2012 Ophir Frieder et al

Page 14: Chapter 10  Object Inheritance

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 end

New method for the

child class

(c) 2012 Ophir Frieder et al

Page 15: Chapter 10  Object Inheritance

Inheritance

The SavingsAccount class can do more than the method it defined It inherits all of the super class’ variables

and methods Table 10.1: SavingsAccount Inherited Summary

(c) 2012 Ophir Frieder et al

Page 16: Chapter 10  Object Inheritance

Inheritance: Polymorphism

Because SavingsAccount is subclass of Account, it can use the transfer method to send funds to an Account object

Does have its limits Cannot use the subclasses properties on a

superclass Subclass has features the superclass does not Cannot use accumulate_interest() method on

an Account object

(c) 2012 Ophir Frieder et al

Page 17: Chapter 10  Object Inheritance

Basic Methods Overriding

It is sometimes convenient to alter methods that already exist in a superclass

The SavingsAccount class needs to make sure the balance does not go below the minimum To achieve this, the

SavingsAccount class will need to override the withdraw method

Needs to define its own withdraw functionality

(c) 2012 Ophir Frieder et al

Page 18: Chapter 10  Object Inheritance

Example 10.2: SavingsAccount Version #2

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name, phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13

(c) 2012 Ophir Frieder et al

Page 19: Chapter 10  Object Inheritance

Example 10.2 Cont’d

14 def withdraw(amount) 15 if (@balance - amount >= @minimum) 16 @balance -= amount 17 else 18 puts "Balance cannot drop below: " +

@minimum.to_s 19 end 20 end 21 end

Withdraw has been

overridden by defining a

method in the subclass with a

new name

(c) 2012 Ophir Frieder et al

Page 20: Chapter 10  Object Inheritance

Accessing the Superclass

In many cases, the overriding methods are similar to the methods they override

Instead of repeating code, we can call the superclass inside an overridden method Simply insert the word super with all the

parameters that are needed

(c) 2012 Ophir Frieder et al

Page 21: Chapter 10  Object Inheritance

Example 10.3: SavingsAccount Version #3

1 require_relative '../chapter_09/account_5.rb' 2 3 class SavingsAccount < Account 4 def initialize(balance, name,

phone_number, interest, minimum) 5 super(balance, name, phone_number) 6 @interest = interest 7 @minimum = minimum 8 end 9 10 def accumulate_interest 11 @balance += @balance * @interest 12 end 13 (c) 2012 Ophir Frieder et al

Page 22: Chapter 10  Object Inheritance

Example 10.3 Cont’d

14 def withdraw(amount) 15 if (@balance - amount >= @minimum) 16 super(amount) 17 else 18 puts "Balance cannot drop below: " +

@minimum.to_s 19 end 20 end 21 end

The difference for this

example is small but can be larger for

more complex programs

(c) 2012 Ophir Frieder et al

Page 23: Chapter 10  Object Inheritance

Summary

Inheritance: classes can be created from other classes and use the resources of the parent class

The parent class or the superclass, defines the relationship with the child class or subclass

Subclasses inherit both data and methods from their parent class

In some cases, methods used by the child class need to be overridden

(c) 2012 Ophir Frieder et al