Top Banner
05/16/22 Assoc. Prof. Stoyan Bonev 1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus
81

9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Dec 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 1

COS240 O-O Languages AUBG, COS dept

Lecture 01Title:

OOP Basic Intro

Reference: COS240 Syllabus

Page 2: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 2

Lecture Contents:

• OOP Aspects• OOP Concepts• OOP Characteristics• OOP Terminology

Page 3: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 3

Software crisis

• Edsger Dijkstra, a Computer Science pioneer, explained the major cause for the software crisis:

• “... machines have become several orders of magnitude more powerful. To put it quite bluntly: as long as there were no machines, programming was no problem at all; when we had a few weak computers, programming became a mild problem, and now we have gigantic computers, programming has become an equally gigantic problem.”

Page 4: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 4

Software crisis

• In order to resolve software crisis, a variety of programming styles or paradigms or approaches or methodologies appeared.

Page 5: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

5

Programming Paradigms

Other programming paradigms besides object-oriented are

- the imperative (or procedural) programming paradigm (e.g. languages such as Pascal or C)

- the logic programming paradigm (e.g. Prolog)

- the functional programming paradigm (e.g. LISP, ML or Haskell)

See course COS220 – Concepts of Programming Languages

Object-oriented and imperative programming paradigms are by far the most popular paradigms.

Page 6: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

6

Imperative Programming Paradigm

Imperative language

=

Behaviour (i.e. operations: code in terms of procedures and/or functions)

+

Data structures (which are separate from behaviour)

Viewed as separate entities or Viewed as separate entities or Algorithms + Data Structures = ProgramsAlgorithms + Data Structures = Programs

Page 7: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

7

Object-Oriented language

Classes and Objects

+

Inheritance

+

Communication via Messages

=

Object-Oriented Paradigm

Commonly performed with method (procedure/function)call, with call parameters

Data and behaviour areData and behaviour areencapsulatedencapsulated

in the classin the class

Page 8: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 8

What is Programming?

• Or different Aspects of Programming.

Page 9: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

9

Aspects of Programming

Programming has many “aspects” or characteristics.

E.g.

Programming is controlling- computer does exactly what you tell it to.

Programming is problem solving- always trying to make computer do something

useful, e.g. finding an optimal travel route.

Programming is creative- must find a good solution out of many possibilities.

Page 10: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

10

Two more aspects which are important for this course are

Programming is modeling- describe the relevant properties and behaviours of a

problem as a system of components, i.e. objects.

Programming is abstraction – identify important features of a problem without

getting lost in detail– ignore irrelevant features of problem.

Page 11: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

11

These last two aspects are important for this course:

- When we write a program, we are actually creating a model of some real-world problem.

- In order to reduce the complexity of the problem, we use the concept of abstraction to simplify the model.

Page 12: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

12

Abstraction

Abstraction is used to decrease the level of complexity of a problem.

In order to develop a class for one or more objects, it is important to abstract (i.e. consider) only the important attributes (data structures) and operations (methods) from the problem - only the ones that matter for the software solution.

Extraneous (i.e. irrelevant) detail should be omitted from consideration.

Note: for two different implementations of the same problem, the attributes and methods that are considered to be relevant may differ! See next slide!

Page 13: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

13

Abstraction

Page 14: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

14

- A cat object for a house lady

would have totally different characteristics (abstractions) compared to

- A cat object for a veterinary surgeon.

Both are cat objects (but different!).

It all depends on the problem to be solved – a cat object for a house lady or one for a vet.

Page 15: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

15

Abstraction

Page 16: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

16

Main concept

Object-oriented programming is creating a collection of independent, but cooperating, active "agents" ( “objects” or “instances”) that, working together, accomplish some desired task.

A popular approach to object-oriented program design is to concentrate on the responsibilities of objects.

- a responsibility is some defined behaviour of an object.

An object’s responsibilities are high-level statements about both the knowledge it maintains and theoperations it supports.

Page 17: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

17

The Object-Oriented Mantra

• Objects do the work. The work is done by “instance methods” of the appropriate class.

• Well designed objects have clear responsibilities and clear interfaces.

• Objects are instances of a class. Their behaviour is implemented in instance methods.

