cmpt 373 software development methods object oriented...
TRANSCRIPT
![Page 1: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/1.jpg)
CMPT 373Software Development Methods
Nick [email protected]
Object Oriented Programming& Inheritance
![Page 2: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/2.jpg)
Why care about Object Oriented Programming?
● Superficial reasons are straightforward:
![Page 3: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/3.jpg)
Why care about Object Oriented Programming?
● Superficial reasons are straightforward:– Much of the world’s code is written in OO languages– Employers consider OOP a core introductory skill– Chances are you will need to understand it to get a job
![Page 4: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/4.jpg)
Why care about Object Oriented Programming?
● Superficial reasons are straightforward:– Much of the world’s code is written in OO languages– Employers consider OOP a core introductory skill– Chances are you will need to understand it to get a job
● But there are detractors– “OOP is inefficient”– “OOP makes it hard to share data easily”
![Page 5: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/5.jpg)
Why care about Object Oriented Programming?
● Superficial reasons are straightforward:– Much of the world’s code is written in OO languages– Employers consider OOP a core introductory skill– Chances are you will need to understand it to get a job
● But there are detractors– “OOP is inefficient”– “OOP makes it hard to share data easily”
● Applied well and thoughtfully, it helps solve real problems– Like any tool, if you apply it poorly, it won’t work well– If you apply it universally or dogmatically, you will miss out on better tools– You need to know how to use a tool to get value out of it
![Page 6: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/6.jpg)
Why care about Object Oriented Programming?
● Superficial reasons are straightforward:– Much of the world’s code is written in OO languages– Employers consider OOP a core introductory skill– Chances are you will need to understand it to get a job
● But there are detractors– “OOP is inefficient”– “OOP makes it hard to share data easily”
● Applied well and thoughtfully, it helps solve real problems– Like any tool, if you apply it poorly, it won’t work well– If you apply it universally or dogmatically, you will miss out on better tools– You need to know how to use a tool to get value out of it
● OOP will not solve your design for you, but it can be an effective tool
![Page 7: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/7.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)
![Page 8: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/8.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1
![Page 9: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/9.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1– You will be competing on the job market with students doing it from year 1
![Page 10: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/10.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1– You will be competing on the job market with students doing it from year 1
● But many schools teach it very badly– OOP textbooks were notoriously bad in the early 2000s– Many were written by people who did not know what they were doing– Many faculty did not learn it well themselves
![Page 11: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/11.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1– You will be competing on the job market with students doing it from year 1
● But many schools teach it very badly– OOP textbooks were notoriously bad in the early 2000s– Many were written by people who did not know what they were doing– Many faculty did not learn it well themselves– This is one of the reasons people complain about OOP
![Page 12: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/12.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1– You will be competing on the job market with students doing it from year 1
● But many schools teach it very badly– OOP textbooks were notoriously bad in the early 2000s– Many were written by people who did not know what they were doing– Many faculty did not learn it well themselves– This is one of the reasons people complain about OOP
● Our goal with OOP is to make you better than that
![Page 13: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/13.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1– You will be competing on the job market with students doing it from year 1
● But many schools teach it very badly– OOP textbooks were notoriously bad in the early 2000s– Many were written by people who did not know what they were doing– Many faculty did not learn it well themselves– This is one of the reasons people complain about OOP
● Our goal with OOP is to make you better than that– Regardless of the language you work in, I recommend:
Effective Java, C++ Coding Standards, Practical Object-Oriented Design in Ruby
![Page 14: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/14.jpg)
Our Goal
● I will assume you have basic, introductory, OOP experience– Most schools teach this in year 1 (ours does a little & is aiming to get better)– Most employers will expect you to have seen it from year 1– You will be competing on the job market with students doing it from year 1
● But many schools teach it very badly– OOP textbooks were notoriously bad in the early 2000s– Many were written by people who did not know what they were doing– Many faculty did not learn it well themselves– This is one of the reasons people complain about OOP
● Our goal with OOP is to make you better than that– Regardless of the language you work in, I recommend:
Effective Java, C++ Coding Standards, Practical Object-Oriented Design in Ruby
Treat these as guides rather than laws.Dogma has no value. Understand the cost/benefit.
![Page 15: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/15.jpg)
What is OOP?
● This is a matter of more debate than expected
![Page 16: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/16.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism
![Page 17: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/17.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism– “An object is a value exporting a procedural interface to data or behavior”[Cook 2009]
![Page 18: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/18.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism– “An object is a value exporting a procedural interface to data or behavior”– Objects are “sites of higher level behaviors more appropriate for use as
dynamic components”
[Cook 2009]
[Kay 1993]
![Page 19: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/19.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism– “An object is a value exporting a procedural interface to data or behavior”– Objects are “sites of higher level behaviors more appropriate for use as
dynamic components”
● Intuitively– Objects provide interchangeable services supporting higher level goals
[Cook 2009]
[Kay 1993]
[Aldrich 2013]
![Page 20: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/20.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism– “An object is a value exporting a procedural interface to data or behavior”– Objects are “sites of higher level behaviors more appropriate for use as
dynamic components”
● Intuitively– Objects provide interchangeable services supporting higher level goals– You can think of OOP as like writing a library for a task
[Cook 2009]
[Kay 1993]
[Aldrich 2013]
![Page 21: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/21.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism– “An object is a value exporting a procedural interface to data or behavior”– Objects are “sites of higher level behaviors more appropriate for use as
dynamic components”
● Intuitively– Objects provide interchangeable services supporting higher level goals– You can think of OOP as like writing a library for a task– OOP is about decoupling implementation from use
[Cook 2009]
[Kay 1993]
[Aldrich 2013]
![Page 22: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/22.jpg)
What is OOP?
● This is a matter of more debate than expected
● Classically:– A combination of data and code– Abstraction, Encapsulation, Inheritance, Polymorphism– “An object is a value exporting a procedural interface to data or behavior”– Objects are “sites of higher level behaviors more appropriate for use as
dynamic components”
● Intuitively– Objects provide interchangeable services supporting higher level goals– You can think of OOP as like writing a library for a task– OOP is about decoupling implementation from use
[Cook 2009]
[Kay 1993]
[Aldrich 2013]
Consider out maze prototyping example.Have we already seen one way this can be useful?
![Page 23: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/23.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 24: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/24.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 25: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/25.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has– Methods define the behaviors
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 26: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/26.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has– Methods define the behaviors– Visibility modifiers enable deciding what
is published to the outside
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 27: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/27.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has– Methods define the behaviors– Visibility modifiers enable deciding what
is published to the outside
– Nested constructs enable the use of scoped enums, classes, aliases, ...
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 28: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/28.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has– Methods define the behaviors– Visibility modifiers enable deciding what
is published to the outside
– Nested constructs enable the use of scoped enums, classes, aliases, ...
– Virtual methods & inheritance enablederived classes with the attributes of base classes
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 29: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/29.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has– Methods define the behaviors– Visibility modifiers enable deciding what
is published to the outside
– Nested constructs enable the use of scoped enums, classes, aliases, ...
– Virtual methods & inheritance enablederived classes with the attributes of base classes
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
class Person {public: Person(); virtual ~Person() = default;
virtual void sleep() = 0;};
![Page 30: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/30.jpg)
Review of classes (the same exist in Java, .NET, ...)
● Classes describe the services of objects– Objects are instances of classes– Fields define the state an object has– Methods define the behaviors– Visibility modifiers enable deciding what
is published to the outside
– Nested constructs enable the use of scoped enums, classes, aliases, ...
– Virtual methods & inheritance enablederived classes with the attributes of base classes
class Student : public Person {public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
class Person {public: Person(); virtual ~Person() = default;
virtual void sleep() = 0;};
void processPerson(Person& p);...Student s{Student::Degree::PHD};processPerson(s);
![Page 31: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/31.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist
![Page 32: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/32.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist– Key: Every guideline has a reason.
Every guideline has exceptions.Understand the reason to perform cost-benefit analysis.
![Page 33: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/33.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist– Key: Every guideline has a reason.
Every guideline has exceptions.Understand the reason to perform cost-benefit analysis.
● Most common examples:– SOLID
![Page 34: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/34.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist– Key: Every guideline has a reason.
Every guideline has exceptions.Understand the reason to perform cost-benefit analysis.
● Most common examples:– SOLID
● Single Responsibility● Open/Closed (more later)● Liskov Substitutability● Interface Segregation● Dependency inversion
![Page 35: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/35.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist– Key: Every guideline has a reason.
Every guideline has exceptions.Understand the reason to perform cost-benefit analysis.
● Most common examples:– SOLID
● Single Responsibility● Open/Closed (more later)● Liskov Substitutability● Interface Segregation● Dependency inversion
– DRY (Don’t Repeat Yourself)
![Page 36: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/36.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist– Key: Every guideline has a reason.
Every guideline has exceptions.Understand the reason to perform cost-benefit analysis.
● Most common examples:– SOLID
● Single Responsibility● Open/Closed (more later)● Liskov Substitutability● Interface Segregation● Dependency inversion
– DRY (Don’t Repeat Yourself)– These in particular are abused via dogma and misapplication
![Page 37: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/37.jpg)
General guidelines for classes
● Several guidelines & rules of thumb exist– Key: Every guideline has a reason.
Every guideline has exceptions.Understand the reason to perform cost-benefit analysis
● Most common examples:– SOLID
● Single Responsibility● Open/Closed (more later)● Liskov Substitutability● Interface Segregation● Dependency inversion
– DRY (Don’t Repeat Yourself)– These in particular are abused via dogma and misapplication
All of these relate to Ousterhout’s complexity criteria,but blind application can be worse.
![Page 38: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/38.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
class Thing { // Thing() // Thing(const Thing&); // Thing(Thing&&); // [virtual] ~Thing(); // Thing& operator==(const Thing&); // Thing& operator==(Thing&&);};
class Thing { // Thing()}
![Page 39: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/39.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
class RGBColor {public: RGBColor(const Intensity r, const Intensity g, const Intensity b);
Hue convertToHue() const;
private: const Intensity red; const Intensity green; const Intensity blue;};
![Page 40: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/40.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
class RGBColor {public: RGBColor(const Intensity r, const Intensity g, const Intensity b);
Hue convertToHue() const;
private: const Intensity red; const Intensity green; const Intensity blue;};
![Page 41: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/41.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
class RGBColor {public: RGBColor(const Intensity r, const Intensity g, const Intensity b);
Hue convertToHue() const;
private: const Intensity red; const Intensity green; const Intensity blue;};
![Page 42: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/42.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibilitytemplate<typename T>class Set {public: Set();
void insert(const T& toAdd);
bool contains(const T& toFind) const;
private: std::vector<T> elements;};
![Page 43: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/43.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibilitytemplate<typename T>class Set {public: Set();
void insert(const T& toAdd);
bool contains(const T& toFind) const;
private: std::vector<T> elements;};
![Page 44: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/44.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibilitytemplate<typename T>class Set {public: Set();
void insert(const T& toAdd);
bool contains(const T& toFind) const;
private: std::vector<T> elements;};
![Page 45: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/45.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibilitytemplate<typename T>class Set {public: Set();
void insert(const T& toAdd);
bool contains(const T& toFind) const;
private: std::vector<T> elements;};
struct Point { int x; int y;};
![Page 46: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/46.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibility
● Refer to objects by interfaces when applicable
![Page 47: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/47.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibility
● Refer to objects by interfaces when applicableTreeTraversal
DepthFirstTraversal BreadthFirstTraversal
![Page 48: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/48.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibility
● Refer to objects by interfaces when applicableTreeTraversal
DepthFirstTraversal BreadthFirstTraversal
voidprintTree(const Tree& tree, const TreeTraversal& t) { t.traverse(tree, printNode);}
![Page 49: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/49.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibility
● Refer to objects by interfaces when applicable
● Don’t give away your internals
![Page 50: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/50.jpg)
General guidelines for classes (common)
● Be careful about compiler provided methods
● Minimize mutability
● Minimize visibility
● Refer to objects by interfaces when applicable
● Don’t give away your internalsclass IntBuffer {public: ... std::vector& getContents(); ...private: std::vector<int> integers;};
![Page 51: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/51.jpg)
General guidelines for classes
● Prefer dependency injection to hardwiring resources [Block 2001,2018]
– Objects that allocate their own state are hard to:prove correct, extend, configure, test, ...
![Page 52: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/52.jpg)
General guidelines for classes
● Prefer dependency injection to hardwiring resources [Block 2001,2018]
– Objects that allocate their own state are hard to:prove correct, extend, configure, test, ...
class CrosswordGenerator { CrosswordGenerator() : clues{std::make_unique<Clues>} { }
private: std::unique_ptr<Clues> clues;};
![Page 53: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/53.jpg)
General guidelines for classes
● Prefer dependency injection to hardwiring resources [Block 2001,2018]
– Objects that allocate their own state are hard to:prove correct, extend, configure, test, ...
class CrosswordGenerator { CrosswordGenerator() : clues{std::make_unique<Clues>} { }
private: std::unique_ptr<Clues> clues;};
class CrosswordGenerator { CrosswordGenerator(... clues) : clues{std::move(clues)} { }
private: std::unique_ptr<Clues> clues;};
![Page 54: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/54.jpg)
General guidelines for classes
● Prefer dependency injection to hardwiring resources [Block 2001,2018]
– Objects that allocate their own state are hard to:prove correct, extend, configure, test, ...
class CrosswordGenerator { CrosswordGenerator() : clues{std::make_unique<Clues>} { }
private: std::unique_ptr<Clues> clues;};
class CrosswordGenerator { CrosswordGenerator(... clues) : clues{std::move(clues)} { }
private: std::unique_ptr<Clues> clues;};
auto englishClues = ...CrosswordGenerator cg{englishClues};
auto frenchClues = ...CrosswordGenerator cg{frenchClues};
![Page 55: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/55.jpg)
General guidelines for classes
● Prefer dependency injection to hardwiring resources [Block 2001,2018]
– Objects that allocate their own state are hard to:prove correct, extend, configure, test, ...
class CrosswordGenerator { CrosswordGenerator() : clues{std::make_unique<Clues>} { }
private: std::unique_ptr<Clues> clues;};
class CrosswordGenerator { CrosswordGenerator(... clues) : clues{std::move(clues)} { }
private: std::unique_ptr<Clues> clues;};
auto englishClues = ...CrosswordGenerator cg{englishClues};
auto frenchClues = ...CrosswordGenerator cg{frenchClues};
Separating the creation of objectsfrom the wiring of objects
creates a more flexible system
![Page 56: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/56.jpg)
General guidelines for classes
● Some are specific to “native code”:Use the PIMPL idiom judiciously [Sutter & Alexandrescu 2005]
– Prevents unnecessary recompilation– Allows the layout to change without breaking ABI in long lived projects
![Page 57: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/57.jpg)
General guidelines for classes
● Some are specific to “native code”:Use the PIMPL idiom judiciously [Sutter & Alexandrescu 2005]
– Prevents unnecessary recompilation– Allows the layout to change without breaking ABI in long lived projects
class Thing {public: Thing(); void doStuff() const;
private: class ThingImpl; std::unique_ptr<ThingImpl> impl;};
Thing.h
![Page 58: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/58.jpg)
General guidelines for classes
● Some are specific to “native code”:Use the PIMPL idiom judiciously [Sutter & Alexandrescu 2005]
– Prevents unnecessary recompilation– Allows the layout to change without breaking ABI in long lived projects
class Thing {public: Thing(); void doStuff() const;
private: class ThingImpl; std::unique_ptr<ThingImpl> impl;};
Thing.h
Thing::Thing() : impl{std::make_unique<ThingImpl>()} { }
voidThing::doStuff() const { impl->doStuff();}
Thing.cpp
![Page 59: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/59.jpg)
Thinking in terms of services
● Modern thinking notes that OOP defines services– Inheritance & runtime polymorphism drive this
![Page 60: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/60.jpg)
Thinking in terms of services
● Modern thinking notes that OOP defines services– Inheritance & runtime polymorphism drive this– Base classes define an interface
List+ begin()+ end()+ push_back()+ clear()
![Page 61: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/61.jpg)
Thinking in terms of services
● Modern thinking notes that OOP defines services– Inheritance & runtime polymorphism drive this– Base classes define an interface– Derived classes provide implementations
List+ begin()+ end()+ push_back()+ clear()
ArrayList+ begin()+ end()+ push_back()+ clear()
LinkedList+ begin()+ end()+ push_back()+ clear()
![Page 62: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/62.jpg)
Thinking in terms of services
● Modern thinking notes that OOP defines services– Inheritance & runtime polymorphism drive this– Base classes define an interface– Derived classes provide implementations– Implementations are interchangeable even at runtime (like remote services)
List+ begin()+ end()+ push_back()+ clear()
ArrayList+ begin()+ end()+ push_back()+ clear()
LinkedList+ begin()+ end()+ push_back()+ clear()
![Page 63: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/63.jpg)
Thinking in terms of services
● Modern thinking notes that OOP defines services– Inheritance & runtime polymorphism drive this– Base classes define an interface– Derived classes provide implementations– Implementations are interchangeable even at runtime (like remote services)
List+ begin()+ end()+ push_back()+ clear()
ArrayList+ begin()+ end()+ push_back()+ clear()
LinkedList+ begin()+ end()+ push_back()+ clear()
voidtransferStudents(List<Student>& from, List<Student>& to) { ranges::copy(from, std::back_inserter(to)); from.clear();}
![Page 64: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/64.jpg)
Thinking in terms of services
● Modern thinking notes that OOP defines services– Inheritance & runtime polymorphism drive this– Base classes define an interface– Derived classes provide implementations– Implementations are interchangeable even at runtime (like remote services)
● This also enables heterogeneous aggregatesvoidletThePeopleSleep(List<Person*>& people) { for (Person* person : people) { person->sleep(); }}
Student
Student
Faculty
Staff
![Page 65: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/65.jpg)
So let’s try it out...
Note: We will go from absurd to practical
![Page 66: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/66.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
![Page 67: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/67.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
![Page 68: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/68.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
class CarOwner : public Person, Car { };
![Page 69: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/69.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Is this good or bad?Why?
class CarOwner : public Person, Car { };
![Page 70: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/70.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Is this good or bad?Why?
class CarOwner : public Person, Car { };
How could you make it better?
![Page 71: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/71.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Person
CarCarOwnerhas-a
is-a
![Page 72: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/72.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Even simpler?
Person
CarCarOwnerhas-a
is-a
![Page 73: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/73.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Person
CarCarOwnerhas-a
is-a
CarPersonhas-a
![Page 74: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/74.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Person
CarCarOwnerhas-a
is-a
CarPersonhas-a
That a car is amongst a Person’s possessionsdoes not make them a special Person
![Page 75: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/75.jpg)
So let’s try it out...
● Suppose we want to model a person who owns a car...
Person Car
CarOwner
is-ais-a
Person
CarCarOwnerhas-a
is-a
CarPersonhas-aThis absurd example captures
common, subtle mistakes
That a car is amongst a Person’s possessionsdoes not make them a special Person
![Page 76: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/76.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
![Page 77: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/77.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
Frogs can be male or female
![Page 78: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/78.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
Frog
MaleFrog FemaleFrog
Frogs can be male or female
![Page 79: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/79.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
Frog
MaleFrog FemaleFrog
Frog-sex:{male,female}
Frogs can be male or female
![Page 80: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/80.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!
![Page 81: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/81.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
![Page 82: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/82.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Professor
![Page 83: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/83.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcheris-a
Professor
![Page 84: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/84.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcheris-a
Teacheris-a
Professor
![Page 85: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/85.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcheris-a
Teacher Napperis-a is-a
Professor
![Page 86: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/86.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcher
Professor
has-a
![Page 87: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/87.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcher
Professor
has-a
Teacherhas-a
![Page 88: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/88.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcher
Professor
has-a
Teacherhas-aNapper
has-a
![Page 89: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/89.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
Researcher
Professor
has-a
Teacherhas-a
Napperhas-a
Note, these are now roles,not people.
![Page 90: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/90.jpg)
So why is inheritance hard?
● Do the LSP and has-a relationships unambiguously tell us how to apply inheritance?
● Every is-a relationship could instead be has-a!– These often capture finer grained relationships– Break individual responsibilities into components
● Whenever is-a applies, you must still make a decision
Researcher
Professor
has-a
Teacherhas-a
Napperhas-a
Note, these are now roles,not people.
![Page 91: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/91.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it
![Page 92: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/92.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it
![Page 93: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/93.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
![Page 94: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/94.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
Frog
MaleFrog FemaleFrog
Frog-sex:{male,female}
![Page 95: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/95.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
Frogs and other animalscan spontaneously change sex!
Frog
MaleFrog FemaleFrog
Frog-sex:{male,female}
![Page 96: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/96.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
Frogs and other animalscan spontaneously change sex!
Frog
MaleFrog FemaleFrog
Frog-sex:{male,female}
Knowing in advance is hard.Composition is flexible & adapts to requirements.
![Page 97: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/97.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
● Guide 2: Might the type be used polymorphically?– Composition does not intrinsically aid it
![Page 98: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/98.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
● Guide 2: Might the type be used polymorphically?– Composition does not intrinsically aid it– Inheritance enables it
![Page 99: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/99.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
● Guide 2: Might the type be used polymorphically?– Composition does not intrinsically aid it– Inheritance enables it– Consider inheritance when a reference to a general type may point to a
more specific one.
![Page 100: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/100.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
● Guide 2: Might the type be used polymorphically?– Composition does not intrinsically aid it– Inheritance enables it– Consider inheritance when a reference to a general type may point to a
more specific one.
0) Student1) Student2) Lecturer3) Professor4) Student
std::vector<People*> folks;
![Page 101: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/101.jpg)
Choosing is-a or has-a
● Guide 1: Might the behavior need to change?– Coarse inheritance often precludes it– Composition often simplifies it– Use coarse grained composition if the relationship is dynamic
● Guide 2: Might the type be used polymorphically?– Composition does not intrinsically aid it– Inheritance enables it– Consider inheritance when a reference to a general type may point to a
more specific one.
0) Student1) Student2) Lecturer3) Professor4) Student
std::vector<People*> folks;
We will revisit this in the context ofalgebraic data types.
![Page 102: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/102.jpg)
So let’s try it out...
● I need– Many different types of animals. This should sound
familiar...
![Page 103: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/103.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().
![Page 104: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/104.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
![Page 105: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/105.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
What does my design look likebased on the rules?
![Page 106: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/106.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Animal
ProfessorCat CorgiParrot
Maine Coon Bengal
![Page 107: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/107.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Animal
ProfessorCat CorgiParrot
Maine Coon Bengal
Is this good?
![Page 108: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/108.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Animal
ProfessorCat CorgiParrot
Maine Coon Bengal
Is this good?
Does Cat serve a purpose?
![Page 109: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/109.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Animal
ProfessorCat CorgiParrot
Maine Coon Bengal
Is this good?
Does it achieve reuse?
![Page 110: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/110.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Animal
ProfessorCat CorgiParrot
Maine Coon Bengal
Is this good?
Does it achieve reuse?
What if I want a newAnimal at run time?
![Page 111: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/111.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better?
![Page 112: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/112.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
If someone on my team did this multiple times,
I would fire them.
Can we do better?
![Page 113: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/113.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
![Page 114: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/114.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movementhas-a
![Page 115: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/115.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movementhas-a
Movement selects from theways any Animal can move.
![Page 116: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/116.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-a
![Page 117: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/117.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-aCrawl
![Page 118: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/118.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-aCrawl Fly
![Page 119: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/119.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-aCrawl Fly Saunter
![Page 120: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/120.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-a
Tweet
Crawl Fly Saunter
![Page 121: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/121.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-a
MeowTweet
Crawl Fly Saunter
![Page 122: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/122.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-a
MeowTweet Ramble
Crawl Fly Saunter
![Page 123: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/123.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-a
MeowTweet Ramble Bark
Crawl Fly Saunter
![Page 124: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/124.jpg)
So let’s try it out...
● I need– Many different types of animals.– Each should be able to move() and speak().– An Animal& should be able to refer to any of them.
Can we do better? Recall: identify & isolate change
Animal
Movement
Vocalization
has-a
has-a
MeowTweet Ramble Bark
Crawl Fly Saunter
class Animal { Movement& m; void move() { m.move(); }};
![Page 125: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/125.jpg)
So let’s try it out...
● So let’s try it out...(!)
![Page 126: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/126.jpg)
Shallow, fine grained inheritance
● Avoids reimplementation of common behavior– e.g. Common aspects of Animal are just fields of Animal
![Page 127: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/127.jpg)
Shallow, fine grained inheritance
● Avoids reimplementation of common behavior– e.g. Common aspects of Animal are just fields of Animal
● Inheritance contracts for fine grained policies
![Page 128: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/128.jpg)
Shallow, fine grained inheritance
● Avoids reimplementation of common behavior– e.g. Common aspects of Animal are just fields of Animal
● Inheritance contracts for fine grained policies
● Enables dynamic selection & configuration of which policies are desired– e.g. A Cat may start out Stationary, then Run, then be Stationary
![Page 129: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/129.jpg)
Shallow, fine grained inheritance
● Avoids reimplementation of common behavior– e.g. Common aspects of Animal are just fields of Animal
● Inheritance contracts for fine grained policies
● Enables dynamic selection & configuration of which policies are desired– e.g. A Cat may start out Stationary, then Run, then be Stationary
Previously static requirements will often become dynamic.
![Page 130: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/130.jpg)
Shallow, fine grained inheritance
● Avoids reimplementation of common behavior– e.g. Common aspects of Animal are just fields of Animal
● Inheritance contracts for fine grained policies
● Enables dynamic selection & configuration of which policies are desired– e.g. A Cat may start out Stationary, then Run, then be Stationary
● Directly identifies & addresses risks of change in class design
![Page 131: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/131.jpg)
Shallow, fine grained inheritance
● Avoids reimplementation of common behavior– e.g. Common aspects of Animal are just fields of Animal
● Inheritance contracts for fine grained policies
● Enables dynamic selection & configuration of which policies are desired– e.g. A Cat may start out Stationary, then Run, then be Stationary
● Directly identifies & addresses risks of change in class design
● We will see shortly how this interacts with other forms of polymorphism
![Page 132: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/132.jpg)
Guidelines for inheritance
● Favor composition over inheritance
● Do not inherit to reuse. Inherit to be reused.
For some reason,textbooks & teachersoften get these wrong
![Page 133: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/133.jpg)
Guidelines for inheritance
● Favor composition over inheritance
● Do not inherit to reuse. Inherit to be reused.
![Page 134: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/134.jpg)
Guidelines for inheritance
● Favor composition over inheritance
● Do not inherit to reuse. Inherit to be reused.
JPanel
CustomJPanel
JComponent
![Page 135: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/135.jpg)
Guidelines for inheritance
● Favor composition over inheritance
● Do not inherit to reuse. Inherit to be reused.
JPanel
CustomJPanel
JComponent
Stack
Vector
![Page 136: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/136.jpg)
Guidelines for inheritance
● Favor composition over inheritance
● Do not inherit to reuse. Inherit to be reused.
Rectangle
Square TextFrameJPanel
CustomJPanel
JComponent
Stack
Vector
![Page 137: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/137.jpg)
Guidelines for inheritance
● Favor composition over inheritance
● Do not inherit to reuse. Inherit to be reused.
Rectangle
Square TextFrameJPanel
CustomJPanel
JComponent
Stack
Vector
![Page 138: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/138.jpg)
Guidelines for inheritance
● Use inheritance for semantic is-a relationships– Liskov substitutability
● If φ is true for the base, then φ is true the derived
BaseA foo(B b)
DerivedC foo(D d)
Derived is substitutable for Base
![Page 139: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/139.jpg)
Guidelines for inheritance
● Use inheritance for semantic is-a relationships– Liskov substitutability
● If φ is true for the base, then φ is true the derived● Arguments in the subtype may be more general
BaseA foo(B b)
DerivedC foo(D d)
B <: D
Arguments are contravariant
![Page 140: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/140.jpg)
Guidelines for inheritance
● Use inheritance for semantic is-a relationships– Liskov substitutability
● If φ is true for the base, then φ is true the derived● Arguments in the subtype may be more general● Return values in the subtype may be more constrained
BaseA foo(B b)
DerivedC foo(D d)
C <: A
Return types are covariant
![Page 141: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/141.jpg)
Guidelines for inheritance
● Use inheritance for semantic is-a relationships– Liskov substitutability
● If φ is true for the base, then φ is true the derived● Arguments in the subtype may be more general● Return values in the subtype may be more constrained
– Semantic substitutability is robust to drift
Rectangle
Square TextFrame
![Page 142: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/142.jpg)
Guidelines for inheritance
● Use inheritance for semantic is-a relationships– Liskov substitutability
● If φ is true for the base, then φ is true the derived● Arguments in the subtype may be more general● Return values in the subtype may be more constrained
– Semantic substitutability is robust to drift
Rectangle
Square TextFrame
![Page 143: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/143.jpg)
Guidelines for inheritance
● Inherit interfaces. Push implementation into the leaves.
Animal
Movement
Vocalization
has-a
has-a
MeowTweet Ramble Bark
Crawl Fly Saunter
class Animal { Movement& m; void move() { m.move(); }};
![Page 144: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/144.jpg)
Guidelines for inheritance
● Inherit interfaces. Push implementation into the leaves.– Hierarchies delocalize code, yielding a yo-yo effect– Ambiguous overrides break encapsulation
[Bloch, “Effective Java”]
class Parent { virtual void foo() { bar(); } virtual void bar() {}};
![Page 145: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/145.jpg)
Guidelines for inheritance
● Inherit interfaces. Push implementation into the leaves.– Hierarchies delocalize code, yielding a yo-yo effect– Ambiguous overrides break encapsulation
class Parent { virtual void foo() { bar(); } virtual void bar() {}};
class Child : public Parent {public: virtual void bar() { foo(); }};
[Bloch, “Effective Java”]
![Page 146: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/146.jpg)
Guidelines for inheritance
● Inherit interfaces. Push implementation into the leaves.– Hierarchies delocalize code, yielding a yo-yo effect– Ambiguous overrides break encapsulation
class Parent { virtual void foo() { bar(); } virtual void bar() {}};
class Child : public Parent {public: virtual void bar() { foo(); }};
[Bloch, “Effective Java”]
class Parent {public: void foo() { barImpl(); } void bar() { barImpl(); }private: virtual void barImpl() = 0;};
![Page 147: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/147.jpg)
Guidelines for inheritance
● Inherit interfaces. Push implementation into the leaves.– Hierarchies delocalize code, yielding a yo-yo effect– Ambiguous overrides break encapsulation
class Parent { virtual void foo() { bar(); } virtual void bar() {}};
class Child : public Parent {public: virtual void bar() { foo(); }};
[Bloch, “Effective Java”]
class Parent {public: void foo() { barImpl(); } void bar() { barImpl(); }private: virtual void barImpl() = 0;};
Non Virtual Interfaces (NVI) helpclarify & are common in C++.
Other patterns help even more...
![Page 148: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/148.jpg)
Guidelines for inheritance
● Design for inheritance.Choose customization points for runtime polymorphism.Prevent inheritance elsewhere. class Student final : public Person {
public: enum class Degree { UNDERGRAD, MASTERS, PHD, };
Student(Degree degree);
void studyOneHour();
void sleep() override;
private: int hoursStudied; Degree degree;};
![Page 149: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/149.jpg)
Summary
● Object oriented programming is a useful tool in your toolbox
![Page 150: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/150.jpg)
Summary
● Object oriented programming is a useful tool in your toolbox
● It can be challenging to use well and should be deliberate
![Page 151: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/151.jpg)
Summary
● Object oriented programming is a useful tool in your toolbox
● It can be challenging to use well and should be deliberate
● Inheritance, specifically, is powerful but often abused
![Page 152: CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner wsumner@sfu.ca Object Oriented](https://reader034.vdocument.in/reader034/viewer/2022052101/603ac82b76424903dc7d1506/html5/thumbnails/152.jpg)
Summary
● Object oriented programming is a useful tool in your toolbox
● It can be challenging to use well and should be deliberate
● Inheritance, specifically, is powerful but often abused
● Object orientation does not solve problems in modelingthat requires more effort, as we will see.