Download presentation
Presentation is loading. Please wait.
1
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 106 - Spring 2010 Today’s Topics Comments and/or Questions? Overriding methods including toString()
3
Object is the superclass of all All classes inherit from Java's Object class. All classes that do not use the extends keyword directly inherit from Java's Object class. What does that mean for us?
4
Overriding methods A subclass can override a superclass's method by providing a definition for a method that exists in the superclass with the same name and number and type of parameters. Let's add a method to Account and override it in SavingsAccount, but not in CheckingAccount. Then let's call the method with an object of SavingsAccount. And let's call the method with an object of CheckingAccount.
5
Overriding methods public String toString() What do you think about the toString() method? Can that be overridden by Account? Can it be overridden by a subclass of Account, like SavingsAccount?
6
Good design principle It should be apparent that in a case like the one we're doing now, Bank Accounts, we don't ever want the data to be allowed to be invalid. Care must be taken to require that a class's variables cannot have invalid data at any time. e.g. We wouldn't want to allow ATM_Withdrawal_Per_Day_Limit to ever be negative. It doesn't make sense. Also, Account #'s typically have a valid range of possibilities. We certainly wouldn't want account_number to be public and therefore able to be changed to an invalid value. We make the appropriate instance variables private for just that reason. And then only allow them to be changed in a controlled way via methods with some testing in them. We could put some testing in the code to handle this.
7
Protected vs. Private vs. Public subclasses have access to public and protected members of their superclass a class has access to all of its own members (whether they are private, protected or public) objects of a class have access only to the public members of the class (and the public members of the parent class(es)). Classes within the same package though are allowed to always access protected members --- but I don't recommend this. What does this mean for our Account program? What about inside the CheckingAccount class, what members of Account can we refer to? What about an object of type CheckingAccount --- does it have access to any of the members in Account?
8
Overloaded methods Overloaded methods are those that have the same name but different numbers or types of parameters. It has nothing to do with the super / subclass (parent / child) relationships that we've been talking about. Does anyone remember when we used Overloaded methods?
9
Account Program Let's continue to implement Account, CheckingAccount, SavingsAccount – toString() – equals(Object o) – Add set methods, get methods Deposit, withdraw methods (instead of setBalance) – Create objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.