Page 18: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

18

Objects have behaviours

• In imperative/procedural programming, programs have– data, which is completely passive– functions/procedures, which can manipulate any data

• In OO programming, an object contains both data and methods that manipulate that data.– An object is active, not passive; it does things.– An object manages its own data.

• It can protect that data (by marking it private).• It can expose that data to other objects (by not

marking it, or by marking it public).• Data should not be exposed without good reason.

Page 19: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

19

Objects have state

• An object contains data (members)

– The data represent the state of the object.

– Data can also describe the relationship of the object to other objects.

Page 20: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

20

Object

Page 21: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

21

Page 22: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

22

Classes and Objects

• All objects are instances or members of a class.

• A class is a description of one or more similar objects– It is a template or blueprint for the object.

• Instance is an object described by a particular class.

• All objects of a given class use the same method (operation) in response to similar “messages”.

• Class provides a template for an object - it contains the definition of the data items comprising the object, and the implementation of the operations (behaviour) allowed on the object.

A rough approximation is to say that a class is an abstract data type.

Page 23: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

23

Example

• A car encapsulates lots of information

• But, you do not need to know how a car works in order to use it– steering wheel and gear shift are the interface.– engine, transmission, gears, wheels, . . . are the

(hidden) implementation.

Page 24: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

24

Likewise, you do not need to know how an object works in order to send messages to it.

But, you do need to know what messages it understands (i.e. what its capabilities are)

- the class of the instance determines what messages can be sent to it.

Page 25: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

25

The eight fundamental concepts (“quarks”) of the object-oriented paradigm:

- Abstraction- Encapsulation- Information Hiding- Class- Object- Method- Message Passing- Polymorphism

See: Armstrong, “The Quarks of Object-Oriented Development”, in Communications of the ACM, 49:2, p.123-128, Feb 2006

Page 26: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

26

Some people think the list should be extended to include such “quarks” as

- inheritance

- subtyping

- overriding

- reuse

Page 27: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

27

Revision of Main Points1. Abstraction is the process of generalization of a real-world problem by reducing its information content, typically in order to retain only information which is relevant for a software solution, i.e. program.

It is a process of simplification – concentrating only on relevant problem details, while ignoring the irrelevant ones

- this greatly simplifies the design of the solution.

We use abstraction to “model” real-world objects.

E.g. for a person object, relevant details that we may wish to “model” may be name, address, age, sex, …and irrelevant details that we can “forget about” for our model may be eye and hair colour, shoe size, …

Page 28: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

28

Forms of Abstraction

• Specialization – class hierarchies• Division in parts – aggregation • Multiple Views – polymorphism

• “Is-a” is a specialization• “Has-a” is an aggregation• ?? is a polymorphism

Page 29: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

29

2. In object-oriented programming, encapsulation is the concept of keeping together data and the methods (aka functions) which act on them – the grouping of data and the methods that manipulate them together in a single software entity: the class.

Page 30: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

30

3. Information hiding is the principle that a class should not expose its data to “users” (aka clients) of the class, but rather should offer an interface allowing users to access the class data indirectly. This allows the modification of how the data in the class is represented, and how the method algorithms are implemented, without changing the interface.Doing so, decouples the implementation of the class from its clients, which helps with making the code reusable.

An interface specifies what “services” or operations (i.e. methods) the class provides for its clients. It is the public view of the class.

Page 31: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

31

Objects separate interface from implementation

- object implementation is “black box,” hiding internal workings and parts.

- object interface protects implementation from misuse.

Interface: public view- allows instances to cooperate with one another

without knowing too many details.- like a contract: consists of a list of capabilities

and documentation for how they can be used.

Implementation: private view- properties that help capabilities complete their

tasks.

Page 32: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

32

An abstract data type (ADT) is a specification of a set of data, and the set of operations (i.e. procedures/functions) that can be performed on that data.

- It is a programmer-defined data type.

4. Classes are the mechanism for expressing abstract data types in object-oriented languages.

A class is a template or blueprint for an object. It specifies what data and operations exist for the class.

5. An object is an instance of a class.

An object is an instance of the data type, class.

