152
CMPT 373 Soſtware Development Methods Nick Sumner [email protected] Object Oriented Programming & Inheritance

CMPT 373 Software Development Methods Object Oriented ...wsumner/teaching/373/07-oops-inheritance.… · CMPT 373 Software Development Methods Nick Sumner [email protected] Object Oriented

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.