860 likes | 1.08k Views
WARNING. These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010. They may not show up well on other PowerPoint versions . You can download PowerPoint 2010 viewer from here .
E N D
WARNING • These slides are not optimized for printing or exam preparation. These are for lecture delivery only. • These slides are made for PowerPoint 2010. They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here. • These slides contain a lot of animations. For optimal results, watch in slideshow mode.
A code extract from a past CS2103 project (similar to yours) Commandcmd= createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); Command History execute() undo() add(Command) (a) 1 (b) 0..1 (c) * (d) 0 mul {a|b|c|d} e.g. mul b 77577
Commandcmd= createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo();
Advanced OO concepts CS2103/T, Lecture 6, Part 1, [Sep 20, 2013]
Advanced OO concepts Abstraction Encapsulation …………………… …………………….
Advanced OO concepts Abstraction Encapsulation …………………… …………………….
borrows Book Student Book Student borrowed by
borrows Book Student Where to put return date?
borrows Book Student Book Student Loan returnDate borrows Association classes
borrows Book Student Book Student Loan Book Student Loan returnDate returnDate borrows Association classes
Book Student Loan returnDate borrows Association classes
Book Student Loan Chapter returnDate borrows
Book Student Loan Chapter returnDate borrows Composition
Book Student Loan Chapter Shelf returnDate borrows Composition
Book Student Loan Chapter Shelf returnDate borrows Composition Aggregation
Book Chapter Shelf Composition Aggregation
Composition Aggregation
Composition Aggregation
Book Student Loan Chapter Shelf returnDate borrows Aggregation Composition
Book Student Loan Chapter Shelf returnDate borrows Aggregation Composition
Book Student Loan Chapter Shelf returnDate borrows Where to put total students?
Book Loan Chapter Shelf Student returnDate -totalStudents +getTotal() borrows You mean I can say Student.getTotal() ? Class-level members
Book Loan Chapter Shelf Student returnDate -totalStudents +getTotal() borrows You mean I can say Student.getTotal() ? OK. Can I say Student.totalStudents? Class-level members
Book Loan Student returnDate -totalStudents +getTotal() borrows Class-level members
Abstraction Encapsulation …………………… …………………….
Example 1 I want to calculate average age of Students Here you go! UGStudent Admin NGStudent PGStudent
Example 1 Gaaah….! UGStudent Admin NGStudent PGStudent
Example 1 foreachStudent s: s.getAge(); … Can? Cannot! UGStudent Admin NGStudent PGStudent
Example 2 Storage s; … s.load(); … Logic Storage
Example 2 How? TestDriver Logic Storage StorageStub
Example 2 TestDriver Logic Storage StorageStub Dependency Injection
Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection
Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection
Treat one type as another and still get the behavior of the actual object Example 1 Example 2 foreachStudent s: s.getAge(); … TestDriver Logic Storage setStorage(Storage) StorageStub UGStudent Storage s; … void setStorage(Storage s){ this.s = s; } Admin NGStudent PGStudent
Treat one type as another and still get the behavior of the actual object Example 1 Example 2 foreachStudent s: s.getAge(); … TestDriver Logic Storage setStorage(Storage) StorageStub UGStudent Storage s; … void setStorage(Storage s){ this.s = s; } Admin NGStudent Polymorphism PGStudent
Different game, different behavior Same hardware/ software Polymorphism
= ability to take many forms Polymorphism
2 1 Polymorphism Polymorphism
2 1 Polymorphism
2 1 Inheritance
2 1 Inheritance foreachStudent s: s.getAge(); … UGStudent * Admin Student NGStudent getAge() PGStudent
2 1 Inheritance foreachStudent s: s.getAge(); … :UGStudent UGStudent * Admin Student NGStudent getAge() PGStudent
2 1 Inheritance is a UGStudent Student NGStudent getAge() PGStudent
How manyof these show true inheritance? Cat Tiger (a) Animal Dog (d) Camera Wheel (b) iPhone Car Telephone Engine (c) Book Novel TextBook true {0|1|2|3|4} e.g. true 3 77577
2 1 Inheritance superclass :subclass parentclass :childclass UGStudent Student NGStudent getAge() PGStudent specialization generalization