Each object has state (aka attributes data), behaviour (aka operations) and a unique identity.

Page 33: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

33

6. Method a way to access, set or manipulate an object’s information.

7. Message Passing the process by which an object sends data to another object or asks the other object to invoke a method.

8. Polymorphism is the ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behaviour.

Page 34: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

34

Class Interface Diagram

Private data

hrs

mins

secs

SetTime

Increment

GetTime

Time(…)

Time

Time class

Constructors

Interface

Implementation

Public

Page 35: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

35

Object-Oriented Programming Elements

1. An object-oriented program is structured as a “community” of interacting “agents” called objects.

2. Computation is performed by the objects communicating with each other, requesting that other objects perform some actions for the requesting object.

Objects communicate by sending and receiving messages.

A message is a request for some action, bundled with whatever information (i.e. parameters) may be necessary to complete the task.

Page 36: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

36

3. Every object is an instance of a class.

A class simply represents a grouping (or template) of similar objects, such as integers, or lists, or whatever.

4. The class is the repository for behaviour associated with an object.

That is, all objects, that are instances of the same class, perform the same actions.

5. Classes may be organized into a singly-rooted tree structure, called an inheritance hierarchy.

Data and behaviour associated with instances of a class are automatically available to any class that is a descendant in this tree structure.

Page 37: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 37

More on Abstraction in OOP

• .

Page 38: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

38

Levels of Abstraction in O-O Programs

An O-O program may be viewed from many levels of abstraction.

At the highest level of abstraction, we may view an O-O program as a community of interacting objects.

Important characteristics here are the lines of communication between the various agents (i.e. objects),

indicating interactions.

Page 39: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

39

Abstraction in OO Programs - Units

The next level of abstraction is found in many (but not all) OO languages.

These languages permit a group of “agents” (classes or methods) working together to be combined into a “unit”.

Examples of a “unit”:

- package in Java

- namespace in C++ and C#

- class in C++, C# and Java

- Module in VB

Page 40: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

40

“Units” allow a programmer to surround a collection of agents (a small community in itself) with a “box”, and control visibility from outside the unit (information hiding).

The “unit” also provides encapsulation – it groups together a set of agents working together.

Page 41: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

41

Note: a class provides encapsulation at different level of abstraction compared to namespace and package.

A class groups together the attributes and operations of a single object, whereas namespaces and packages group together classes.

Page 42: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Modularity

Modular system – in which modules can be built separately and changes in one have minimum impact on others

Modularity supports independence of models Modularity enhances design clarity, eases

implementation Reduces cost of testing, debugging and

maintenance Cannot simply chop a program into modules

to get modularly Need some criteria for decomposition

Page 43: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Modularity

Criteria for achieving modularity: – coupling of modules

Low coupling is desirable, subject of Soft Eng – module cohesion

High cohesion is desirable, subject of Soft Eng – open closed principle – Liskov’s principle

Page 44: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Open-closed Principle

Principle: A module should be open for extension but closed for modification

Behavior can be extended to accommodate new requirements, but existing code is not modified

I.e. allows addition of code, but not modification of existing code

Minimizes risk of having existing functionality stop working due to changes – a very important consideration while changing code

Good for programmers as they like writing new code

Page 45: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Open-closed Principle… In OO this principle is satisfied by using

inheritance and polymorphism Inheritance allows creating a new class

to extend behavior without changing the original class

Consider example of a client object which interacts with a printer object for printing

Page 46: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Example

Page 47: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Example.. Client directly calls methods on Printer1 If another printer is to be allowed

A new class Printer2 will be created But the client will have to be changed if it

wants to use Printer 2 Alternative approach

Have Printer1 as a subclass of a general Printer For modification, add another subclass Printer

2 Client does not need to be changed

Page 48: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Example…

Page 49: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Liskov’s Substitution Principle

Principle: Program using object o1 of base class C should remain unchanged if o1 is replaced by an object of a subclass of C

If hierarchies follow this principle, the open-closed principle gets supported

Page 50: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Summary

Modularity is the criteria for deciding quality of the design

Modularity enhanced by low coupling, high cohesion, and following open-closed principle

Page 51: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

51

