Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Ten: Inheritance.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
ITM 352 Class inheritance, hierarchies Lecture #.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
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.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Static. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A static method does not operate on an object double dY.
Chapter 3 – Implementing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Java Programming Week 4: Inheritance (Chapter 10).
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
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 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
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:
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter Ten: Inheritance
Chapter 13 - Inheritance.
Lecture 3 John Woodward.
Data Structures and Algorithms revision
CSC 222: Object-Oriented Programming Spring 2017
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Computing with C# and the .NET Framework
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
Packages, Interfaces & Exception Handling
CSC 205 Java Programming II
Chapter Three - Implementing Classes
Chapter 10 – Inheritance Big Java by Cay Horstmann
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Adapted from Java Concepts Companion Slides
JAVA CLASSES.
Presentation transcript:

Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Inheritance is a mechanism for extending existing classes by adding instance variables and methods: class SavingsAccount extends BankAccount { added instance variables new methods } A subclass inherits the methods of its superclass : SavingsAccount collegeFund = new SavingsAccount(10); // Savings account with 10% interest collegeFund.deposit(500); // OK to use BankAccount method with SavingsAccount object Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In subclass, specify added instance variables, added methods, and changed or overridden methods: public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { Constructor implementation } public void addInterest() { Method implementation } Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Instance variables declared in the superclass are present in subclass objects SavingsAccount object inherits the balance instance variable from BankAccount, and gains one additional instance variable, interestRate : Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Implement the new addInterest method: public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A subclass has no access to private instance variables of its superclass Encapsulation: addInterest calls getBalance rather than updating the balance variable of the superclass (variable is private ) Note that addInterest calls getBalance without specifying an implicit parameter (the calls apply to the same object) Inheriting from a class differs from implementing an interface: the subclass inherits behavior from the superclass Inheritance Hierarchies

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1 /** 2 An account that earns interest at a fixed rate. 3 */ 4 public class SavingsAccount extends BankAccount 5 { 6 private double interestRate; 7 8 /** 9 Constructs a bank account with a given interest rate. rate the interest rate 11 */ 12 public SavingsAccount(double rate) 13 { 14 interestRate = rate; 15 } 16 ch09/accounts/SavingsAccount.java Continued

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 17 /** 18 Adds the earned interest to the account balance. 19 */ 20 public void addInterest() 21 { 22 double interest = getBalance() * interestRate / 100; 23 deposit(interest); 24 } 25 } ch09/accounts/SavingsAccount.java (cont.)

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 9.1 Inheritance

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Which instance variables does an object of class SavingsAccount have? Answer: Two instance variables: balance and interestRate. Self Check 9.3

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Name four methods that you can apply to SavingsAccount objects. Answer: deposit, withdraw, getBalance, and addInterest. Self Check 9.4

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. If the class Manager extends the class Employee, which class is the superclass and which is the subclass? Answer: Manager is the subclass; Employee is the superclass. Self Check 9.5

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A subclass has no access to the private instance variables of the superclass: public class SavingsAccount extends BankAccount { public void addInterest() { double interest = getBalance() * interestRate / 100; balance = balance + interest; // Error }... } Common Error: Shadowing Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Beginner’s error: “solve” this problem by adding another instance variable with same name: public class SavingsAccount extends BankAccount { private double balance; // Don’t public void addInterest() { double interest = getBalance() * interestRate / 100; balance = balance + interest; // Compiles but doesn’t // update the correct balance }... } Common Error: Shadowing Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Now the addInterest method compiles, but it doesn’t update the correct balance! Common Error: Shadowing Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A subclass method overrides a superclass method if it has the same name and parameter types as a superclass method When such a method is applied to a subclass object, the overriding method is executed Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Example: deposit and withdraw methods of the CheckingAccount class override the deposit and withdraw methods of the BankAccount class to handle transaction fees: public class BankAccount {... public void deposit(double amount) {... } public void withdraw(double amount) {... } public double getBalance() {... } } public class CheckingAccount extends BankAccount {... public void deposit(double amount) {... } public void withdraw(double amount) {... } public void deductFees() {... } } Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Use the super reserved word to call a method of the superclass: public class CheckingAccount extends BankAccount { public void deposit(double amount) { transactionCount++; // Now add amount to balance super.deposit } Overriding Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Remaining methods of CheckingAccount also invoke a superclass method: public class CheckingAccount extends BankAccount { private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0; private int transactionCount;... public void withdraw(double amount { transactionCount++; // Now subtract amount from balance super.withdraw(amount); } Overriding Methods Continued

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; }... } Overriding Methods (cont.)

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Syntax 9.2 Calling a Superclass Method