Presentation is loading. Please wait.

Presentation is loading. Please wait.

10-1 Intro Inheritance1 of 4 Inheritance is a mechanism for enhancing existing classes. Inheritance is a mechanism for enhancing existing classes. A new.

Similar presentations


Presentation on theme: "10-1 Intro Inheritance1 of 4 Inheritance is a mechanism for enhancing existing classes. Inheritance is a mechanism for enhancing existing classes. A new."— Presentation transcript:

1 10-1 Intro Inheritance1 of 4 Inheritance is a mechanism for enhancing existing classes. Inheritance is a mechanism for enhancing existing classes. A new more specific class that has all the traits of an existing class has the capability to inherit all the data & methodology from the more general class. A new more specific class that has all the traits of an existing class has the capability to inherit all the data & methodology from the more general class. Consider a “SavingsAcct” class which pays interest on the balance. We can have “SavingsAcct” inherit from the more general “BankAcct” class without having to reinvent the wheel(reuse). Consider a “SavingsAcct” class which pays interest on the balance. We can have “SavingsAcct” inherit from the more general “BankAcct” class without having to reinvent the wheel(reuse). public class SavingsAcct extends BankAcct public class SavingsAcct extends BankAcct{ // new methods, overloaded/overridden methods & instance fields } All methods & data of “BankAcct” are inherited by “SavingsAcct”. All methods & data of “BankAcct” are inherited by “SavingsAcct”.  Be aware of access privileges though! Consider: Consider:  SavingsAcct s = new SavingsAcct(3.5);  s.deposit(1000);// method from “BankAcct”!  s.deposit(1000);// method from “BankAcct”!

2 10-1 Intro Inheritance Cont.2 of 4 Terminology: Terminology:  Superclass – Base class or more general class (“BankAcct”);  Subclass – Class inheriting or more specific class(“SavingsAcct”). Every class in Java extends from the Object class. Every class in Java extends from the Object class.  The Object class is the lowest common denominator among all classes.  The object class has some common methods which all class objects have access to : .toString( ) .equals(Object obj)  The Programmer must override these methods for specific meaning!  Overriding Overloading?

3 10-1 Intro Inheritance Cont.3 of 4 In-Class Work: In-Class Work:  Write a subclass “SavingsAcct” extending “BankAcct” with:  Instance Field: myRate  Method:addInterest( )  Now write the client (tester) code to perform the following: 1. Create a new SavingsAcct reference. 3.25% 2. Deposit $ Calculate Interest 4. Deposit another $ Calculate Interest 6. Withdraw $ Calculate Interest 8. Print the Balance

4 Homework Homework I.Add another subclass CheckingAcct with the following attributes:  CheckingAcct extends BankAcct //data- myNumCks- incr whenever check is written. //myTransactions- incr whenever a transaction occurs. i.e. deposit( ), withdraw( ), …  writeCheck( ) Call validateFunds( )Call validateFunds( ) deduct amount from balancededuct amount from balance increment myNumCksincrement myNumCks  deductFees( ) Charge a $0.25 fee for every 10 th check written.Charge a $0.25 fee for every 10 th check written.  validateFunds( ) Verify there are enough funds to cover check.Verify there are enough funds to cover check. Charge a $5 fee if balance falls below $100.Charge a $5 fee if balance falls below $100. II.Write a transfer(BankAcct acct, double amt) method in BankAcct which transfers amt from the parameter BankAcct acct and deposits to the implicit parameter.


Download ppt "10-1 Intro Inheritance1 of 4 Inheritance is a mechanism for enhancing existing classes. Inheritance is a mechanism for enhancing existing classes. A new."

Similar presentations


Ads by Google