Abstraction in OO Languages – Object Clients and Servers

The next level of abstraction considers the relationship between two individual objects.

Typically one object (server) is providing a service, and the other object (client) is using the service.

Page 52: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

52

Abstraction in OO Languages - Description of Services

We can next examine just the “unit” providing a service, independent of the client.

We define the nature of the services that are offered, but not how those services are realized, i.e. implemented.

Example: operations for an integer stack data structure

void push(int val); // push data item onto stack

int pop(); // pop data item from stack

Services need to be declared as public, otherwise they will not be visible (i.e. accessible) to clients!

Page 53: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

53

Services offered:

push a data item onto the stack

pop a data item from the stack

Services offered:

push a data item onto the stack

pop a data item from the stack

Page 54: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

54

Interfaces are used to describe services at this level of abstraction.

A (service) interface is a mechanism that defines behaviour without describing an implementation.

It “advertizes” the services provided by a class – the public interface, i.e. the methods available to client classes and how they may be used.

E.g. interface in C++ for a stack:

class Stack { public: void push(int val); int pop(); ...};

Note that the use of the word“interface” here is not the same

as the syntactic entityinterface found in Java and C#.

Page 55: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

55

Levels of Abstraction - An Implementation

Next we look again at the services provided, but this time from the implementation side.

E.g. in C++ for a stack

class Stack { public: void push(int val); int pop(); private: const int StackSize = 40; int top: int data[StackSize];};

Concern here is with the high-level approach to providing the designated service. Here, an array is being used to implement the Stack.

Page 56: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

56

Levels of Abstraction - A Method in Isolation

Finally, we consider the implementation of each method in isolation – the precise sequence of operations required.

E.g. in C++

class Stack { public: void push(int val); int pop(); private: const int StackSize = 40; int top: int data[StackSize];}; int Stack::pop(){ return data[top--]; }

Page 57: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

57

Every level of abstraction is important, Every level of abstraction is important, and often you move quickly back and and often you move quickly back and

forth between levels.forth between levels.

Page 58: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

58

Abstraction Classification

Abstraction is used to help understand a complex system

– to separate out the important details from the unnecessary ones for the software solution.

This concept of abstraction can be subdivided into a variety of different forms …

Page 59: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

59

Page 60: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

60

“Is-a” and “Has-a” abstractions

The ideas of

division into parts and

division into specializations

represent the two of the most important forms of abstraction used in object-oriented programming.

These two important types of abstraction are

• Division into parts “Has-a” abstraction

• Division into specialization “Is-a” abstraction

Page 61: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

61

“Has-a” Abstraction

Division into parts takes a complex system, and divides it into component parts, which can then be considered in isolation.

It is characterized by sentences that have the words such as “has-a”

• A car has-a engine, and has-a transmission • A bicycle has-a wheel • A window has-a menu bar

Allows us to drop down a level of complexity when we consider the component in isolation – specialization.

Works downwards

Page 62: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

62

Is-a Abstraction

Is-a abstraction takes a complex system, and views it as an instance of a more general abstraction.

It is characterized by sentences that have the words “is-a”

• A car is-a wheeled vehicle, which is-a means of transportation • A bicycle is-a wheeled vehicle • A pack horse is-a means of transportation

Allows us to categorize an artifact and make it applicable to many different situations – generalization.

Works upwards

Page 63: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

63

Both of the previous forms of abstraction workon a hierarchical tree of concepts – down or up.

Page 64: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

64

Layers of Specialization

Yet another approach to dealing with complexity is to structure abstraction using layers of specialization.

This is sometimes referred to as a taxonomy.

For example, in biology, we divide living things into animals and plants.

Animals are then divided into vertebrates and invertebrates.

Vertebrates includes mammals, which can be divided into (among other categories) cats and dogs, and so on.

Page 65: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

65

The same technique is used in object-oriented languages.

