Inheritance and Subclasses in Java CS 21 a
Inheritance and Subclasses in Java CS 21 a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 10, Horstmann text) 6/19/2005
Inheritance n n Inheritance: an object-oriented programming language feature that allows for the definition of a class in terms of another class Another example of a class relationship (besides Aggregation and Association) In Java, use the extends keyword Promotes reusability of existing code Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 2
Example: Savings. Account n n A savings account is a bank account that earns interest Savings. Account Attributes n n n balance interest rate Methods n n deposit withdraw get balance add interest double balance double interest. Rate double get. Balance() void deposit(double amount) void withdraw(double amount) void add. Interest() Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 3
Example: Savings. Account n n n If we wrote the class from scratch, the resulting class will be very similar to Bank. Account The same as Bank. Account except for an additional field an additional method Better to extend Bank. Account instead Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 4
Bank. Account revisited public class Bank. Account { private double balance; public Bank. Account() { balance = 0; } public void deposit( double amount ) { balance = balance + amount; } public void withdraw( double amount ) { if ( amount <= balance ) { balance = balance - amount; } else { System. out. println( "Insufficient balance" ); } } public double get. Balance( ) { return balance; } } Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 5
public class Savings. Account. java { private double balance; private double interest. Rate; public Savings. Account() { balance = 0; interest. Rate = 1. 0; Just like Bank. Account } except for the code in public Savings. Account( double a. Rate ) { balance = 0; bold interest. Rate = a. Rate; } public void deposit( double amount ) { balance = balance + amount; } public void withdraw( double amount ) { // some code omitted… } public double get. Balance( ) { return balance; } public void add. Interest() { double interest = balance*interest. Rate/100; balance = balance + interest; // or, deposit( interest ); } } Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 6
Using extends public class Savings. Account extends Bank. Account { private double interest. Rate; public Savings. Account() { interest. Rate = 1. 0; } public Savings. Account( double a. Rate ) { interest. Rate = a. Rate; } public void add. Interest() { // double interest = balance*interest. Rate/100; double interest = get. Balance()*interest. Rate/100; // balance = balance + interest; deposit( interest ); } } Savings. Account. java Notice that (public) methods defined in Bank. Account (e. g. , deposit) can be used within Savings. Account Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 7
Using Savings. Account objects Can call methods defined in Bank. Account Savings. Account mary = new Savings. Account(); mary. deposit( 1000 ); System. out. println( “Balance: ” + mary. get. Balance() ); mary. add. Interest(); System. out. println( “Balance: ” + mary. get. Balance() ); … and methods defined in Savings. Account Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 8
The inheritance relationship n n Use inheritance for “is-a” relationships Examples n n A A savings account is a bank account Bank Account manager is an employee graduate student is a student Diagramming notation: circle is a shape Savings Account Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 9
Some terminology n n n Savings. Account is a subclass of Bank. Account is a superclass Inheritance relationships result in a class hierarchy Shape Circle Rectangle Square Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 10
Superclass variables, subclass objects n n n Savings accounts are bank accounts so it is possible to have a Bank. Account variable point to a Savings. Account object But not the other way around Superclass variables can refer to subclass objects, not vice-versa n n Bank. Account b 1 = new Savings. Account(); (note: only methods indicated in Bank. Account may be invoked through b 1) Savings. Account b 2 = new Bank. Account(); (not allowed because a bank account is not necessarily a savings account) Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 11
Superclass variables, subclass objects n Consider the following declarations n n n Bank. Account b = new Bank. Account(); Savings. Account s = new Savings. Account(); Bank. Account t = new Savings. Account(); Savings. Account z = new Bank. Account(); - valid invalid! Which method calls are valid? n n n b. withdraw( 100 ); b. add. Interest(); s. withdraw( 100 ); s. add. Interest(); t. withdraw( 100 ); t. add. Interest(); - calls Bank. Account’s withdraw - will not compile! - calls Bank. Account’s withdraw - calls Savings. Account’s add. Interest - calls Bank. Account’s withdraw - will not compile! (even though t refers to a savings account) Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 12
Another example: Checking. Account n Define a checking account as a bank account that “counts” transactions carried out (deposit and withdraw transactions), and deducts a transaction fee for each transaction beyond the third transaction n n Fees are deducted through a deduct. Fees() method, which resets the transaction count How do we write this class? Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 13
The Checking. Account class n If we wrote this class from scratch: n n n Same as Bank. Account, but with an int transaction. Count field deposit and withdraw methods begin with the statement transaction. Count++; and proceeds just like in Bank. Account get. Balance method is as it was with Bank. Account deduct. Fees method computes and deducts fees Inheritance not as useful yet, because all methods except get. Balance are different from the methods in Bank. Account Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 14
Method overriding n In the subclass, a method with the same signature (in the superclass) can have a definition different from that of the superclass n n Use super. method. Name(…) to call the superclass’ method When calling that method for an object of that class, the subclass’ method takes precedence over the superclass’ method Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 15
Method overriding and super public class Checking. Account { private double balance; private int transaction. Count; // … public void deposit( double amount ) { transaction. Count++; balance = balance + amount; } public void withdraw( double amount ) { transaction. Count++; if ( amount <= balance ) { balance = balance - amount; } else { System. out. println( "Insufficient balance" ); } } // … } -Methods can be overridden -Use super to call a superclass’ original method (“extending” a method) public class Checking. Account extends Bank. Account { private int transaction. Count; // … public void deposit( double amount ) { transaction. Count++; super. deposit(amount); } public void withdraw( double amount ) { transaction. Count++; super. withdraw(amount); } // … } Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 16
Method overriding and dynamic binding n n n Suppose: Bank. Account b = new Bank. Account(); Checking. Account c = new Checking. Account(); Bank. Account d = new Checking. Account(); Which withdraw() method is called? b. withdraw( 100 ); - Bank. Account’s c. withdraw( 100 ); - Checking. Account’s d. withdraw( 100 ); - Checking. Account’s Dynamic binding prevails in the d. withdraw(); call n The method appropriate to the object is always called Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 17
Inheritance and constructors public class Bank. Account { private double balance; public Bank. Account() { balance = 0; } public Bank. Account( double init. Bal ) { balance = init. Bal; } // … } public class Checking. Account extends Bank. Account { private int transaction. Count; public Checking. Account() { transaction. Count = 0; } //… } Checking. Account c = new Checking. Account(); Which of the constructors are called? Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 18
Inheritance and constructors Checking. Account = new Checking. Account(); n n In the above statement, Checking. Account’s (default) constructor is called Since Checking. Account is a Bank. Account, a Bank. Account constructor should also be called n n Which one? Answer: the default constructor Note that Bank. Account() is called before Checking. Account() What if we want a particular constructor of a superclass called? Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 19
Incorrect attempt We want Checking. Account c = new Checking. Account( 1000 ); to create an account with an initial balance of 1000 public class Checking. Account extends Bank. Account { private int transaction. Count; public Checking. Account() { transaction. Count = 0; } public Checking. Account( double init. Bal ) { transaction. Count = 0; } // … This will still call Bank. Account( ), not Bank. Account( 1000 ) } Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 20
Using super() public class Checking. Account extends Bank. Account { private int transaction. Count; public Checking. Account() { transaction. Count = 0; } public Checking. Account( double init. Bal ) { super( init. Bal ); transaction. Count = 0; } // … } • super( … ) indicates which superclass constructor will be called • If not indicated, it defaults to super( ) with no parameters • Call to super(…) should be the first line in the subclass’ constructor Implicitly calls “super(); ” or Bank. Account( ) Calls a particular constructor Bank. Account( double ) Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 21
Object arrays and inheritance n n Suppose Employee is a superclass and Manager and Secretary are subclasses of Employee An Employee array can have its elements refer to different kinds of objects Can use a for-statement to call the same method on the different objects Allows the program to view the objects in a uniform way Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 22
Object arrays and inheritance emps null Employee[] emps; for ( int i = 0; i < 5; i++ ) { emps[i]. increase. Salary( ); } 0 Manager null object 1 null 2 Employee null object 3 null 4 Secretary null Object Secretary Object Manager object Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 23
Summary n n n Use inheritance for similar types of objects Common characteristics and behavior are placed in the superclass Subclasses share or inherit superclass’ characteristics and behavior Behavior can be overridden; use super. method. Name(…) to invoke a superclass’ method when “extending” a method Use super(…) to ensure the correct superclass constructor is called Other options in Java: abstract classes and interfaces Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 24
- Slides: 24