WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.

Slides:



Advertisements
Similar presentations
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Advertisements

WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
15 min WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010/2013.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Modern Programming Tools And Techniques-I
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Lecture 12 Inheritance.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
15 min WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Presentation transcript:

WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here.here These slides contain a lot of animations. For optimal results, watch in slideshow mode.

Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); Command execute() undo() History add(Command) A code extract from a past CS2103 project (similar to yours) (a) 1 (b) 0..1 (c) * (d) 0 mul {a|b|c|d} e.g. mul b mul {a|b|c|d} e.g. mul b 77577

Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo();

Advanced OO concepts CS2103/T, Lecture 6, Part 1, [Sep 20, 2013]

Advanced OO concepts

BookStudent borrows BookStudent borrowed by

BookStudent borrows Where to put return date?

BookStudent borrows BookStudent borrows Loan returnDate

BookStudent borrows BookStudent borrows Loan returnDate BookStudentLoan returnDate

BookStudent borrows Loan returnDate

BookStudent borrows Loan returnDate Chapter

BookStudent borrows Loan returnDate Chapter

BookStudent borrows Loan returnDate ChapterShelf

BookStudent borrows Loan returnDate ChapterShelf

BookChapterShelf

BookStudent borrows Loan returnDate Chapter Shelf

BookStudent borrows Loan returnDate Chapter Shelf

BookStudent borrows Loan returnDate Chapter Shelf Where to put total students?

Book borrows Loan returnDate ChapterShelf You mean I can say Student.getTotal() ? Student -totalStudents +getTotal()

Book borrows Loan returnDate ChapterShelfStudent -totalStudents +getTotal() OK. Can I say Student.totalStudents ? You mean I can say Student.getTotal() ?

Book borrows Loan returnDate Student -totalStudents +getTotal()

Example 1 Admin I want to calculate average age of Students UGStudent PGStudent NGStudent Here you go!

Example 1 Admin Gaaah….! UGStudent PGStudent NGStudent

Example 1 Admin UGStudent PGStudent NGStudent Can? Cannot! foreach Student s: s.getAge(); … foreach Student s: s.getAge(); …

Example 2 StorageLogic Storage s; … s.load(); … Storage s; … s.load(); …

Example 2 Storage TestDriver StorageStub How? Logic

Example 2 Storage TestDriver StorageStub Logic

Example 2 Storage Storage s; … s.load(); … Storage s; … s.load(); … TestDriver StorageStub Logic setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; }

Example 2 Storage Storage s; … s.load(); … Storage s; … s.load(); … TestDriver StorageStub Logic setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; }

Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Storage TestDriver StorageStub Logic setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; } Treat one type as another and still get the behavior of the actual object Example 2Example 1

Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Storage TestDriver StorageStub Logic setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; } Treat one type as another and still get the behavior of the actual object Example 2Example 1

Different game, different behavior Same hardware/ software

= ability to take many forms

12

12

12

12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge()

12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge() :UGStudent

12 UGStudent PGStudent NGStudent Student getAge() is a

How many of these show true inheritance? Animal Dog Cat Tiger Car Engine Wheel Book TextBook Novel iPhone Telephone Camera (a) (b) (c) (d) true {0|1|2|3|4} e.g. true 3 true {0|1|2|3|4} e.g. true

12 UGStudent PGStudent NGStudent Student getAge() super class : sub class parent class : child class specialization generalization

12 UGStudent PGStudent NGStudent Student getAge() UGStudent PGStudent NGStudent inheritance tree/hierarchy

Find two things that are completely different and not similar in any way.

Java: Single class hierarchy Object HelloWorld Square FilledSquare … … … … C++: Many class hierarchies

Java: Single class hierarchy Object HelloWorld Square FilledSquare … … … … C++: Many class hierarchies

12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge() :UGStudent

12 Admin UGStudent PGStudent NGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * getAge() :UGStudent

12 Admin UGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * :UGStudent If expecting super class, can accept sub class PGStudent NGStudent

Which one of these subclasses are not substitutable? Account AccountTypeA AccountTypeB AccountTypeC Precondition: 0 < i < 100 Precondition: 0 < i < 10 Precondition: 0 < i < 100 Precondition: 0 < i < 1000 BankSystem subs {a|b|c} e.g. subs b subs {a|b|c} e.g. subs b 77577

