28
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Embed Size (px)

Citation preview

Page 1: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

• Often categorize concepts into hierarchies:

Inheritance Hierarchies

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Page 2: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Example: Different account types:

1. Checking account:

•No interest

•Small number of free transactions per month

•Charges transaction fee for additional transactions

2. Savings account:

•Earns interest that compounds monthly

• Superclass: BankAccount

• Subclasses: CheckingAccount & SavingsAccount

Inheritance Hierarchies

Page 3: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Behavior of account classes:

• All support getBalance method

• Also support deposit and withdraw methods, but implementation details differ

• Checking account needs a method deductFees to deduct the monthly fees and to reset the transaction counter

• Checking account can override deposit and withdraw methods to count the transactions

• Savings account needs a method addInterest()

Inheritance Hierarchies

Page 4: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Inheritance Hierarchies

Page 5: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 10.1 Inheritance

Page 6: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 7: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• In subclass, specify additional instance variables, additional 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 } public void getBalance() { // overridden method implementation } }

Inheritance Hierarchies

Page 8: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 9: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 10: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 11: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 12: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 13: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Idea:

public class CheckingAccount extends BankAccount{ public void deposit(double amount) { transactionCount++; // Now add amount to balance using deposit() deposit(); // Not complete }}

• Won't work because compiler interpretsdeposit(amount);

asthis.deposit(amount);

which calls the method we are currently writing infinite ⇒recursion

Overriding Methods

Page 14: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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

Page 15: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 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.)

Page 16: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 10.2 Calling a Superclass Method

Page 17: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Constructors have special requirements for using super()…

Calling the super class constructor

Page 18: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 10.3 Calling a Superclass Constructor

Page 19: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Superclass references don’t know the full story:

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

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

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

Can be used to transfer money from any type of BankAccount

Converting Between Subclass and Superclass Types

Page 20: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 10.4 The instanceof Operator

Page 21: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

How to 10.1: Inheritance Hierarchy

Page 22: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Type of a variable doesn’t completely determine type of object to which it refers:

Question q1 = new NumericQuestion();q1.display();

Question q2 = new FreeResponseQuestion();q2.display();

Which display method is called?

• Dynamic method lookup: When the virtual machine calls an instance method, it locates the method of the implicit parameter's class

• Also called Dynamic method invocation

Polymorphism and Inheritance

Page 23: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Polymorphism: Ability to treat objects with differences in behavior in a uniform way

• The method call

display();

is a shortcut for

this.display();

• this can refer to a Question or a subclass object

Polymorphism and Inheritance

ChoiceQuestion q1 = new ChoiceQuestion();q1.display();MultiChoiceQuestion q2 = new MultiChoiceQuestion();q2.display();

Page 24: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Recall AnimalList with all dogs, cats, rabbits, etc. calling sleep()

• If a subclass implements a method that is in a superclass, such as sleep(), the sublcass overrides the super class method

• Dynamic lookup (dynamic invocation) occurs to determine which method to call

• To get both the subclass method and the super class method be invoked:

sleep()super.sleep()

Polymorphism and Inheritance

Page 25: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• Recall AnimalList with all dogs, cats, rabbits, etc. calling sleep()

• If a subclass implements a method that is in a superclass, such as sleep(), the sublcass overrides the super class method

• Dynamic lookup (dynamic invocation) occurs to determine which method to call

• To get both the subclass method and the super class method be invoked:

sleep()super.sleep()

Polymorphism and Inheritance

Page 26: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

• If a subclass does not implement a method that is invoked, the compiler traverses up the hierarchy to find the closest one

Polymorphism and Inheritance

mChoiceQ.display();

display()

display()

display()

Page 27: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

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

Object: The Cosmic Superclass

Page 28: Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved

Big Java by Cay HorstmannCopyright © 2009 by John Wiley & Sons. All rights reserved.

Object: The Cosmic Superclass

• Most useful methods:

• String toString()

• boolean equals(Object otherObject)

• Good idea to override these methods in your classes