chapter 13 inheritance. an introduction to inheritance inheritance: extend classes by adding methods...

43
Chapter 13 Inheritance

Upload: kelly-manning

Post on 18-Dec-2015

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Chapter 13

Inheritance

Page 2: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

An Introduction to Inheritance

• Inheritance: extend classes by adding methods and fields (variables)

• Example: Savings account = bank account with interest

• “extends BankAccount” means that SavingsAccount inherits from BankAccount (a SavingsAccount is a type of BankAccount)

class SavingsAccount extends BankAccount{ new methods new instance fields }

Page 3: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

An Introduction to Inheritance

• SavingsAccount automatically inherits all methods and instance fields of BankAccount

• Extended class = superclass (BankAccount), extending class = subclass (Savings)

Continued…

SavingsAccount collegeFund = new SavingsAccount(10);

// Savings account with 10% interest

collegeFund.deposit(500);

// OK to use BankAccount method with SavingsAccount object

Page 4: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

An Introduction to Inheritance• Inheriting from class is not the same as

implementing an interface.

With an interface, you promise to implement the method names’ listed in the interface.

With inheritance, the subclass inherits and contains all the methods (behavior) and all the variables (state) from the super class.

• One advantage of inheritance is code reuse: we don’t have to re-write superclass methods and variables in a subclass.

Page 5: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

An Inheritance Diagram• Every class extends the Object class either directly or indirectly.

• Every SavingsAccount object is a member of the SavingsAccount class (and so has methods and variables from that class)

• Every SavingsAccount object is also a member of the BankAccount class (has methods and variables from that class)

• Every SavingsAccount object is implicitly a member of the Object class (has methods and variables from that class)

Page 6: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

An Introduction to Inheritance• In a subclass, we specify added instance

fields, added methods, and changed or overridden methods

public class SavingsAccount extends BankAccount { public SavingsAccount(double rate) { interestRate = rate; } public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); }

private double interestRate; }

Page 7: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Layout of a Subclass Object• SavingsAccount object inherits the

balance instance field from BankAccount, and gains one additional instance field: interestRate:

Figure 2:Layout of a Subclass Object

Page 8: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Inheritance Hierarchies• Sets of classes can form complex inheritance hierarchies

• Example:

Figure 3:A Part of the Hierarchy of Ancient Reptiles

Page 9: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

A Simpler Hierarchy: Hierarchy of Bank Accounts

• Consider a bank that offers its customers the following account types:

1. Checking account: no interest; small number of free transactions per month, additional transactions are charged a small fee

2. Savings account: earns interest that compounds monthly.

Continued…

Page 10: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

A Simpler Hierarchy: Hierarchy of Bank Accounts

Figure 5:Inheritance Hierarchy for Bank Account Classes

Continued…

• Inheritance hierarchy:

Page 11: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

A Simpler Hierarchy: Hierarchy of Bank Accounts

• All bank accounts support the getBalance method

• All bank accounts support the deposit and withdraw methods. However, the methods behave differently in different subclasses (e.g. checking account charges fees for deposit and withdrawal). The methods are over-ridden (re-defined) in different subclasses.

• Checking account needs a method deductFees; savings account needs a method addInterest

Page 12: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Three different things to do with methods

• Override method in subclass: Supply a different implementation of a method that exists in the

superclass Must have same signature (same name and same parameter types) If method is applied to an object of the subclass type, the overriding

method is executed

• Inherit method in subclass: Don't supply a new implementation of a method that exists in superclass Superclass method can be applied to the subclass objects

• Add method to subclass: Supply a new method that doesn't exist in the superclass New method can be applied only to subclass objects

Page 13: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Inheriting Instance Variables (Fields)

• Can't override variables (also known as fields)

• Inherit field: All fields from the superclass are automatically inherited

• Add field: We can supply a new field that doesn't exist in the superclass

• What if you define a new field with the same name as a superclass field? Each object would have two instance fields of the same

name Fields can hold different values Legal but extremely undesirable

Page 14: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Implementing the CheckingAccount Class

• Overrides deposit and withdraw to increment the transaction count:

Continued…

