Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I...

102
Principles of Programming Languages Paradigms: OOP. Andrei Arusoaie 1 1 Department of Computer Science December 17, 2018

Transcript of Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I...

Page 1: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Principles of Programming LanguagesParadigms: OOP.

Andrei Arusoaie1

1Department of Computer Science

December 17, 2018

Page 2: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Outline

Paradigms

Object-Oriented Programming

Conclusion

Page 3: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Outline

Paradigms

Object-Oriented Programming

Conclusion

Page 4: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Outline

Paradigms

Object-Oriented Programming

Conclusion

Page 5: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Paradigms

I Imperative Programming:

I Object Oriented ProgrammingI Functional ProgrammingI Logic Programming

Page 6: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Paradigms

I Imperative Programming:I Object Oriented Programming

I Functional ProgrammingI Logic Programming

Page 7: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Paradigms

I Imperative Programming:I Object Oriented ProgrammingI Functional Programming

I Logic Programming

Page 8: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Paradigms

I Imperative Programming:I Object Oriented ProgrammingI Functional ProgrammingI Logic Programming

Page 9: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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 have some implementations3. Methods in interfaces must be implemented by the

implementing class

Page 95: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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 have some implementations3. Methods in interfaces must be implemented by the

implementing class

Page 96: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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 have some implementations3. Methods in interfaces must be implemented by the

implementing class

Page 97: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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 have some implementations3. Methods in interfaces must be implemented by the

implementing class

Page 98: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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 have some implementations

3. Methods in interfaces must be implemented by theimplementing class

Page 99: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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 have some implementations3. Methods in interfaces must be implemented by the

implementing class

Page 100: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

Related concepts

I GenericsI PackagesI ModulesI Namespaces

Page 101: Principles of Programming Languages Paradigms: OOP. · Object-Oriented Programming: Motivation-1 I Complex software I NATO conference, 1968: software crisis 1.User dissatisfaction

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