Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2007 Today’s Topics Comments and/or Questions? Is-a and Has-a relationships. Account exercise using Inheritance

3 Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. For example: – A Card has a suit and has a number – A Deck has 52 Cards – A Car has a color and has a make and has a model,... – An Employee has a hire date and has a salary,... – Can we think of any more?

4 Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. public class Card { private int suit; private int number; //... } public class Car { private String color; private String make; //... }

5 Is-a vs. Has-a A “has a” relationship is one that corresponds to a class and its data members. public class Date { private int year, month, day; //... } public class Employee { private Date hire_date; private double salary; //... }

6 Is-a vs. Has-a An “is a” relationship is one that corresponds to a class and the class from which it inherits. Also can be said as “is a type of”. For example: – A Faculty member is an Employee – A Staff member is an Employee – A Car is a Vehicle – A Truck is a Vehicle – A Motorcycle is a Vehicle – Can we think of any more?

7 Other inheritance relationships Can you think of anything else that has a “has a” relationship? – Rooms have doors, windows, etc. Can you think of anything else that has an “is a” relationship? – Bedroom is a Room – Dining room is a Room – How about Animals?

8 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

9 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

10 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;

11 Bank account What methods might we need?

12 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)

13 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 Inheritance ideas

14 Inheritance If class A inherits from class B, then class A is a subclass of B and class B is a superclass of A. We also talk of class A as being class B's child and class B being class A's parent. We can draw the inheritance relationships hierarchy by having superclass(es) at the top and subclass(es) lower, using lines to connect where there's an inheritance relationship.

15 Inheritance Class A may inherit from class B which can in turn, inherit from class C. (draw on the board.) However, this is not considered multiple inheritance. Multiple inheritance is an object-oriented concept that allows a subclass to inherit from more than one class --- this is NOT allowed in Java. e.g. Class D cannot inherit from both class E and F directly. (draw on the board.)

16 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.

17 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()

18 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.

19 Hierarchy of the account classes SavingsAccount and CheckingAccount each inherit from Account What about AccountTester? How about the Object class?


Download ppt "CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google