Inheritance and Subclasses CS 21 a 6282004 Inheritance
Inheritance and Subclasses CS 21 a 6/28/2004
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 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 2
Example: Checking. Account n n Suppose we define Checking. Account from scratch Attributes n n n balance number of checks drawn Methods n n n 6/28/2004 deposit withdraw get balance draw a check …others Checking. Account int balance int num. Checks int get. Balance() void deposit( int amount ) void withdraw( int amount ) void draw. Check( int amount ) …others Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 3
Example: Checking. Account n n n Resulting class is very similar to Bank. Account The same as Bank. Account except for an additional field and some methods Better to extend Bank. Account instead 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 4
Bank. Account revisited public class Bank. Account { private int balance = 0; public int get. Balance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 5
public class Checking. Account { private int balance = 0; private int num. Checks = 0; Checking. Account. java public int get. Balance() { return balance; Just like Bank. Account } except for the code in public void deposit( int amount ) bold { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } public void draw. Check( int amount ) { balance = balance - amount; // or, withdraw( amount ); num. Checks++; } public int num. Checks. Drawn() { return num. Checks; } } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 6
Using extends public class Checking. Account extends Bank. Account { private int num. Checks = 0; Checking. Account. java public void draw. Check( int amount ) { withdraw( amount ); // can’t do balance = balance – amount; // because balance is private to Bank. Account num. Checks++; } public int num. Checks. Drawn() { return num. Checks; } } Notice that (public) methods defined in Bank. Account (e. g. , withdraw) can be used within Checking. Account 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 7
Using Checking. Account objects Can call methods defined in Bank. Account Checking. Account mary = new Checking. Account(); mary. deposit( 1000 ); System. out. println( “Balance: ” + mary. get. Balance() ); mary. draw. Check( 100 ); System. out. println( “Balance: ” + mary. get. Balance() ); System. out. println( “Checks Drawn: ” + mary. num. Checks. Drawn() ); … and methods defined in Checking. Account 6/28/2004 Copyright 2004, 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 checking account is a bank account manager is an employee graduate student is a student circle is a shape Bank Account Diagramming notation: Checking Account 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 9
Some terminology n n n Checking. Account is a subclass of Bank. Account is a superclass Inheritance relationships result in a class hierarchy Shape Circle Rectangle Square 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 10
Method overriding n Suppose Bank. Account has a show. Status() method defined as follows: public void show. Status() { System. out. println( “Balance is ” + balance ); } n Checking. Account can redefine or override show. Status(): public void show. Status() { System. out. println( “Balance is ” + get. Balance() ); System. out. println( “Checks issued: ” + num. Checks. Drawn() ); } n Calling show. Status() on a Checking. Account object invokes the appropriate method 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 11
Superclass variables for subclass objects n n n Checking accounts are bank accounts so it is possible to have a Bank. Account variable point to a Checking. Account object But not the other way around Superclass variables can refer to subclass objects, not vice-versa n n 6/28/2004 Bank. Account b 1 = new Checking. Account(); (note: only methods indicated in Bank. Account may be invoked through b 1) Checking. Account b 2 = new Bank. Account(); (not allowed because a bank account is not necessarily a checking account) Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 12
Variable vs object examples n Consider the following declarations n n n Bank. Account b = new Bank. Account(); Checking. Account c = new Checking. Account(); Bank. Account d = new Checking. Account(); Checking. Account f = new Bank. Account(); - valid invalid! Which methods will the following calls invoke? n n n n 6/28/2004 b. show. Status( ); c. withdraw( 100 ); c. show. Status( ); c. draw. Check( 100 ); d. withdraw( 100 ); d. show. Status( ); d. draw. Check(); - Bank. Account’s Checking. Account’s invalid! Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved Dynamic binding: the overriden show. Status() method will be called Because there is no draw. Check() method in Bank. Account 13
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 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 14
Object arrays and inheritance emps null Employee[] emps; for ( int i = 0; i < 5; i++ ) { emps[i]. increase. Salary( ); } 6/28/2004 0 Manager null object 1 null 2 Employee null object 3 null 4 Secretary null Object Secretary Object Manager object Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 15
Inheritance and constructors public class Bank. Account { private int balance; public Bank. Account() { balance = 0; } public Bank. Account( int init. Bal ) { balance = init. Bal; } public int get. Balance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } public class Checking. Account extends Bank. Account { private int num. Checks; public Checking. Account() { num. Checks = 0; } public void draw. Check( int amount ) { withdraw( amount ); num. Checks++; } public int num. Checks. Drawn() { return num. Checks; } } Checking. Account c = new Checking. Account(); } Which of the constructors are called? 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 16
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? 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 17
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 num. Checks; public Checking. Account() { num. Checks = 0; } public Checking. Account( int start. Bal ) { num. Checks = 0; } public void draw. Check( int amount ) { withdraw( amount ); num. Checks++; } public int num. Checks. Drawn() { return num. Checks; } This will still call Bank. Account( ), not Bank. Account( 1000 ) } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 18
Using super() public class Checking. Account extends Bank. Account { private int num. Checks; public Checking. Account() { num. Checks = 0; } public Checking. Account( int start. Bal ) { super( start. Bal ); num. Checks = 0; } public void draw. Check( int amount ) { withdraw( amount ); num. Checks++; } public int num. Checks. Drawn() { return num. Checks; } } 6/28/2004 • 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( int ) Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 19
Protected access n n n protected: another access modifier besides private and public If indicated in a superclass, protected means that the field or method is visible in subclasses but not visible elsewhere Example: in Bank. Account, if balance attribute was protected instead of private, then n n 6/28/2004 Checking. Account can access balance directly But outside classes still cannot access balance Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 20
public class Bank. Account { protected int balance = 0; public int get. Balance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } } public class Checking. Account extends Bank. Account { private int num. Checks = 0; public void draw. Check( int amount ) { balance = balance – amount; // above statement is now possible // because balance is protected in Bank. Account num. Checks++; } public int num. Checks. Drawn() { return num. Checks; } } public class Another. Class {. . . Checking. Account c = new Checking. Account(); c. deposit( 1000 ); // allowed because these c. draw. Check( 100 ); // methods are public c. balance = 1000000. 00; // not allowed because variable is protected c. num. Checks = 0; // not allowed because variable is private. . . } 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 21
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 n n Use protected modifier to get direct access Behavior can be overridden Use super(…) to ensure the correct superclass constructor is called In Java there are other options n n 6/28/2004 To be taken up in CS 21 b Abstract. Copyright classes, Interfaces and implements 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved 22
- Slides: 22