topics
play

Topics 11/13/2006 Chapter 11, start Chapter 12 11/20/2006 Chapter - PDF document

Date Chapter 11/6/2006 Chapter 10, start Chapter 11 Topics 11/13/2006 Chapter 11, start Chapter 12 11/20/2006 Chapter 12 Inheritance Concepts 11/27/2006 Chapter 13 12/4/2006 Final Exam Inheritance Design 12/11/2006 Project Due


  1. Date Chapter 11/6/2006 Chapter 10, start Chapter 11 Topics 11/13/2006 Chapter 11, start Chapter 12 11/20/2006 Chapter 12 • Inheritance Concepts 11/27/2006 Chapter 13 12/4/2006 Final Exam • Inheritance Design 12/11/2006 Project Due – Inherited Members of a Class Chapter 10 – Subclass Constructors – Adding Specialization to the Subclass Object-Oriented Programming – Overriding Inherited Methods Part 3: • The protected Access Modifier Inheritance, Polymorphism, and • Abstract Classes and Methods Interfaces • Polymorphism • Interfaces Home Home Home Inheritance Concepts A Sample Vehicle Hierarchy • A common form of reuse of classes is inheritance. • We can organize classes into hierarchies of functionality. • The class at the top of the hierarchy ( superclass ) defines instance variables and methods common to all classes in the hierarchy. • We derive a subclass, which inherits behavior and fields from the superclass . • This hierarchy is depicted using a Unified Modeling Language (UML) diagram. • In UML diagrams, arrows point from the subclass to the superclass. Home Home 1

  2. Superclasses and Subclasses Superclasses and Subclasses • A superclass can have multiple subclasses. • A big advantage of inheritance is that we can write common code once and reuse it in subclasses. – Generalization • Subclasses can be superclasses of other subclasses. • A subclass can define new methods and instance variables, some of which may override (hide) • A subclass can inherit directly from only one those of a superclass. superclass. – Specialization • All classes inherit from the Object class. Home Home Specifying Inheritance An Applet Hierarchy • When we wrote an applet, • The syntax for defining a subclass is to use the extends we defined a subclass. keyword in the class header, as in • We say that inheritance implements an "is a" relationship, in that a subclass accessModifier class SubclassName object "is a" superclass object as well. extends SuperclassName { • Thus, RollABall "is a" JApplet (its // class definition direct superclass), Applet , Panel, } Container, Component , and Object . • The superclass name specified after the extends keyword is • RollABall begins with more than called the direct superclass. 275 methods and 15 fields inherited • As mentioned, a subclass can have many superclasses, but from its 6 superclasses. only one direct superclass. Home Home 2

  3. The Bank Account Hierarchy BankAccount.java 1/3 • The BankAccount class is import java.text.DecimalFormat; the superclass. public class BankAccount { – Instance variables: public final DecimalFormat MONEY • balance ( double ) = new DecimalFormat( "$#,##0.00" ); • MONEY (final DecimalFormat) private double balance; – Methods: public BankAccount( ) { • Default and overloaded constructors balance = 0.0; • deposit and withdraw methods } • balance accessor public BankAccount( double startBalance ) { • toString deposit( startBalance ); • See Example 10.1 BankAccount.java (next slide) } Home Home BankAccount.java 2/3 BankAccount.java 3/3 public void withdraw( double amount ) { public double getBalance( ) { if ( amount >= 0.0 && amount <= balance ) return balance; balance -= amount; } else public void deposit( double amount ) { System.err.println( "Withdrawal amount must be positive " + "and cannot be greater than balance" ); if ( amount >= 0.0 ) } balance += amount; public String toString( ) else { return ( "balance is " + MONEY.format( balance ) ); System.err.println( "Deposit amount must be } positive." ); } } Home Home 3

  4. The CheckingAccount Class CheckingAccountClient.java • We derive the CheckingAccount subclass from public class CheckingAccountClient { BankAccount: public static void main( String [] args ) { CheckingAccount c1 = new CheckingAccount( ); public class CheckingAccount extends BankAccount System.out.println( "New checking account: " + c1 ); { } c1.deposit( 350.75 ); • A subclass inherits all the public members of a System.out.println( "\nAfter depositing $350.75: " + c1 ); superclass. Thus, the CheckingAccount class inherits – the MONEY instance variable c1.withdraw( 200.25 ); – The getBalance , deposit , withdraw , and toString System.out.println( "\nAfter withdrawing $200.25: " + c1 ); methods } • See Example 10.3 CheckingAccountClient.java (next } slide) Home Home private Members protected Members • protected members are inherited by subclasses • Superclass members declared as private are NOT (like public members), while still being hidden inherited, although they are part of the subclass. from client classes (like private members). • Also, any class in the same package as the • Thus, the balance instance variable is allocated to all CheckingAccount objects, but methods of the superclass can directly access a protected field, CheckingAccount class cannot directly access balance . even if that class is not a subclass. • Disadvantage: • To set or get the value of balance , the CheckingAccount – Because more than one class can directly access a methods must call the withdraw , deposit, or getBalance protected field , protected access compromises methods of the Superclass BankAccount. encapsulation and complicates maintenance of a program. • This simplifies maintenance because the BankAccount – For that reason, we prefer to use private , rather than class enforces the data validation rules for balance . protected, for our instance variables. Home Home 4

  5. Inheritance Rules Subclass Constructors Superclass Inherited Directly Accessible by Directly Accessible by • Constructors are not inherited. Members by Subclass? Client of Subclass? subclass? • However, the subclass can call the constructors of public fields yes yes, by using field yes the superclass to initialize inherited fields. name • Implicit invocation public methods yes yes, by calling method yes from subclass methods – The default constructor of the subclass automatically calls the default constructor of the superclass protected fields yes yes, by using field no, must call accessors name and mutators • For explicit invocation, use this syntax: protected yes yes, by calling method no methods from subclass methods super( argument list ); private fields no no, must call accessors no, must call accessors and mutators and mutators If used, this statement must be the first statement in the private methods no no no subclass constructor Home Home BankAccount.java Version 2 CheckingAccount Constructors import java.text.DecimalFormat; public class BankAccount { public CheckingAccount( ) public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" ); { private double balance; // optional explicit call public BankAccount ( ) { // to BankAccount default constructor balance = 0.0; super( ); System.out.println( "In BankAccount default constructor" ); } } public BankAccount ( double startBalance ) { public CheckingAccount( double startBalance ) if ( balance >= 0.0 ) balance = startBalance; { else balance = 0.0; // explicit call to BankAccount System.out.println( "In BankAccount overloaded constructor" ); // overloaded constructor } super( startBalance ); public String toString ( ) } { • See Examples 10.4 (BankingAccount.java V2 & return ( "balance is " + MONEY.format( balance ) ); 10.5 CheckingAccount.java V2) } Home Home } 5

  6. Common Error CheckingAccount.java Version 2 Trap public class CheckingAccount extends BankAccount { public CheckingAccount( ) { • An attempt by a subclass to directly access a super( ); // optional, call BankAccount constructor private field or call a private method defined in a System.out.println( "In CheckingAccount " superclass will generate a compiler error. + "default constructor" ); • To set initial values for private variables, call the } appropriate constructor of the direct superclass. public CheckingAccount( double startBalance ) { super( startBalance ); // call BankAccount constructor • For example, this statement in the overloaded System.out.println( "In CheckingAccount " CheckingAccount class constructor calls the + "overloaded constructor" ); overloaded constructor of the BankAccount class: } } super( startBalance ); Home Home Software Engineering Inheritance Rules for Constructors Tip Overloaded constructors in a subclass should Superclass Inherited Directly Accessible by Directly Accessible by Members by Subclass? Client of Subclass Using explicitly call the direct superclass constructor to subclass? a Subclass Reference? initialize the fields in its superclasses. constructors no yes, using no super( arg list ) in a subclass constructor Home Home 6

Recommend


More recommend