cs 106 introduction to computer science i 11 / 15 / 2006 instructor: michael eckmann

23
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Post on 20-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

CS 106Introduction to Computer Science I

11 / 15 / 2006

Instructor: Michael Eckmann

Page 2: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 106 - Fall 2006

Today’s Topics• Comments and/or Questions?• Look for programming assignment to be assign in few hours• Inheritance ideas• Bank Account exercise• class Object

– toString

• super

• overriding methods in class hierachies

• public, private, protected

• ...

Page 3: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Bank account Exercise • What data members describe a bank account?–That is, what does a bank account have.

• What types are these?

• We want to handle Checking and Savings Accounts

Page 4: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Bank account • Maybe we might want the following data members:–Account number

–Balance

–Overdrawn Fee

–ATM Withdrawal Per Day Limit

–Bounced Check Fee

– Interest Rate

Page 5: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Bank account • So we could have a class named BankAccount that

contains the following data:

String Account_number;

double Balance;

double Overdrawn_Fee;

int ATM_Withdrawal_Per_Day_Limit;

double Bounced_Check_Fee;

double Interest_Rate;

Page 6: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Bank account • What methods might we need?

Page 7: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Bank account • Notice that the data here isn't for all accounts:–Account number (for all accounts)

–Balance (for all accounts)

–Overdrawn Fee (for all accounts)

–ATM Withdrawal Per Day Limit (for all accounts)

–Bounced Check Fee (for checking accounts only)

– Interest Rate (for savings accounts only)

Page 8: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Bank account • We could create an Account class that contained the

common stuff among all accounts• We then could create a SavingsAccount class that inherits

all the stuff about an Account from Account class and adds the things that are specific to Savings Accounts.

• We also could create a CheckingAccount class that inherits all the stuff about an Account from Account class and adds the things that are specific to Checking Accounts.

• These ideas are Inheritence ideas

Page 9: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Inheritance

• A subclass inherits all the instance variables in its superclass and has access to (can call) any public methods.

• Even if there is private instance data in a superclass, the subclass inherits that data but can't refer to the variables.

• e.g. a SavingsAccount will inherit accountNumber from Account, so an object of type SavingsAccount will have an accountNumber.

Page 10: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

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?

• Let's visit the Java API for the Object class.

– equals()

– toString()

Page 11: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

super• is a reference to the parent class

• super(); // is a call to the parent's default (no parameter) constructor

• A call to a parent constructor should be the first thing done in any constructor of a child.

• If you don't explicitly call it, Java will automatically call the parent's default constructor if it exists.

Page 12: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Hierarchy of the account classes

• SavingsAccount and CheckingAccount each inherit from Account

• What about AccountTester?

• How about the Object class?

Page 13: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

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.

Page 14: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Overriding methodspublic 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?

Page 15: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

equals method• In class Object, the header for the equals method is this:

public boolean equals(Object obj)

• If we want to override this method, we must make sure our equals method has the SAME signature.

• Does anyone recall what a signature contains?

Page 16: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

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'll put some testing in the code to handle this.

Page 17: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

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?

Page 18: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

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?

Page 19: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

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

Page 20: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Abstract classes• Abstract classes

– Can never be instantiated

– Can contain both abstract methods and actual (non-abstract) methods

– Can contain instance variables as well as constants

• If a class contains any abstract methods then it MUST BE an abstract class

• But an abstract class is not required to have abstract methods

• Abstract classes are different from interfaces which we will see next.

Page 21: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

More on overriding methods• Suppose we have a class Pet, Dog, and Cat.

• We provide a speak() method in Pet but with no body. Then, any class that inherits from Pet, must implement this method.

• Further, let's assume we have a class TalkingDog which doesn't bark when he speaks, instead he speaks English.

• To implement this stuff ...

Page 22: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

Class Pet, Dog and Cat• Let's suppose all Pets have names, breeds and make sounds, and know

how to sleep.

• Let's also suppose that different Dogs have different skills, but different Cats do not.

• Data to be stored in Pet is:

– Name

– Type (cat, dog, etc.)

– Breed

• Additional data to be stored in Dog is:

– Skill

• No additional data is stored in Cat.

• Let's set up the classes with these data and the relationships among the classes.

Page 23: CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann

More on overriding methods• So we have classes Pet (abstract), Dog, and Cat.

• We can provide a speak() method in Pet with no body (and we make it abstract). Then, any class that inherits from Pet, must implement this method.

• We can provide an actual method sleeps() in Pet that is not abstract.

• And the class TalkingDog which doesn't bark when he speaks, instead he speaks English.

• So, Dog and Cat inherit from Pet.

• TalkingDog inherits from Dog.

• Let's implement this stuff (instantiate objects of Dog, Cat, and TalkingDog)

• Note: because Pet contains an abstract method, the class is not instantiable --- the class itself must be declared as abstract.