Big Java 4

Chapter 10 – Inheritance

Chapter Goals

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

Self Check 10.1

What is the purpose of the JTextComponent class in Figure 2?

Self Check 10.2

Why don’t we place the addInterest method in the BankAccount class?

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

Inheritance Hierarchies

ch10/accounts/SavingsAccount.java

Your browser does not support the <object> tag.

Syntax 10.1 Inheritance

Inheritance

Self Check 10.3

Which instance variable does an object of class SavingsAccount have?

Self Check 10.4

Name four methods that you can apply to SavingsAccount objects.

Self Check 10.5

If the class Manager extends the class Employee, which class is the superclass and which is the subclass?

Common Error: Shadowing Instance Variables

Common Error: Shadowing Instance variables

Overriding Methods

Overriding Methods

Overriding Methods

Overriding Methods

Overriding Methods

Syntax 10.2 Calling a Superclass Method

Calling a Superclass Method

Animation 10.1: Inheritance

Link to Flash animation

Self Check 10.6

Categorize the methods of the SavingsAccount class as inherited, new, and overridden.

Self Check 10.7

Why does the withdraw method of the CheckingAccount class call super.withdraw?

Self Check 10.8

Why does the deductFees method set the transaction count to zero?

Subclass Construction

ch10/accounts/CheckingAccount.java

Your browser does not support the <object> tag.

Syntax 10.3 Calling a Superclass Constructor

Calling a Superclass Constructor

Self Check 10.9

Why didn't the SavingsAccount constructor in Section 10.2 call its superclass constructor?

Self Check 10.10

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

Converting Between Subclass and Superclass Types

Converting Between Subclass and Superclass Types

Converting Between Subclass and Superclass Types

Syntax 10.4 The instanceof Operator

instanceof Operator

Self Check 10.11

Why did the second parameter of the transfer method have to be of type BankAccount and not, for example, SavingsAccount?

Self Check 10.12

Why can't we change the second parameter of the transfer method to the type Object?

Polymorphism and Inheritance

Polymorphism and Inheritance

Polymorphism and Inheritance

ch10/accounts/AccountTester.java

Your browser does not support the <object> tag. Program Run:

Self Check 10.13

If a is a variable of type BankAccount that holds a non-null reference, what do you know about the object to which a refers?

Self Check 10.14

If a refers to a checking account, what is the effect of calling a.transfer(1000, a)?

Protected Access

Protected Access

Object: The Cosmic Superclass

Overriding the toString Method

Overriding the toString Method

Overriding the equals Method

Overriding the equals Method

Overriding the equals Method

The clone Method

The clone Method

The Object.clone method

Self Check 10.15

Should the call x.equals(x) always return true?

Self Check 10.16

Can you implement equals in terms of toString? Should you?

Scripting Languages

Using Inheritance to Customize Frames

Example: Investment Viewer Program

public class InvestmentFrame extends JFrame 
{ 
   private JButton button; 
   private JLabel label; 
   private JPanel panel; 
   private BankAccount account;
   
   public InvestmentFrame() 
   {  
      account = new BankAccount(INITIAL_BALANCE); 
      // Use instance variables for components 
      label = new JLabel("balance: " + account.getBalance()); 
      // Use helper methods 
      createButton(); 
      createPanel(); 
      setSize(FRAME_WIDTH, FRAME_HEIGHT); 
   } 
   
   private void createButton() 
   {
         button = new JButton("Add Interest"); 
      ActionListener listener = new AddInterestListener(); 
      button.addActionListener(listener); 
   } 
   
   private void createPanel() 
   { 
      panel = new JPanel(); 
      panel.add(button); 
      panel.add(label);      
      add(panel); 
   } 
}

Example: Investment Viewer Program

Of course, we still need a class with a main method:
public class InvestmentViewer2 
{  
   public static void main(String[] args) 
   {  
      JFrame frame = new InvestmentFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true);      
   } 
}

Self Check 10.17

How many Java source files are required by the investment viewer application when we use inheritance to define the frame class?

Self Check 10.18

Why does the InvestmentFrame constructor call setSize(FRAME_WIDTH, FRAME_HEIGHT), whereas the main method of the investment viewer class in Chapter 9 called frame.setSize(FRAME_WIDTH, FRAME_HEIGHT)?