800 likes | 816 Views
Explore key programming concepts in Java such as inheritance, polymorphism, and exceptions. Learn how to use the extends keyword and work with class types efficiently. Enhance your coding skills with practical examples from Cay Horstmann's "Java 2 Essentials."
E N D
Lecture 3 “Just Java” Chapter 6 - Inheritance, Polymorphism and the class whose name is Class “Just Java” Chapter 7 Exceptions BankAccount example is from Cay Horstmann’s “Java 2 Essentials” Chapter 9
Inheritance and code reuse Supertype Base Class Subtype Derived Class The Extends keyword The “is a” relationship Inheritance Diagram The Object class Converting Between Class Types Polymorphism RTTI Exceptions Important Programming Concepts
// File Name BankAccount.java • public class BankAccount • { • private double balance; • public BankAccount() • { balance = 0; • } • public BankAccount(double initialBalance) • { balance = initialBalance; • } Cay Horstmann's Bank Account Example
public void deposit(double amount) • { balance = balance + amount; • } • public void withdraw(double amount) • { balance = balance - amount; • } • public double getBalance() • { return balance; • } • } Cay Horstmann's Bank Account Example
// again but let’s add more members • class BankAccount { • static private int accountNumber = 0; • private double balance; • private int acctNum; • public BankAccount() • { balance = 0; • acctNum = accountNumber++; • } • public BankAccount(double initialBalance) • { balance = initialBalance; • acctNum = accountNumber++; • } Cay Horstmann's Bank Account Example
public void deposit(double amount) { • balance = balance + amount; • } • public void withdraw(double amount) { • balance = balance - amount; • } • public int getAccountNumber() { • return acctNum; • } • public double getBalance() { • return balance; • } • } Cay Horstmann's Bank Account Example
A Savings Account “IS A” Bank Account public class SavingsAccount extends BankAccount { // new methods // new instance variables // we already have deposit, withdraw, etc. } Cay Horstmann's Bank Account Example
Inheritance Diagram Object Unlike C++, Java is singly rooted Bank Account Savings Account Cay Horstmann's Bank Account Example
Specializing the base class class SavingsAccount extends BankAccount { private double rate; public SavingsAccount(double initialDeposit, double interestRate) { super(initialDeposit); rate = interestRate; } void addInterest() { double interest = getBalance() * rate; deposit(interest); } Cay Horstmann's Bank Account Example
public static void main(String args[]) { SavingsAccount rainyDay = new SavingsAccount(100,.10); rainyDay.addInterest(); rainyDay.addInterest(); System.out.println(rainyDay.getAccountNumber()); System.out.println(rainyDay.getBalance()); SavingsAccount collegeFund = new SavingsAccount(1000,.10); collegeFund.addInterest(); collegeFund.addInterest(); System.out.println(collegeFund.getAccountNumber()); System.out.println(collegeFund.getBalance()); } } Cay Horstmann's Bank Account Example
Output C:\heinz\90-876\examples\Inherit\BankAccount>java SavingsAccount 0 121.0 1 1210.0 Cay Horstmann's Bank Account Example
Converting Between Class Types A SavingsAccount “is a” BankAccount. A BankAccount “is a” Object. Is the following legal? Object o = new SavingsAccount(100,.10); Cay Horstmann's Bank Account Example
Converting Between Class Types Object o = new SavingsAccount(100,.10); Assignment is fine! An Object Cay Horstmann's Bank Account Example
Converting Between Class Types Is the following legal? SavingsAccount s = new SavingsAccount(100,.10); Object o = s; Sure! Both references point to the same object. Cay Horstmann's Bank Account Example
Converting Between Class Types Is the following legal? SavingsAccount s = new SavingsAccount(100,.10); BankAccount b = s; Sure! Cay Horstmann's Bank Account Example
Converting Between Class Types Is the following legal? SavingsAccount s = new SavingsAccount(100,.10); BankAccount b = s; b.addInterest() Cay Horstmann's Bank Account Example
Converting Between Class Types Is the following legal? SavingsAccount s = new SavingsAccount(100,.10); BankAccount b = s; b.addInterest() NO! A BankAccount object has no addInterest method! Cay Horstmann's Bank Account Example
Converting Between Class Types Is the following legal? SavingsAccount s = new SavingsAccount(100,.10); BankAccount b = s; ((SavingsAccount)b).addInterest(); Yes! We tell the compiler we will take the risk! Cay Horstmann's Bank Account Example
Converting Between Class Types How about the following? SavingsAccount s = new SavingsAccount(100,.10); Object o = s; ((SavingsAccount)o).addInterest(); Cay Horstmann's Bank Account Example
Converting Between Class Types How about the following? SavingsAccount s = new SavingsAccount(100,.10); Object o = s; ((SavingsAccount)o).addInterest(); Sure! Why? Are we taking a risk? Cay Horstmann's Bank Account Example
Converting Between Class Types How about the following? SavingsAccount s = new SavingsAccount(100,.10); Rectangle r = s; ((SavingsAccount)r).addInterest(); Cay Horstmann's Bank Account Example
Converting Between Class Types How about the following? SavingsAccount s = new SavingsAccount(100,.10); Rectangle r = s; ((SavingsAccount)r).addInterest(); No! Why? Cay Horstmann's Bank Account Example
Inheritance Diagram Object Rectangle Bank Account Savings Account Cay Horstmann's Bank Account Example
Polymorphism Consider the following static method: public void static display(BankAccount b) { System.out.println(“Acct. Number:” + b.getAccountNumber()); System.out.println(“Balance $” + b.getBalance()); } Cay Horstmann's Bank Account Example
Polymorphism Consider the following static method: public void static display(BankAccount b) { System.out.println(“Acct. Number:” + b.getAccountNumber()); System.out.println(“Balance $” + b.getBalance()); } // Suppose we call it with the following code. What happens? public static void main(String args[]) { BankAccount collegeFund = new BankAccount(100); display(collegeFund); } Cay Horstmann's Bank Account Example
Polymorphism How about with this code? public void static display(BankAccount b) { System.out.println(“Acct. Number:” + b.getAccountNumber()); System.out.println(“Balance $” + b.getBalance()); } public static void main(String args[]) { SavingsAccount rainyDay = new SavingsAccount(100,.10); display(rainyDay); } Cay Horstmann's Bank Account Example
Inheritance Diagram (UML) Object Rectangle BankAccount SavingsAccount CDAccount Cay Horstmann's Bank Account Example
Polymorphism Is this OK? public void static display(BankAccount b) { System.out.println(“Acct. Number:” + b.getAccountNumber()); System.out.println(“Balance $” + b.getBalance()); } public static void main(String args[]) { CDAccount retirement= new CDAccount(100,.10,5); display(retirement); } Cay Horstmann's Bank Account Example
Java Access Control public Interface Access Only accessible within the class private protected “Sort of private” Cay Horstmann's Bank Account Example
Accessible to Member Visibility Public Protected Package Private Same class Class in same package Subclass indifferent package Non-subclass different package Yes Yes Yes Yes Yes Yes Yes Yes No Yes No No Yes No No No Java Access Control Class member accessibility Classes are either public or package access Cay Horstmann's Bank Account Example
Interfaces and Abstract classes Cay Horstmann's Bank Account Example
Review Inheritance • The inheritance relationship is often called the “is-a” • relationship. • For example, a CheckingAccount “is-a” BankAccount. • Now, suppose that we have a routine that manipulates • BankAccount objects -- • static void foo(BankAccount x) { • // do things to x • } • What kind of things can foo() do to x? Cay Horstmann's Bank Account Example
Inheritance static void foo(BankAccount x) { x.withdraw(1000); x.deposit(50.0); : : } foo() can call those methods on x that are provided by the BankAccount class Cay Horstmann's Bank Account Example
Inheritance • // A CheckingAccount “is-a” BankAccount. • class CheckingAccount extends BankAccount { • } • static void foo(BankAccount x) { • // do things to x • } • Should we be able to pass a CheckingAccount object to • this routine? Cay Horstmann's Bank Account Example
Inheritance • // A CheckingAccount “is-a” BankAccount. • class CheckingAccount extends BankAccount { • } • static void foo(BankAccount x) { • // do things to x • } • Should we be able to pass a CheckingAccount object to this • routine? SURE!! Cay Horstmann's Bank Account Example
Inheritance Often we want to write a method that is able to handle any object that meets certain requirements. In the example above, foo() requires that it receive BankAccount objects. The objects may be CD Account objects or Checking Account objects etc.. As long as the object that is passed to foo() extends the BankAccount class, the writer of foo() knows that the object has methods called “deposit” and “withdraw”. Since the object “is a” BankAccount, we are promised that certain operations will be available. Interfaces take this a step further… Cay Horstmann's Bank Account Example
INTERFACES • Interfaces • Replace multiple inheritance • Have no instance variables • Have only abstract methods (all parameters but no bodies) • Have only public methods • Are implemented not extended as in inheritance • Are not classes…you can’t create interface objects • May be referenced • May contain constants (all are public static final by default) Cay Horstmann's Bank Account Example
The BankAccount Again interface Account { public double getBalance(); public void setBalance(double x); } Any class that implements this interface MUST have these two methods defined exactly as specified. Cay Horstmann's Bank Account Example
class BankAccount implements Account { private double balance; private double rate; public BankAccount() { balance = 0; } public BankAccount(double initialBalance, double arate) { rate = arate / 100;; balance = initialBalance; } Cay Horstmann's Bank Account Example
public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance() { return balance; } Cay Horstmann's Bank Account Example
public void setBalance(double a) { balance = a; } public void update() { balance = balance + balance * rate; } } We have provided implementations for the two methods. Cay Horstmann's Bank Account Example
public class BankAccountTest { public static void main(String[] args) { BankAccount myAccount = new BankAccount(1000,10); int month; for (month = 1; month <= 2; month++) { myAccount.update(); } myAccount.deposit(100); foo(myAccount); } Call foo() with an object that implements interface Account Cay Horstmann's Bank Account Example
public static void foo(Account a) { double m = a.getBalance(); System.out.println(m); } } The name of an interface Any other class that implements Account can be passed to foo(). Cay Horstmann's Bank Account Example
Consider A Student Class • public class Student { • } • Student x = new Student(“Joe”,2.3); • Student y = new Student(“Zack”,1.7); • Student z = new Student(“Amy”,3.0); How would you put these three in order? Cay Horstmann's Bank Account Example
Consider A Student Class • public class Student { • } • Student x = new Student(“Joe”,2.3); • Student y = new Student(“Zack”,3.7); • Student z = new Student(“Amy”,3.0); It depends on how they are compared. Cay Horstmann's Bank Account Example
INTERFACES • public interface Comparable { • int compareTo(Object other); • } • public class Student implements Comparable { • // this class MUST define compareTo() • } Automatically public Found in java.lang Cay Horstmann's Bank Account Example
INTERFACES • Suppose we have a function foo • void foo(Comparable x[]) { • } • Can we pass an array of Student objects to this function? • Why would we want to? Cay Horstmann's Bank Account Example
INTERFACES • Suppose we have a function foo • void foo(Comparable x[]) { • } • Can we pass an array of Student objects to this function? SURE • Why would we want to? Perhaps foo() sorts. Cay Horstmann's Bank Account Example
Abstract Classes • When you extend an existing class, you have a choice whether • or not to redefine the methods of the superclass. If you don’t • redefine the method, it will appear in the derived class as it appears • in the superclass. • Sometimes it is desirable to force derived class programmers • to redefine a method • There may be no good (superclass) default for the method • Only the subclass programmer can know how to implement • the method Cay Horstmann's Bank Account Example
Abstract Classes • Consider • public class BankAccount { • public void deductFees() { …body goes here ? … } • : • : • } • public class SavingsAccount extends BankAccount{ • } What should this method do? We silently get the deductFees() method Cay Horstmann's Bank Account Example