To preserve sustitutability, sub classes should not impose preconditions that are more restrictive than the super class. AccountTypeA AccountTypeB Account AccountTypeC Precondition: 0 < i < 100 Precondition: 0 < i < 10 Precondition: 0 < i < 100 Precondition: 0 < i < 1000 BankSystem

Based on Liscov Substitution Principle by Barbara Liskov

12 Admin UGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * :UGStudent PGStudent NGStudent Example 1

12 Admin UGStudent foreach Student s: s.getAge(); … foreach Student s: s.getAge(); … Student * :UGStudent PGStudent NGStudent Example 1

12 TestDriver StorageStub Logic setStorage(Storage) Storage Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; } load() Example 2

12 TestDriver StorageStub Logic setStorage(Storage) FileStorage Storage Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); DBStorage load() All ‘load’ the same way? Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; }

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load()

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() Storage s; … s.load(); Storage s; … s.load(); At compile time… Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() );

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() At run-time… Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); Storage s; … s.load(); Storage s; … s.load(); Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; }

12 TestDriver Logic setStorage(Storage) At run-time… Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); :Logics:StorageStub Storage s; … s.load(); Storage s; … s.load(); Storage s; … void setStorage(Storage s){ this.s = s; } Storage s; … void setStorage(Storage s){ this.s = s; }

12 TestDriver Logic setStorage(Storage) Storage s; … s.load(); Storage s; … s.load(); Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); Storage load() StorageStub load() DBStorage load() FileStorage load() At run-time…

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() :Logics:?????????? At run-time… Storage s; … s.load(); Storage s; … s.load();

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() void load (){ //do nothing } void load (){ //do nothing } Storage s; … s.load(); Storage s; … s.load(); Can remove? Cannot!

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() All Storage objects should support a load method, but implementation is up to the child classes

12 TestDriver Logic setStorage(Storage) Storage load() StorageStub load() DBStorage load() FileStorage load() abstract void load () ; virtual void load () = 0

12 TestDriver Logic setStorage(Storage) Storage load() {abstract} StorageStub load() DBStorage load() FileStorage load() Incomplete! Cannot create objects!

12 TestDriver Logic setStorage(Storage) {abstract} Storage load() {abstract} StorageStub load() DBStorage load() FileStorage load()

12 TestDriver Logic setStorage(Storage) {abstract} Storage load() {abstract} init() store() StorageStub load() DBStorage load() FileStorage load()

12 TestDriver Logic setStorage(Storage) {abstract} Storage load() {abstract} init() {abstract} store(){abstract} StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store()

12 TestDriver Logic setStorage(Storage) > Storage load() init() store() StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store()

12 TestDriver Logic setStorage(Storage) > Storage load() init() store() StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store() 2

12 TestDriver Logic setStorage(Storage) > Storage load() init() store() StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store() 2 Logic logic; … logic.setStorage(new StorageStub() ); Logic logic; … logic.setStorage(new StorageStub() ); Storage s; void setStorage(Storage s){ this.s = s; } Storage s; void setStorage(Storage s){ this.s = s; }

12 > Storage load() init() store() 2 class interface Storage { public void load (); public void init(); } extends class StorageStub implements { … } class Storage { public: virtual void load() = 0; virtual void init() = 0; }; StorageStub load(), init(), store() DBStorage load(), init(), store() FileStorage load(), init(), store()

122 Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); Command execute() undo() Edit execute() undo() execute() undo() Delete execute() undo() execute() undo() Add execute() undo() execute() undo() History add(Command)

122 {abstract} Command execute() {abstract} undo() {abstract} Edit execute() undo() execute() undo() Delete execute() undo() execute() undo() Add execute() undo() execute() undo() History add(Command) * Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo();

Command cmd = createCommand(commandString); cmd.exectute(); history.add(cmd); … cmd.undo(); 122 > Command execute() undo() Edit execute() undo() execute() undo() Delete execute() undo() execute() undo() Add execute() undo() execute() undo() History add(Command) *

Advanced OO Concepts

… gradually, and as needed.