Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented...
Transcript of Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented...
![Page 1: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/1.jpg)
Principles of Programming LanguagesParadigms: Object Oriented Programming.
Andrei Arusoaie1
1Department of Computer Science
December 19, 2019
![Page 2: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/2.jpg)
Outline
Paradigms
Object-Oriented Programming
Conclusion
![Page 3: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/3.jpg)
Outline
Paradigms
Object-Oriented Programming
Conclusion
![Page 4: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/4.jpg)
Outline
Paradigms
Object-Oriented Programming
Conclusion
![Page 5: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/5.jpg)
Paradigms
I Imperative Programming:
I Object Oriented ProgrammingI Declarative programming
I Functional ProgrammingI Logic Programming
![Page 6: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/6.jpg)
Paradigms
I Imperative Programming:I Object Oriented Programming
I Declarative programmingI Functional ProgrammingI Logic Programming
![Page 7: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/7.jpg)
Paradigms
I Imperative Programming:I Object Oriented ProgrammingI Declarative programming
I Functional Programming
I Logic Programming
![Page 8: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/8.jpg)
Paradigms
I Imperative Programming:I Object Oriented ProgrammingI Declarative programming
I Functional ProgrammingI Logic Programming
![Page 9: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/9.jpg)
Object-Oriented Programming: Motivation-1
I Complex software
I NATO conference, 1968: software crisis1. User dissatisfaction with the final product2. cost overruns3. buggy software4. brittle software
![Page 10: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/10.jpg)
Object-Oriented Programming: Motivation-1
I Complex softwareI NATO conference, 1968: software crisis
1. User dissatisfaction with the final product
2. cost overruns3. buggy software4. brittle software
![Page 11: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/11.jpg)
Object-Oriented Programming: Motivation-1
I Complex softwareI NATO conference, 1968: software crisis
1. User dissatisfaction with the final product2. cost overruns
3. buggy software4. brittle software
![Page 12: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/12.jpg)
Object-Oriented Programming: Motivation-1
I Complex softwareI NATO conference, 1968: software crisis
1. User dissatisfaction with the final product2. cost overruns3. buggy software
4. brittle software
![Page 13: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/13.jpg)
Object-Oriented Programming: Motivation-1
I Complex softwareI NATO conference, 1968: software crisis
1. User dissatisfaction with the final product2. cost overruns3. buggy software4. brittle software
![Page 14: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/14.jpg)
Motivation-2
Main issues in large software projects:I Names: functions, variables, . . .
I Constraints: time and space complexityI Memory management (garbage collection and address
spaces)I ConcurrencyI Event-driven interfaces
How to fix all these?
![Page 15: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/15.jpg)
Motivation-2
Main issues in large software projects:I Names: functions, variables, . . .I Constraints: time and space complexity
I Memory management (garbage collection and addressspaces)
I ConcurrencyI Event-driven interfaces
How to fix all these?
![Page 16: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/16.jpg)
Motivation-2
Main issues in large software projects:I Names: functions, variables, . . .I Constraints: time and space complexityI Memory management (garbage collection and address
spaces)
I ConcurrencyI Event-driven interfaces
How to fix all these?
![Page 17: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/17.jpg)
Motivation-2
Main issues in large software projects:I Names: functions, variables, . . .I Constraints: time and space complexityI Memory management (garbage collection and address
spaces)I Concurrency
I Event-driven interfaces
How to fix all these?
![Page 18: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/18.jpg)
Motivation-2
Main issues in large software projects:I Names: functions, variables, . . .I Constraints: time and space complexityI Memory management (garbage collection and address
spaces)I ConcurrencyI Event-driven interfaces
How to fix all these?
![Page 19: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/19.jpg)
Motivation-2
Main issues in large software projects:I Names: functions, variables, . . .I Constraints: time and space complexityI Memory management (garbage collection and address
spaces)I ConcurrencyI Event-driven interfaces
How to fix all these?
![Page 20: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/20.jpg)
How to fix all these?
I Extreme discipline
(hard to get when working in teams)I Or a new paradigm that facilitates:
1. solving very complex problems2. working in teams3. code modularity4. easy development and code maintenance
![Page 21: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/21.jpg)
How to fix all these?
I Extreme discipline (hard to get when working in teams)
I Or a new paradigm that facilitates:1. solving very complex problems2. working in teams3. code modularity4. easy development and code maintenance
![Page 22: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/22.jpg)
How to fix all these?
I Extreme discipline (hard to get when working in teams)I Or a new paradigm that facilitates:
1. solving very complex problems2. working in teams3. code modularity4. easy development and code maintenance
![Page 23: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/23.jpg)
How to fix all these?
I Extreme discipline (hard to get when working in teams)I Or a new paradigm that facilitates:
1. solving very complex problems
2. working in teams3. code modularity4. easy development and code maintenance
![Page 24: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/24.jpg)
How to fix all these?
I Extreme discipline (hard to get when working in teams)I Or a new paradigm that facilitates:
1. solving very complex problems2. working in teams
3. code modularity4. easy development and code maintenance
![Page 25: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/25.jpg)
How to fix all these?
I Extreme discipline (hard to get when working in teams)I Or a new paradigm that facilitates:
1. solving very complex problems2. working in teams3. code modularity
4. easy development and code maintenance
![Page 26: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/26.jpg)
How to fix all these?
I Extreme discipline (hard to get when working in teams)I Or a new paradigm that facilitates:
1. solving very complex problems2. working in teams3. code modularity4. easy development and code maintenance
![Page 27: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/27.jpg)
Object-Oriented Programming
A bunch of concepts:
I Classes, objects, members (attributes + methods), accessmodifiers
I Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 28: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/28.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiers
I Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 29: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/29.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructors
I Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 30: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/30.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowing
I Encapsulation, information hiddingI AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 31: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/31.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hidding
I AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 32: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/32.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI Abstraction
I InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 33: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/33.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI Inheritance
I PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 34: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/34.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI InheritanceI Polymorphism
I Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 35: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/35.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)
I Interfaces, abstract classes
![Page 36: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/36.jpg)
Object-Oriented Programming
A bunch of concepts:I Classes, objects, members (attributes + methods), access
modifiersI Constructors, destructorsI Overloading, overriding, shadowingI Encapsulation, information hiddingI AbstractionI InheritanceI PolymorphismI Dynamic method selection (or dynamic dispatch)I Interfaces, abstract classes
![Page 37: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/37.jpg)
Classes vs. Objects
I Class =
a specification used to construct objectsI Classes establish the name of the class, the data method
members with types and signature, visibility andimplementation.
I Object = an instance of a class; it has a state where itsdata members (or attributes or properties) – described inthe corresponding class – have values
![Page 38: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/38.jpg)
Classes vs. Objects
I Class = a specification used to construct objects
I Classes establish the name of the class, the data methodmembers with types and signature, visibility andimplementation.
I Object = an instance of a class; it has a state where itsdata members (or attributes or properties) – described inthe corresponding class – have values
![Page 39: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/39.jpg)
Classes vs. Objects
I Class = a specification used to construct objectsI Classes establish the name of the class, the data method
members with types and signature, visibility andimplementation.
I Object = an instance of a class; it has a state where itsdata members (or attributes or properties) – described inthe corresponding class – have values
![Page 40: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/40.jpg)
Classes vs. Objects
I Class = a specification used to construct objectsI Classes establish the name of the class, the data method
members with types and signature, visibility andimplementation.
I Object =
an instance of a class; it has a state where itsdata members (or attributes or properties) – described inthe corresponding class – have values
![Page 41: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/41.jpg)
Classes vs. Objects
I Class = a specification used to construct objectsI Classes establish the name of the class, the data method
members with types and signature, visibility andimplementation.
I Object = an instance of a class; it has a state where itsdata members (or attributes or properties) – described inthe corresponding class – have values
![Page 42: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/42.jpg)
Constructors and Destructors
I Constructor
: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the objectI Garbage Collector: automatic memory management
(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 43: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/43.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the objectI Garbage Collector: automatic memory management
(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 44: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/44.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the objectI Garbage Collector: automatic memory management
(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 45: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/45.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the objectI Garbage Collector: automatic memory management
(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 46: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/46.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor
: frees the memory occupied by the objectI Garbage Collector: automatic memory management
(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 47: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/47.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the object
I Garbage Collector: automatic memory management(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 48: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/48.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the objectI Garbage Collector
: automatic memory management(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 49: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/49.jpg)
Constructors and Destructors
I Constructor: a special “method” which has the same nameas the class and no return type
I When an object is created the constructor is called:allocate the necessary memory, initialise members (do notforget inheritance)
I Typically (in many OO programming languages) we usethe new keyword to call the constructor
I Destructor: frees the memory occupied by the objectI Garbage Collector: automatic memory management
(efficient memory allocation, automatically frees thememory, enables memory reuse)
![Page 50: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/50.jpg)
Encapsulation
I Encapsulation
: organise the data and operations (over thatdata) in a single unit
I Advantage: manipulate the objects in an uniform andconsistent way using methods
I Information hidding: separate the interface (i.e., the publicinformation in a class) from the implementation
I Access modifiers: used to restrict/grant access to object’sdata
![Page 51: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/51.jpg)
Encapsulation
I Encapsulation: organise the data and operations (over thatdata) in a single unit
I Advantage: manipulate the objects in an uniform andconsistent way using methods
I Information hidding: separate the interface (i.e., the publicinformation in a class) from the implementation
I Access modifiers: used to restrict/grant access to object’sdata
![Page 52: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/52.jpg)
Encapsulation
I Encapsulation: organise the data and operations (over thatdata) in a single unit
I Advantage: manipulate the objects in an uniform andconsistent way using methods
I Information hidding: separate the interface (i.e., the publicinformation in a class) from the implementation
I Access modifiers: used to restrict/grant access to object’sdata
![Page 53: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/53.jpg)
Encapsulation
I Encapsulation: organise the data and operations (over thatdata) in a single unit
I Advantage: manipulate the objects in an uniform andconsistent way using methods
I Information hidding: separate the interface (i.e., the publicinformation in a class) from the implementation
I Access modifiers: used to restrict/grant access to object’sdata
![Page 54: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/54.jpg)
Encapsulation
I Encapsulation: organise the data and operations (over thatdata) in a single unit
I Advantage: manipulate the objects in an uniform andconsistent way using methods
I Information hidding: separate the interface (i.e., the publicinformation in a class) from the implementation
I Access modifiers: used to restrict/grant access to object’sdata
![Page 55: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/55.jpg)
Abstraction
I It comes in the same “package” with Encapsulation
I Example: driving a car doesn’t require knowing theinternals of the combustion engine
I Abstraction: preserve the information that is important in agiven context
I The human mind can operate easier with abstractionsI Experiment:197328643791468251973286437914682 (30
sec)1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 56: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/56.jpg)
Abstraction
I It comes in the same “package” with EncapsulationI Example: driving a car doesn’t require knowing the
internals of the combustion engine
I Abstraction: preserve the information that is important in agiven context
I The human mind can operate easier with abstractionsI Experiment:197328643791468251973286437914682 (30
sec)1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 57: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/57.jpg)
Abstraction
I It comes in the same “package” with EncapsulationI Example: driving a car doesn’t require knowing the
internals of the combustion engineI Abstraction: preserve the information that is important in a
given context
I The human mind can operate easier with abstractionsI Experiment:197328643791468251973286437914682 (30
sec)1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 58: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/58.jpg)
Abstraction
I It comes in the same “package” with EncapsulationI Example: driving a car doesn’t require knowing the
internals of the combustion engineI Abstraction: preserve the information that is important in a
given contextI The human mind can operate easier with abstractions
I Experiment:197328643791468251973286437914682 (30sec)
1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 59: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/59.jpg)
Abstraction
I It comes in the same “package” with EncapsulationI Example: driving a car doesn’t require knowing the
internals of the combustion engineI Abstraction: preserve the information that is important in a
given contextI The human mind can operate easier with abstractionsI Experiment:
197328643791468251973286437914682 (30sec)
1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 60: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/60.jpg)
Abstraction
I It comes in the same “package” with EncapsulationI Example: driving a car doesn’t require knowing the
internals of the combustion engineI Abstraction: preserve the information that is important in a
given contextI The human mind can operate easier with abstractionsI Experiment:197328643791468251973286437914682 (30
sec)
1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 61: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/61.jpg)
Abstraction
I It comes in the same “package” with EncapsulationI Example: driving a car doesn’t require knowing the
internals of the combustion engineI Abstraction: preserve the information that is important in a
given contextI The human mind can operate easier with abstractionsI Experiment:197328643791468251973286437914682 (30
sec)1. How many can you remember?2. How many can you remember with unlimited amount of
time?
![Page 62: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/62.jpg)
Inheritance
I Inheritance
: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...I ... with some exceptions:
I Field hidding: Child can redefine data members (i.e, usethe same name) from Parent
I Overriding: Child can override methods defined inParent
I Single vs. multiple inheritance
![Page 63: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/63.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...I ... with some exceptions:
I Field hidding: Child can redefine data members (i.e, usethe same name) from Parent
I Overriding: Child can override methods defined inParent
I Single vs. multiple inheritance
![Page 64: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/64.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”
I If class Child inherits from class Parent then Childuses the same implementation as Parent...
I ... with some exceptions:I Field hidding: Child can redefine data members (i.e, use
the same name) from ParentI Overriding: Child can override methods defined in
Parent
I Single vs. multiple inheritance
![Page 65: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/65.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...
I ... with some exceptions:I Field hidding: Child can redefine data members (i.e, use
the same name) from ParentI Overriding: Child can override methods defined in
Parent
I Single vs. multiple inheritance
![Page 66: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/66.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...I ... with some exceptions:
I Field hidding: Child can redefine data members (i.e, usethe same name) from Parent
I Overriding: Child can override methods defined inParent
I Single vs. multiple inheritance
![Page 67: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/67.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...I ... with some exceptions:
I Field hidding: Child can redefine data members (i.e, usethe same name) from Parent
I Overriding: Child can override methods defined inParent
I Single vs. multiple inheritance
![Page 68: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/68.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...I ... with some exceptions:
I Field hidding: Child can redefine data members (i.e, usethe same name) from Parent
I Overriding: Child can override methods defined inParent
I Single vs. multiple inheritance
![Page 69: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/69.jpg)
Inheritance
I Inheritance: reuse the code/features from a previouslydefined class
I Principle: DRY – “Do not repeat yourself”I If class Child inherits from class Parent then Child
uses the same implementation as Parent...I ... with some exceptions:
I Field hidding: Child can redefine data members (i.e, usethe same name) from Parent
I Overriding: Child can override methods defined inParent
I Single vs. multiple inheritance
![Page 70: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/70.jpg)
Polymorphism
I It comes in many flavours:1. Overloading: multiple implementations of the same method
with different parameters2. Parametric – often used with generics: methods work with
parametric classes (e.g., sort for List<T>)3. Subtype: single interface to multiple representations of the
same type (the most common type of polymorphism)I Subtype polymorphism: uses inheritance
![Page 71: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/71.jpg)
Polymorphism
I It comes in many flavours:
1. Overloading: multiple implementations of the same methodwith different parameters
2. Parametric – often used with generics: methods work withparametric classes (e.g., sort for List<T>)
3. Subtype: single interface to multiple representations of thesame type (the most common type of polymorphism)
I Subtype polymorphism: uses inheritance
![Page 72: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/72.jpg)
Polymorphism
I It comes in many flavours:1. Overloading: multiple implementations of the same method
with different parameters
2. Parametric – often used with generics: methods work withparametric classes (e.g., sort for List<T>)
3. Subtype: single interface to multiple representations of thesame type (the most common type of polymorphism)
I Subtype polymorphism: uses inheritance
![Page 73: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/73.jpg)
Polymorphism
I It comes in many flavours:1. Overloading: multiple implementations of the same method
with different parameters2. Parametric – often used with generics: methods work with
parametric classes (e.g., sort for List<T>)
3. Subtype: single interface to multiple representations of thesame type (the most common type of polymorphism)
I Subtype polymorphism: uses inheritance
![Page 74: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/74.jpg)
Polymorphism
I It comes in many flavours:1. Overloading: multiple implementations of the same method
with different parameters2. Parametric – often used with generics: methods work with
parametric classes (e.g., sort for List<T>)3. Subtype: single interface to multiple representations of the
same type (the most common type of polymorphism)
I Subtype polymorphism: uses inheritance
![Page 75: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/75.jpg)
Polymorphism
I It comes in many flavours:1. Overloading: multiple implementations of the same method
with different parameters2. Parametric – often used with generics: methods work with
parametric classes (e.g., sort for List<T>)3. Subtype: single interface to multiple representations of the
same type (the most common type of polymorphism)I Subtype polymorphism: uses inheritance
![Page 76: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/76.jpg)
Subtype polymorphism
I Suppose that Child inherits Parent, and consider afunction SomeType foo(Parent p) {...}
I In this case foo can be applied to an instance of anysubclass of Parent:
Child child = new Child();SomeType t = foo(child);
I Consider Parent bar(Parent p) { return p; }:Child child = new Child();Child p = bar(child); // rejected by static type checkerChild p = (Child) bar(child); // dynamic check
![Page 77: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/77.jpg)
Subtype polymorphism
I Suppose that Child inherits Parent, and consider afunction SomeType foo(Parent p) {...}
I In this case foo can be applied to an instance of anysubclass of Parent:
Child child = new Child();SomeType t = foo(child);
I Consider Parent bar(Parent p) { return p; }:Child child = new Child();Child p = bar(child); // rejected by static type checkerChild p = (Child) bar(child); // dynamic check
![Page 78: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/78.jpg)
Subtype polymorphism
I Suppose that Child inherits Parent, and consider afunction SomeType foo(Parent p) {...}
I In this case foo can be applied to an instance of anysubclass of Parent:
Child child = new Child();SomeType t = foo(child);
I Consider Parent bar(Parent p) { return p; }:Child child = new Child();Child p = bar(child);
// rejected by static type checkerChild p = (Child) bar(child); // dynamic check
![Page 79: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/79.jpg)
Subtype polymorphism
I Suppose that Child inherits Parent, and consider afunction SomeType foo(Parent p) {...}
I In this case foo can be applied to an instance of anysubclass of Parent:
Child child = new Child();SomeType t = foo(child);
I Consider Parent bar(Parent p) { return p; }:Child child = new Child();Child p = bar(child); // rejected by static type checker
Child p = (Child) bar(child); // dynamic check
![Page 80: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/80.jpg)
Subtype polymorphism
I Suppose that Child inherits Parent, and consider afunction SomeType foo(Parent p) {...}
I In this case foo can be applied to an instance of anysubclass of Parent:
Child child = new Child();SomeType t = foo(child);
I Consider Parent bar(Parent p) { return p; }:Child child = new Child();Child p = bar(child); // rejected by static type checkerChild p = (Child) bar(child);
// dynamic check
![Page 81: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/81.jpg)
Subtype polymorphism
I Suppose that Child inherits Parent, and consider afunction SomeType foo(Parent p) {...}
I In this case foo can be applied to an instance of anysubclass of Parent:
Child child = new Child();SomeType t = foo(child);
I Consider Parent bar(Parent p) { return p; }:Child child = new Child();Child p = bar(child); // rejected by static type checkerChild p = (Child) bar(child); // dynamic check
![Page 82: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/82.jpg)
Dynamic dispatch
I Dynamic dispatch
: select the correct implementation of apolymorphic method at runtimeclass Parent {... void f() {...} ...}class Child : Parent {... void f() {...} ...}...Child child = new Child();child.f() // f((Parent) child).f(); // f
![Page 83: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/83.jpg)
Dynamic dispatch
I Dynamic dispatch: select the correct implementation of apolymorphic method at runtime
class Parent {... void f() {...} ...}class Child : Parent {... void f() {...} ...}...Child child = new Child();child.f() // f((Parent) child).f(); // f
![Page 84: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/84.jpg)
Dynamic dispatch
I Dynamic dispatch: select the correct implementation of apolymorphic method at runtimeclass Parent {... void f() {...} ...}class Child : Parent {... void f() {...} ...}...Child child = new Child();child.f()
// f((Parent) child).f(); // f
![Page 85: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/85.jpg)
Dynamic dispatch
I Dynamic dispatch: select the correct implementation of apolymorphic method at runtimeclass Parent {... void f() {...} ...}class Child : Parent {... void f() {...} ...}...Child child = new Child();child.f() // f
((Parent) child).f(); // f
![Page 86: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/86.jpg)
Dynamic dispatch
I Dynamic dispatch: select the correct implementation of apolymorphic method at runtimeclass Parent {... void f() {...} ...}class Child : Parent {... void f() {...} ...}...Child child = new Child();child.f() // f((Parent) child).f();
// f
![Page 87: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/87.jpg)
Dynamic dispatch
I Dynamic dispatch: select the correct implementation of apolymorphic method at runtimeclass Parent {... void f() {...} ...}class Child : Parent {... void f() {...} ...}...Child child = new Child();child.f() // f((Parent) child).f(); // f
![Page 88: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/88.jpg)
Late binding
I Late binding
:class Parent {
int a = 0;void f() { g(); }void g() { a = 1; }
}class Child : Parent {
int b = 0;void g() { b = 2; }
}...Child child = new Child();child.f() // b = 2;a = 0;
![Page 89: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/89.jpg)
Late binding
I Late binding:class Parent {
int a = 0;void f() { g(); }void g() { a = 1; }
}class Child : Parent {
int b = 0;void g() { b = 2; }
}...Child child = new Child();child.f()
// b = 2;a = 0;
![Page 90: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/90.jpg)
Late binding
I Late binding:class Parent {
int a = 0;void f() { g(); }void g() { a = 1; }
}class Child : Parent {
int b = 0;void g() { b = 2; }
}...Child child = new Child();child.f() // b =
2;a = 0;
![Page 91: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/91.jpg)
Late binding
I Late binding:class Parent {
int a = 0;void f() { g(); }void g() { a = 1; }
}class Child : Parent {
int b = 0;void g() { b = 2; }
}...Child child = new Child();child.f() // b = 2;
a = 0;
![Page 92: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/92.jpg)
Late binding
I Late binding:class Parent {
int a = 0;void f() { g(); }void g() { a = 1; }
}class Child : Parent {
int b = 0;void g() { b = 2; }
}...Child child = new Child();child.f() // b = 2;a =
0;
![Page 93: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/93.jpg)
Late binding
I Late binding:class Parent {
int a = 0;void f() { g(); }void g() { a = 1; }
}class Child : Parent {
int b = 0;void g() { b = 2; }
}...Child child = new Child();child.f() // b = 2;a = 0;
![Page 94: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/94.jpg)
Interfaces vs. Abstract classes
I Abstract class
: class without implementation for (at leastone) methods
I Interface: has methods without implementationI Traits:
1. Both can be inherited2. Abstract classes can contain implementation for functions3. Methods in interfaces must be implemented by the
implementing class
![Page 95: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/95.jpg)
Interfaces vs. Abstract classes
I Abstract class: class without implementation for (at leastone) methods
I Interface: has methods without implementationI Traits:
1. Both can be inherited2. Abstract classes can contain implementation for functions3. Methods in interfaces must be implemented by the
implementing class
![Page 96: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/96.jpg)
Interfaces vs. Abstract classes
I Abstract class: class without implementation for (at leastone) methods
I Interface
: has methods without implementationI Traits:
1. Both can be inherited2. Abstract classes can contain implementation for functions3. Methods in interfaces must be implemented by the
implementing class
![Page 97: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/97.jpg)
Interfaces vs. Abstract classes
I Abstract class: class without implementation for (at leastone) methods
I Interface: has methods without implementationI Traits:
1. Both can be inherited
2. Abstract classes can contain implementation for functions3. Methods in interfaces must be implemented by the
implementing class
![Page 98: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/98.jpg)
Interfaces vs. Abstract classes
I Abstract class: class without implementation for (at leastone) methods
I Interface: has methods without implementationI Traits:
1. Both can be inherited2. Abstract classes can contain implementation for functions
3. Methods in interfaces must be implemented by theimplementing class
![Page 99: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/99.jpg)
Interfaces vs. Abstract classes
I Abstract class: class without implementation for (at leastone) methods
I Interface: has methods without implementationI Traits:
1. Both can be inherited2. Abstract classes can contain implementation for functions3. Methods in interfaces must be implemented by the
implementing class
![Page 100: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/100.jpg)
Related concepts
I GenericsI PackagesI ModulesI Namespaces
![Page 101: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/101.jpg)
Facts
I OOP scales; it is widely used in the industryI Not all languages that are advertised as OOP languages
support all the features above (e.g., C++ does not haveGC)
I Sometimes, misunderstanding of the OOP concepts maycomplicate your code
![Page 102: Principles of Programming Languages Paradigms: …arusoaie.andrei/lectures/PLP/...Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis](https://reader034.fdocuments.in/reader034/viewer/2022050205/5f58242b0858ab78636bfecd/html5/thumbnails/102.jpg)
Resources
I Chapter 10 in Programming Languages: Principles andParadigms by Maurizio Gabbrielli and Simone Martini.URL: http://websrv.dthu.edu.vn/attachments/newsevents/content2415/Programming_Languages_-_Principles_and_Paradigms_thereds1106.pdf