Presentation is loading. Please wait.

Presentation is loading. Please wait.

“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day.

Similar presentations


Presentation on theme: "“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day."— Presentation transcript:

1 “The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day

2 Test Tomorrow 3 March 2011

3 Test Covering: –Chapters 1 – 11 (More Classes and Objects) Format: –40 marks aim for mark per minute –Short answer/multiple choice section –Short programming questions

4 Questions Demonstrate your knowledge of the syntax and semantics of Java, and the important underlying concepts Be able to understand and to write methods and short code fragments –Not full programs –Possibly outline what needs to be in a class (design)

5 Chapter 11 More About Classes Inheritance

6 Overriding toString public class ChequeAccount extends Account {... public String toString () { return (accountNumber + ” bal = R” + balance); } // toString } // class ChequeAccount

7 The equals Method Compares two objects –Default: compares the references (same as == ) Can override –e.g. compare account numbers

8 Overriding equals public class ChequeAccount extends Account {... public boolean equals (Object other) { Account a = (Account)other; return accountNumber.equals(a.accountNumber); } // equals } // class ChequeAccount

9 Polymorphism It’s Greek to me! Objects can take on different forms: ChequeAccount c = new ChequeAccount(); Account a = new ChequeAccount(); ?

10 Polymorphism (cont.) The ChequeAccount object has taken on the form of an Account object It is still a ChequeAccount object Account a = new ChequeAccount(); a ChequeAccount

11 Polymorphism (cont.) Inheritance is the key: –Any object can take on the form of any of its superclasses Throwable t = new FileNotFoundException();

12 The type of the reference variable determines what we can do with object –Using polymorphism we lose access to more specialised features Polymorphism (cont.) We cannot access any overdraft information using a Account a = new ChequeAccount();

13 Overridden methods The type of the object determines which method is called Polymorphism (cont.) The ChequeAccount withdraw method is used –allows overdraft Account a = new ChequeAccount(); a.withdraw(100);

14 Why Do We Use Polymorphism? Generic methods Generic lists

15 Generic Methods This method can work with Account objects or any subclass of the Account class: –ChequeAccount –FixedDepositAccount –CreditCardAccount –etc. public void writeBalance (Account acc) { logFile.println(acc.accountNumber + ": R" + acc.balance); } // writeBalance

16 Generic Lists This array can hold any object Object list[] = new Object[100]; list[0] = ”George”; list[1] = new ChequeAccount(); list[2] = new Integer(-73); list[3] = new double[5]; list[4] = System.in;...

17


Download ppt "“The beauty of empowering others is that your own power is not diminished in the process.” – Barbara Colorose Thought for the Day."

Similar presentations


Ads by Google