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

Slides:



Advertisements
Similar presentations
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February
“The task of leadership is not to put greatness into people, but to elicit it, for the greatness is there already.” – John Buchan Thought for the Day.
Mark Dixon, School of Computing SOFT 120Page Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7.
CHAPTER 9 DEFINING CLASSES & CREATING OBJECTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 8 Extending classes with inheritance.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Computer Science I Inheritance Professor Evan Korth New York University.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Chapter 12: Adding Functionality to Your Classes.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Chapter 8 More Object Concepts
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Topic 4 Inheritance.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
Chapter 13 ATM Case Study Part 2: Implementing an Object-Oriented Design Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Advanced Object-Oriented Programming
Inheritance and Polymorphism
Phil Tayco Slide version 1.1 Created Oct 30, 2017
An Introduction to Inheritance
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Inheritance Basics Programming with Inheritance
Object-oriented design for multiple classes
Computer Programming with JAVA
Java Programming, Second Edition
CSC 205 – Java Programming II
Inheritance and Polymorphism
Object Oriented Programming in A Level
Presentation transcript:

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

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

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

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

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

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end (c) 2012 Ophir Frieder et al

class Account def initialize(balance, name, = = = phone_number end def += amount end def -= amount end (c) 2012 Ophir Frieder et al

def display puts "Name: " puts "Phone number: " puts "Balance: " end def transfer(amount, -= amount target_account.deposit(amount) end def status end (c) 2012 Ophir Frieder et al

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end < defines inheritance in Ruby (c) 2012 Ophir Frieder et al

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end Account class is the parent class (c) 2012 Ophir Frieder et al

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end SavingsAccount is the child class (c) 2012 Ophir Frieder et al

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end “ super ” is a call to the parent class’ constructor (c) 2012 Ophir Frieder et al

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end The variables are unique to the SavingsAccount class (c) 2012 Ophir Frieder et al

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 end New method for the child class (c) 2012 Ophir Frieder et al

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 @phone_numbertransfer(amount,targetAmount) display (c) 2012 Ophir Frieder et al

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

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

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 (c) 2012 Ophir Frieder et al

Example 10.2 Cont’d 14 def withdraw(amount) 15 if - amount -= amount 17 else 18 puts "Balance cannot drop below: " 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

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

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) = interest = minimum 8 end 9 10 def accumulate_interest 12 end 13 (c) 2012 Ophir Frieder et al

Example 10.3 Cont’d 14 def withdraw(amount) 15 if - amount 16 super(amount) 17 else 18 puts "Balance cannot drop below: " 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

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