public class CheckingAccount extends BankAccount { public void deposit(double amount) {. . .} public void withdraw(double amount) {. . .} public void deductFees() {. . .} // new method private int transactionCount; // new instance field }

Page 15: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Implementing the CheckingAccount Class

• Each CheckingAccount object has two instance fields: balance (inherited from BankAccount) transactionCount (new to CheckingAccount)

• You can apply four methods to CheckingAccount objects: getBalance() (inherited from BankAccount) deposit(double amount) (override BankAccount method) withdraw(double amount)(override BankAccountmethod) deductFees() (new to CheckingAccount)

Page 16: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Inheritance and public, private, and protected

• If a variable or method in a class is public, it can be used anywhere (e.g. in the subclasses of that class)

• If a variable or method is private, it cannot be used anywhere outside that class, not even in subclasses of that class.

• This means that a subclass can only use the public part of its parent superclass.

• If a variable or method is declared protected, it cannot be used outside that class, except in subclasses of that class.

• In Horstmann’s BankAccount class, the balance variable is declared private. We therefore need to use public methods to access that variable in all subclasses.

Page 17: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Invoking a Super Class Method• Can't just call deposit(amount)

in deposit method of CheckingAccount

• That will result in the deposit method calling itself: an infinite loop. We want to call deposit in the superclass.

• We call the superclass method using the keyword super:

• There is also a keyword this, which can be used to refer to methods or variables of this class, rather than to methods of its superclass.

public void deposit(double amount) { transactionCount++; // Now add amount to balance super.deposit(amount); }

Page 18: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Implementing Remaining Methods

public class CheckingAccount extends BankAccount { . . . public void withdraw(double amount) { transactionCount++; // Now subtract amount from balance super.withdraw(amount); } Continued…

Page 19: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Implementing Remaining Methods

public void deductFees() { if (transactionCount > FREE_TRANSACTIONS) { double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS); super.withdraw(fees); } transactionCount = 0; } . . . private static final int FREE_TRANSACTIONS = 3; private static final double TRANSACTION_FEE = 2.0;}

Page 20: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Common Error: Shadowing Instance Fields• A subclass has no access to the private

instance fields (variables) of the superclass

• Beginner's error: "solve" this problem by adding another instance field with same name:

public class CheckingAccount extends BankAccount { public void deposit(double amount) { transactionCount++; balance = balance + amount; } . . .

private double balance; // Don't do this!}

Page 21: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Common Error: Shadowing Instance Fields

• Now the deposit method compiles, but it doesn't update the correct balance!

Figure 6:Shadowing Instance Fields

Page 22: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Subclass Construction

• super followed by a parenthesis indicates a call to the superclass constructor

Continued…

public class CheckingAccount extends BankAccount { public CheckingAccount(double initialBalance) { // Construct superclass super(initialBalance); // Initialize transaction count transactionCount = 0; } . . . }

Page 23: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Subclass Construction

• Must be the first statement in subclass constructor

• If subclass constructor doesn't call superclass constructor, default superclass constructor is used Default constructor: constructor with no

parameters If all constructors of the superclass require

parameters, then the compiler reports an error

Page 24: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Self Check

8. Why didn't the SavingsAccount constructor in Section 13.1 call its superclass constructor?

9. When you invoke a superclass method with the super keyword, does the call have to be the first statement of the subclass method?

Page 25: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Answers

8. It was content to use the default constructor of the superclass, which sets the balance to zero.

9. No–this is a requirement only for constructors. For example, the SavingsAccount.deposit method first increments the transaction count, then calls the superclass method.

Page 26: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Converting Between Subclass and Superclass Types

• Ok to convert subclass reference to superclass reference

SavingsAccount collegeFund = new SavingsAccount(10); BankAccount anAccount = collegeFund; Object anObject = collegeFund;

Page 27: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Converting Between Subclass and Superclass Types • The three object references stored in collegeFund, anAccount, and anObject all refer to the same object of type SavingsAccount

Figure 7:Variables of Different Types Refer to the Same Object

Page 28: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Converting Between Subclass and Superclass Types• Superclass references don't know the full story:

• When you convert between a subclass object to its superclass type: The value of the reference stays the same–it is

the memory location of the object But, less information is known about the object

Continued…

anAccount.deposit(1000); // OK anAccount.addInterest(); // No--not a method of the class to which anAccount belongs

Page 29: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Converting Between Subclass and Superclass Types

• Why would anyone want to know less about an object? Reuse code that knows about the superclass but

not the subclass:

• Can be used to transfer money from any type of BankAccount

public void transfer(double amount, BankAccount other) { withdraw(amount);// same as this.withdraw(amount) other.deposit(amount); }

Page 30: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Converting Between Subclass and Superclass Types

• Occasionally you need to convert from a superclass reference to a subclass reference

• This cast is dangerous: if it is not possible to cast the object to the class given, an exception is thrown

Continued…

BankAccount anAccount = (BankAccount) anObject;

Page 31: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Converting Between Subclass and Superclass Types

• Solution: use the instanceof operator

• instanceof: tests whether an object belongs to a particular type

if (anObject instanceof BankAccount) { BankAccount anAccount = (BankAccount) anObject; . . . }

Page 32: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Polymorphism• In Java, type of a variable doesn't completely determine

type of object to which it refers

• Method calls are determined by type of actual object, not type of object reference

• Compiler will check that only legal methods are invoked

BankAccount aBankAccount = new SavingsAccount(1000); // aBankAccount holds a reference to a SavingsAccount

BankAccount anAccount = new CheckingAccount(); anAccount.deposit(1000); // Calls "deposit" from CheckingAccount

Object anObject = new BankAccount(); anObject.deposit(1000); // Wrong!

Page 33: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Polymorphism

• Polymorphism: ability to refer to objects of multiple types with varying behavior

• Polymorphism at work:

• Depending on types of amount and other, different versions of withdraw and deposit are called

public void transfer(double amount, BankAccount other) { withdraw(amount); // Shortcut for this.withdraw(amount) other.deposit(amount); }

Page 34: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

File AccountTester.java

01: /**02: This program tests the BankAccount class and03: its subclasses. 04: */05: public class AccountTester06: { 07: public static void main(String[] args)08: { 09: SavingsAccount momsSavings 10: = new SavingsAccount(0.5);11: 12: CheckingAccount harrysChecking13: = new CheckingAccount(100);14: 15: momsSavings.deposit(10000);16:

Continued…

Page 35: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

File AccountTester.java

17: momsSavings.transfer(2000, harrysChecking); 18: harrysChecking.withdraw(1500);19: harrysChecking.withdraw(80); 20: 21: momsSavings.transfer(1000, harrysChecking);22: harrysChecking.withdraw(400); 23: 24: // Simulate end of month25: momsSavings.addInterest();26: harrysChecking.deductFees();27: 28: System.out.println("Mom's savings balance = $“29: + momsSavings.getBalance());30: 31: System.out.println("Harry's checking balance = $“32: + harrysChecking.getBalance());33: }34: }

Page 36: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Object: The Cosmic Superclass

• All classes defined without an explicit extends clause automatically extend Object

Figure 8:The Object Class is the Superclass of Every Java Class

Page 37: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Object: The Cosmic Superclass

• Most useful methods: String toString() boolean equals(Object otherObject)

• Good idea to override these methods in your classes

Page 38: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Overriding the toString Method

• Returns a string representation of the object

• Useful for debugging:

Continued…

Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString(); // Sets s to "java.awt.Rectangle[x=5,y=10,width=20,height=30]"

Page 39: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Overriding the twostring Method

• toString is called by built-in methods whenever a string is needed:

• Object.toString prints class name and the hash code of the object

"box=" + box; // Result: "box=java.awt.Rectangle[x=5,y=10,width=20,height=30]"

BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to something like "BankAccount@d24606bf"

Page 40: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Overriding the toString Method

• To provide a nicer representation of an object, override toString:

• This works better:

public String toString() { return "BankAccount[balance=" + balance + "]"; }

BankAccount momsSavings = new BankAccount(5000); String s = momsSavings.toString(); // Sets s to "BankAccount[balance=5000]"

Page 41: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Overriding the equals Method• equals tests for equal contents

Figure 9:Two References to Equal Objects

Continued…

Page 42: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Overriding the equals Method

• == tests for equal location

Figure 10:Two References to the Same Object

Page 43: Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =

Overriding the equals Method• Define the equals method to test whether

two objects have equal state

• When redefining equals method, you cannot change object signature; use a cast instead:

public class Coin { . . . public boolean equals(Object otherObject) { Coin other = (Coin) otherObject; return name.equals(other.name) && value == other.value; } . . . }