Date post: | 17-Jan-2018 |
Category: |
Documents |
Upload: | shannon-cecily-wheeler |
View: | 219 times |
Download: | 0 times |
Inheritance and SubclassesCS 21a
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 2
Inheritance 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/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 3
Example: CheckingAccount
Suppose we define CheckingAccount from scratch
Attributes balance number of checks drawn
Methods deposit withdraw get balance draw a check …others
CheckingAccountint balanceint numChecksint getBalance()void deposit( int amount )void withdraw( int amount )void drawCheck( int amount )…others
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 4
Example: CheckingAccount
Resulting class is very similar to BankAccount
The same as BankAccount except for an additional field and some methods
Better to extend BankAccount instead
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 5
BankAccount revisited
public class BankAccount{ private int balance = 0; public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; }}
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 6
public class CheckingAccount{ private int balance = 0; private int numChecks = 0;
public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } public void drawCheck( int amount ) { balance = balance - amount; // or, withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; }}
CheckingAccount.java
Just like BankAccount except for the code in bold
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 7
public class CheckingAccount extends BankAccount{ private int numChecks = 0;
public void drawCheck( int amount ) { withdraw( amount ); // can’t do balance = balance – amount; // because balance is private to BankAccount numChecks++; } public int numChecksDrawn() { return numChecks; }}
CheckingAccount.java
Using extends
Notice that (public) methods defined in BankAccount (e.g., withdraw) can be used within CheckingAccount
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 8
Using CheckingAccount objects
CheckingAccount mary = new CheckingAccount();mary.deposit( 1000 );System.out.println( “Balance: ” + mary.getBalance() );mary.drawCheck( 100 );System.out.println( “Balance: ” + mary.getBalance() );System.out.println( “Checks Drawn: ” +
mary.numChecksDrawn() );
Can call methods defined in BankAccount
… and methods defined in CheckingAccount
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 9
The inheritance relationship
Use inheritance for “is-a” relationships Examples
A checking account is a bank account A manager is an employee A graduate student is a student A circle is a shape
Checking
Account
BankAccount
Diagramming notation:
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 10
Some terminology CheckingAccount is a subclass of
BankAccount BankAccount is a superclass
Inheritance relationships result in aclass hierarchy
Circle
Shape
Square
Rectangle
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 11
Method overriding Suppose BankAccount has a showStatus() method
defined as follows:public void showStatus(){ System.out.println( “Balance is ” + balance );}
CheckingAccount can redefine or override showStatus():public void showStatus(){ System.out.println( “Balance is ” + getBalance() ); System.out.println( “Checks issued:” + numChecksDrawn() );}
Calling showStatus() on a CheckingAccount object invokes the appropriate method
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 12
Superclass variables for subclass objects
Checking accounts are bank accounts so it is possible to have a BankAccount variable point to a CheckingAccount object
But not the other way around Superclass variables can refer to subclass
objects, not vice-versa BankAccount b1 = new CheckingAccount();
(note: only methods indicated in BankAccount may be invoked through b1)
CheckingAccount b2 = new BankAccount();(not allowed because a bank account is not necessarily a checking account)
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 13
Variable vs object examples
Consider the following declarations BankAccount b = new BankAccount(); - valid CheckingAccount c = new CheckingAccount(); - valid BankAccount d = new CheckingAccount(); - valid CheckingAccount f = new BankAccount(); - invalid!
Which methods will the following calls invoke? b.showStatus( ); - BankAccount’s c.withdraw( 100 ); - BankAccount’s c.showStatus( ); - CheckingAccount’s c.drawCheck( 100 ); - CheckingAccount’s d.withdraw( 100 ); - BankAccount’s d.showStatus( ); - CheckingAccount’s d.drawCheck(); - invalid!
Dynamic binding:the overriden showStatus() method will be called
Because there is no drawCheck() method in BankAccount
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 14
Object arrays and inheritance
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/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 15
Object arrays and inheritance
null
Employee[] emps;
for ( int i = 0; i < 5; i++ ){ emps[i].increaseSalary( );}
0
1
2
34
nullnullnullnullnull
emps Manager
objectEmployee object
Secretary Object
Manager
object
Secretary Object
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 16
Inheritance and constructors
public class BankAccount{ private int balance;
public BankAccount() { balance = 0; } public BankAccount( int initBal ) { balance = initBal; } public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; }}
public class CheckingAccount extends BankAccount{ private int numChecks;
public CheckingAccount() { numChecks = 0; } public void drawCheck( int amount ) { withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; }}
CheckingAccount c = new CheckingAccount();
Which of the constructors are called?
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 17
Inheritance and constructors
CheckingAccount = new CheckingAccount();
In the above statement, CheckingAccount’s (default) constructor is called
Since CheckingAccount is a BankAccount, a BankAccount constructor should also be called Which one? Answer: the default constructor Note that BankAccount() is called before
CheckingAccount() What if we want a particular constructor of a
superclass called?
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 18
Incorrect attempt
public class CheckingAccount extends BankAccount{ private int numChecks;
public CheckingAccount() { numChecks = 0; } public CheckingAccount( int startBal ) { numChecks = 0; } public void drawCheck( int amount ) { withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; }}
We want CheckingAccount c = new CheckingAccount( 1000 );to create an account with an initial balance of 1000
This will still callBankAccount( ),notBankAccount( 1000 )
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 19
Using super()public class CheckingAccount extends BankAccount{ private int numChecks;
public CheckingAccount() { numChecks = 0; } public CheckingAccount( int startBal ) { super( startBal ); numChecks = 0; } public void drawCheck( int amount ) { withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; }}
•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’ constructorImplicitly calls
“super();” or BankAccount( )
Calls a particular constructorBankAccount( int )
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 20
Protected access 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 BankAccount, if balance attribute was protected instead of private, then CheckingAccount can access balance directly But outside classes still cannot access balance
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 21
public class BankAccount{ protected int balance = 0; public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; }}
public class CheckingAccount extends BankAccount{ private int numChecks = 0;
public void drawCheck( int amount ) { balance = balance – amount; // above statement is now possible // because balance is protected in BankAccount numChecks++; } public int numChecksDrawn() { return numChecks; }}public class AnotherClass
{ ... CheckingAccount c = new CheckingAccount(); c.deposit( 1000 ); // allowed because these c.drawCheck( 100 ); // methods are public
c.balance = 1000000.00; // not allowed because variable is protected c.numChecks = 0; // not allowed because variable is private ...}
6/28/2004Copyright 2004, by the authors of these slides, and Ateneo
de Manila University. All rights reserved L16: Inheritance
Slide 22
Summary Use inheritance for similar types of objects Common characteristics and behavior are
placed in the superclass Subclasses share or inherit superclass’
characteristics and behavior 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
To be taken up in CS 21b Abstract classes, Interfaces and implements