New interfaces can be formed from existing interfaces. (An interface is a syntactical construct in C# and Java. See later!)

A new class can be formed by inheriting from an existing (super) class.

In doing so, all the properties (data fields and behaviour) we associate with the original class become available to the new class.

Page 66: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

66

Page 67: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

67

So, a Java application may use the following methods:

setTitle(String) inherited from class Frame

setSize(int, int) inherited from class Component

show() inherited from class Window

repaint() inherited from class Component

paint() inherited from class Component

Page 68: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

68

Inheritance Hierarchy

Another form of abstraction is a taxonomy, in object-oriented languages more often termed an inheritance hierarchy or generalization.

Here the layers are more detailed representatives of a general category.

- An example of this type of system is a biological division into categories such as

Living ThingAnimal

MammalCat

Page 69: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

69

Living ThingAnimal

MammalCat

Each lower level is a more specialized version of the previous.

Each upper level is a generalization of the previous lower level.

Page 70: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

04/19/23 Assoc. Prof. Stoyan Bonev 70

More on message passing

• i

Page 71: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

71

Message-Passing Syntax

Use the term message passing (sometimes also called method lookup) to mean the dynamic process of requesting an object to perform a specific action.

In particular:

- A message always specifies some object, which is called the receiver, to perform some action.

- The action performed in response to the message is not fixed, but may differ depending upon the class of the receiver. That is, different objects may accept the same message, and yet perform different actions – different classes may have similarly-named methods.

Page 72: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

72

Message passing in C++, C#, Java - Examples

aCard.flip();

aCard.setFaceUp(true);

aGame.displayCard(aCard, 45, 56);

Most OO languages implement message passing by invoking a class method

such as flip() or setFaceUp(true) or displayCard(aCard, 45, 56)

Page 73: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

73

Message Passing Syntax

Although the syntax may differ in different languages, all messages have three identifiable parts: E.g.

• The receiver (the object to which the message is being sent) – the object which will perform some action.

• The selector (the text that indicates the particular message being sent) – the action, i.e. method being invoked.

• An optional list of arguments

aGame.displayCard(aCard, 42, 27)

receiver selector arguments

Page 74: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

74

The most common syntax for message passing usesa dot operator (.) to separate the receiver from the message selector.

aCard.flip();

aCard.setFaceUp(true);

aGame.displayCard(aCard, 45, 56);

Page 75: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

75

The Receiver Variable – accessing the receiver from within a method

A message is always passed to a receiver (i.e. the object for which a method is being invoked).

aGame.displayCard(aCard, 42, 27)

In most OO languages, however, the receiver does not appear in the argument list for the method.

Instead, the receiver is only implicitly involved in the method invocation.

But sometimes, it is necessary to access the receiver from within the method.

Page 76: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

76

Inside a method, the receiver object can be accessed by means of a “pseudo-variable”.

A pseudo-variable is like an ordinary variable, only it need not be declared and cannot be modified – it is a syntactic keyword of the programming language.

• It is called this in Java, C++ and C#

• Every method of an object has a this reference that refers to the current object for which the method has been called.

• It is used in a method body when reference to the object itself is needed – self-reference

Page 77: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

77

Implicit Use of This

Within a method, a message expression or a data access with no explicit receiver is implicitly assumed to refer to this, i.e. the current receiver object.

Example: code in C#

class PlayingCard {

...

public void flip() {setFaceUp(!faceUp);}

...

} A method and data variablewith no receiver specified.

Page 78: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

78

This is assumed to be equivalent to

class PlayingCard {

...

public void flip() {this.setFaceUp(!this.faceUp);}

...}

Page 79: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

79

Some style guidelines for Java and C# suggest the use of this when arguments in a constructor are used to initialize a data member.

The same name can then be used for the argument and a data member, with an explicit this being used to access the data member, and so distinguish between the two (identical) names.

class PlayingCard { public PlayingCard (int suit, int rank) { this.rank = rank; // this.rank is the data member this.suit = suit; // rank is the argument value this.faceUp = true; } ... private int suit; private int rank; private boolean faceUp;}

Page 80: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

80

In C++, the pseudo-variable is a pointer – the this pointer.

Again, the this pointer can be used to refer to the invoking object inside a method.

class MyClass {

int i ...};

i = 10; // within some method - implicit

Or

this->i = 10; // explicit - equivalent

Page 81: 9/3/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 01 Title: OOP Basic Intro Reference: COS240 Syllabus.

Thank Youfor

Your attention!