Inf1-OP Classes and Objects - Part I Volker Seeker, adapting - - PowerPoint PPT Presentation

inf1 op
SMART_READER_LITE
LIVE PREVIEW

Inf1-OP Classes and Objects - Part I Volker Seeker, adapting - - PowerPoint PPT Presentation

Inf1-OP Classes and Objects - Part I Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics January 28, 2019 Why OO? Software engineering as managing change Changing code is hard and expensive, but


slide-1
SLIDE 1

Inf1-OP

Classes and Objects - Part I Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

School of Informatics

January 28, 2019

slide-2
SLIDE 2

Why OO?

slide-3
SLIDE 3

Software engineering as managing change Changing code is hard and expensive, but because the world changes, essential.

slide-4
SLIDE 4

Software engineering as managing change

How can we make changing code easy and cheap? ◮ minimise the amount of code that must change ◮ make it easy to work out which code must change → have the code that must change live together

slide-5
SLIDE 5

How can we make change easier and cheaper? Key idea: Information Hiding

Hide certain information inside well-defined pieces of code, so that users of that piece of code don’t depend on it, and don’t need to change if it changes. → e.g. Modularity via Functions

slide-6
SLIDE 6

Application Programming Interface

The interface between the user of the code and the implementation itself is called an Application Programming Interface (API).

API

Programmer

Implementation

slide-7
SLIDE 7

Intuition

Client API ◮ adjust volume ◮ switch channel ◮ switch to standby Implementation ◮ cathode ray tube ◮ 20” screen, 22 kg ◮ Sony Trinitron KV20M10 client needs to know how to use API implementation needs to know what API to implement

Implementation and client need to agree on API ahead of time.

slide-8
SLIDE 8

Intuition

Client API ◮ adjust volume ◮ switch channel ◮ switch to standby Implementation ◮ HD LED display ◮ 37” screen, 10 kg ◮ Samsung UE37C5800 client needs to know how to use API implementation needs to know what API to implement

Can substitute better implementation without changing the client.

slide-9
SLIDE 9

Data representation

Recall: a data type is a set of values and operations on those

  • values. May be

◮ primitive, built into the language with operations defined in the compiler/runtime, e.g. int, double, boolean ◮ user-defined, with operations defined in the programming language itself, e.g. PrinterQueue, HotelRoom, . . .

slide-10
SLIDE 10

Data representation

Recall: a data type is a set of values and operations on those

  • values. May be

◮ primitive, built into the language with operations defined in the compiler/runtime, e.g. int, double, boolean ◮ user-defined, with operations defined in the programming language itself, e.g. PrinterQueue, HotelRoom, . . . ◮ Intermediate case where user-defined types are provided with the standard libraries in Java, e.g. String.

slide-11
SLIDE 11

Hiding data representation

You shouldn’t need to know how a data type is implemented in

  • rder to use it. It should suffice to read the documentation: what
  • perations are there, what do they do?

→ Then you can write code that won’t need to change if the implementation changes.

This concept is known as Encapsulation

The general idea is not specific to OO, but Java does it differently from Haskell.

slide-12
SLIDE 12

Towards object oriented programming...

So far in this course, we’ve been doing Procedural programming [verb oriented] ◮ tell the computer to do this, then ◮ tell the computer to do that. You know: ◮ how to program with primitive data types e.g. int, boolean; ◮ how to control program flow to do things with them, e.g. using if, for; ◮ how to group similar data into arrays.

slide-13
SLIDE 13

Philosophy of object orientation

Problem: what your software must do changes a lot. Structuring it based on that is therefore expensive. The domain in which it works changes much less. → structuring your software around the things in the domain makes it easier to understand and maintain.

https://www.alphansotech.com/wp-content/uploads/2015/11/object-oriented-concept-13-728-1.jpg

slide-14
SLIDE 14

Philosophy of object orientation Object Oriented programming (OOP) [noun oriented]

◮ Things in the world know things: instance variables. ◮ Things in the world do things: methods. In other words, objects have state and behaviour.

slide-15
SLIDE 15

State and Behaviour State

◮ running (yes/no) ◮ speed (10mph) ◮ petrol (87%)

Behaviour

◮ start Engine ◮ stop Engine ◮ accelerate ◮ break ◮ refill petrol

slide-16
SLIDE 16

State and Behaviour State

◮ running (yes/no) ◮ speed (10mph) ◮ petrol (87%)

Behaviour

◮ start Engine ◮ stop Engine ◮ accelerate ◮ break ◮ refill petrol A program runs by objects sending messages (initiating behaviour) to one another, and reacting to receiving messages (e.g. changing state, sending more messages).

slide-17
SLIDE 17

Classes and Objects

How does this work in Java?

slide-18
SLIDE 18

Classes to organise code

Java is a class-based object-oriented language. All code is organised in classes which serve as user defined data types.

Car

boolean running int speed double petrol

startEngine() stopEngine() accelerate(int amount) break(int amount) refillPetrol(double amount)

State Behaviour

slide-19
SLIDE 19

Classes to organise code

Java is a class-based object-oriented language. All code is organised in classes which serve as user defined data types.

Car

boolean running int speed double petrol

startEngine() stopEngine() accelerate(int amount) break(int amount) refillPetrol(double amount)

State Behaviour

All the classes you wrote so far only defined behaviour.

slide-20
SLIDE 20

Creating a class instance

Now only one important thing is missing. A Constructor.

Car

boolean running int speed double petrol

startEngine() stopEngine() accelerate(int amount) break(int amount) refillPetrol(double amount)

State Behaviour

slide-21
SLIDE 21

Creating a class instance

Now only one important thing is missing. A Constructor.

Car

boolean running int speed double petrol

startEngine() stopEngine() accelerate(int amount) break(int amount) refillPetrol(double amount) Car()

Constructor State Behaviour

A Constructor is used to create an instance of a class which can then be used in your program.

slide-22
SLIDE 22

Classes as blueprints

Car Class new Car() Car Instances

◮ Constructor is a special method with the same name as the class ◮ Allocates memory for the class instance and initialises its state

slide-23
SLIDE 23

Instances are Objects

In Java, instances of classes are what you consider to be Objects.

slide-24
SLIDE 24

Car Example

Using a Car class and its API

Car mycar = new Car () ; mycar . s t a r t E n g i n e () ; mycar . a c c e l e r a t e (30) ; mycar . break (30) ; mycar . stopEngine () ; mycar . r e f i l l P e t r o l ( 0 . 5 ) ;

slide-25
SLIDE 25

Car Example

Using a Car class and its API

Car mycar = new Car () ; mycar . s t a r t E n g i n e () ; mycar . a c c e l e r a t e (30) ; mycar . break (30) ; mycar . stopEngine () ; mycar . r e f i l l P e t r o l ( 0 . 5 ) ;

Note that we have two independent ideas here: ◮ Conceptual objects (class instances) such as mycar are directly present in the program; ◮ They have static (compile-time) types (Car class) that define their behaviour.

slide-26
SLIDE 26

Objects ...

◮ have a static (compile-time) type defined inside a class ◮ are instances of classes created at runtime ◮ are created using a constructor and the new keyword

slide-27
SLIDE 27

Objects ...

◮ have a static (compile-time) type defined inside a class ◮ are instances of classes created at runtime ◮ are created using a constructor and the new keyword ◮ are reference types

slide-28
SLIDE 28

Objects are Reference Types

What happens in memory?

slide-29
SLIDE 29

Arrays in Memory

Recall what happens with arrays:

14 4 myarr memory int[] myarr = new int[5]; myarr[3] = 4; myarr[2] = myarr[3] + 10; reference

slide-30
SLIDE 30

Class instances in memory

What happens to our Car?

true 30 0.8 mycar memory Car myCar = new Car(); mycar.startEngine(); mycar.accelerate(30); mycar.break(30); mycar.stopEngine(); mycar.refillPetrol(0.5); reference boolean running int speed double petrol

slide-31
SLIDE 31

Class instances in memory

What happens to our Car?

true 30 0.8 mycar memory Car myCar = new Car(); mycar.startEngine(); mycar.accelerate(30); mycar.break(30); mycar.stopEngine(); mycar.refillPetrol(0.5); reference boolean running int speed double petrol

◮ creating a class instance reserves memory for its state (plus some interal extras) ◮ the constructor is executed to initialise this memory (hence new and ctor in combination) ◮ the local variable mycar holds a reference to the actual object representation in memory (same as for arrays)

slide-32
SLIDE 32

Closing the Loop on Arrays

The Java language specification states: An object is a class instance or an array.

In Java, arrays are treated like class instances, e.g.

◮ created using new ◮ referenced in memory ◮ underlying class definintion (hidden in the language implementation).

However, they differ in some ways, e.g.

◮ in the way their state is accessed: myarr[3] = 5; ◮ except for length: for( int i =0; i < myarr.length; i++) ◮ and have no behaviour methods.

slide-33
SLIDE 33

Class instances in memory

Copying an object instance:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = myCar; reference yourCar reference

slide-34
SLIDE 34

Class instances in memory

Copying an object instance:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = myCar; reference yourCar reference

Assigning the reference of an object instance to a local variable of the same type does not copy the

  • bject’s memory, only its reference!
slide-35
SLIDE 35

Class instances in memory

Copying an object instance:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = new Car(); yourCar.running = myCar.running; yourCar.speed = myCar.speed; yourCar.petrol = myCar.petrol; reference yourCar reference true 30 0.8

To copy an instance, a new one of the same type needs to be created and its entire state copied over.

slide-36
SLIDE 36

Class instances in memory

Comparing class instances:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = new Car(); System.out .println(myCar == yourCar); yourCar true 30 0.8

What does this print?

slide-37
SLIDE 37

Class instances in memory

Comparing class instances:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = new Car(); System.out .println(myCar == yourCar); yourCar true 30 0.8

What does this print?

False

slide-38
SLIDE 38

Class instances in memory

Comparing class instances:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = new Car(); System.out .println(myCar == yourCar); yourCar true 30 0.8

What does this print?

False

== compares object references not object states

slide-39
SLIDE 39

Class instances in memory

Comparing class instances:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = myCar; System.out .println(myCar == yourCar); yourCar

What does this print?

True

== compares object references not object states

slide-40
SLIDE 40

Class instances in memory

Comparing class instances:

true 30 0.8 myCar memory Car myCar = new Car(); Car yourCar = new Car(); System.out .println(myCar.speed == yourCar.speed); yourCar true 30 0.8

What does this print?

True

== compares object references not object states

in contrast to primitive types

slide-41
SLIDE 41

Class instances in memory

Comparing class instances: Conveniently, most classes coming with the Java library such as String or Integer implement the comparison method equals.

I n t e g e r sizeA = new I n t e g e r (700) ; I n t e g e r sizeB = new I n t e g e r (700) ; // p r i n t s t r u e System . out . p r i n t l n ( sizeA . e qu a l s ( sizeB ) ) ;

By convention, the equals method is implemented in a way that compares the states of two objects. (Later I will show you how you can do that for your own types.)

slide-42
SLIDE 42

Lets practice that!

slide-43
SLIDE 43

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

int a = 5;

4

int b = 5;

5

System.out.println(a == b);

6

}

7

}

slide-44
SLIDE 44

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

int a = 5;

4

int b = 5;

5

System.out.println(a == b);

6

}

7

} Prints true. Values of primitive types are compared with ==.

slide-45
SLIDE 45

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = new Integer(5);

4

Integer b = new Integer(5);

5

System.out.println(a == b);

6

}

7

}

slide-46
SLIDE 46

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = new Integer(5);

4

Integer b = new Integer(5);

5

System.out.println(a == b);

6

}

7

} Prints false. References of object instances are compared with ==.

slide-47
SLIDE 47

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = new Integer(5);

4

Integer b = new Integer(5);

5

System.out.println(a.equals(b));

6

}

7

}

slide-48
SLIDE 48

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = new Integer(5);

4

Integer b = new Integer(5);

5

System.out.println(a.equals(b));

6

}

7

} Prints true. States of object instances are compared with equals.

slide-49
SLIDE 49

Autoboxing and Unboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.

Integer num = 5;

If the conversion goes the other way, this is called unboxing.

I n t e g e r num = new I n t e g e r (5) ; i n t sum = 10 + num ;

slide-50
SLIDE 50

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = 5;

4

Integer b = 5;

5

System.out.println(a == b);

6

}

7

}

slide-51
SLIDE 51

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = 5;

4

Integer b = 5;

5

System.out.println(a == b);

6

}

7

} Prints true. Even though object references are compared, true is printed because the literal 5 is cashed by the compiler and the same object is used under the hood. This caching process of certain literal values is called Interning.

slide-52
SLIDE 52

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = 200;

4

Integer b = 200;

5

System.out.println(a == b);

6

}

7

}

slide-53
SLIDE 53

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

Integer a = 200;

4

Integer b = 200;

5

System.out.println(a == b);

6

}

7

} Prints False. Integer literals are only cashed from -128 until 127 (1 byte).

slide-54
SLIDE 54

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

String a = "this is a test";

4

String b = "this is a test";

5

System.out.println(a == b);

6

}

7

}

slide-55
SLIDE 55

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

String a = "this is a test";

4

String b = "this is a test";

5

System.out.println(a == b);

6

}

7

} Prints True. String literals are also interned. NOTE: Technically, the process of assigning a literal to a String object type is not autoboxing because a String literal is not a primitive type.

slide-56
SLIDE 56

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

String a = new String("this is a test");

4

String b = new String("this is a test");

5

System.out.println(a == b);

6

}

7

}

slide-57
SLIDE 57

What does it print?

1

class Main {

2

public static void main(String[] args) {

3

String a = new String("this is a test");

4

String b = new String("this is a test");

5

System.out.println(a == b);

6

}

7

} Prints False. If you explicitely use a constructor, two different

  • bject instances are created.
slide-58
SLIDE 58

Java rules for comparrisson

For Primitives use == For Object References use == For Object States use equals (if it is implemented)

slide-59
SLIDE 59

Class vs Instance Methods

slide-60
SLIDE 60

Using methods

Using a method associated with an instance of a class

Car myCar = new Car () ; myCar . s t a r t E n g i n e () ; myCar . a c c e l e r a t e (20) ;

The method is called by using the ’.’ operator on the variable name of the class instance.

slide-61
SLIDE 61

Using methods

Using a method associated with an instance of a class

Car myCar = new Car () ; myCar . s t a r t E n g i n e () ; myCar . a c c e l e r a t e (20) ;

The method is called by using the ’.’ operator on the variable name of the class instance.

But what about this?

double rnd = Math.random()∗ 10;

Here, the method is called by using the ’.’ operator on the class name itself.

slide-62
SLIDE 62

Class Methods vs. Instance Methods

Instance Methods: ◮ Associated with an object. ◮ Identifying an instance method requires an object name: myCar.startEngine() Class Methods: ◮ Associated with a class. ◮ Identifying a method in a separate class requires name of the class: Math.random().

slide-63
SLIDE 63

Class Methods vs. Instance Methods

Consider class methods to be globally available, should you be able to import the corresponding type. They are also called static methods indicated by the function modifier you need to use when implementing them. There is not just static behaviour, there is also static state which I will show you later.

slide-64
SLIDE 64

Summary

slide-65
SLIDE 65

Summary: Why use object orientation?

OO has taken the world by storm. Why?

slide-66
SLIDE 66

Summary: Why use object orientation?

OO has taken the world by storm. Why? It is well suited to support good software engineering practices.

Quick reminder: this is not a SE course, however, it lays the foundation for it.

slide-67
SLIDE 67

Summary: Why use object orientation?

OO has taken the world by storm. Why? It is well suited to support good software engineering practices.

Quick reminder: this is not a SE course, however, it lays the foundation for it.

◮ use objects to model real-world entities ◮ use classes to model domain concepts. ◮ These change more slowly than specific functional requirements, ◮ so what OO does is to put things together that change together as requirements evolve. Change is the thing that makes software engineering hard and interesting; OO helps manage it.

slide-68
SLIDE 68

Summary: in Java

◮ A variable can have

◮ a primitive type e.g., boolean, int, double; or ◮ a reference type: any class, e.g. String, Car, Color and any array type.

◮ Instances of reference types are created using new. ◮ Variables of reference types contain references to their representation in memory.

◮ Two references can refer to the same memory location. ◮ Copying the reference does not copy the state of the object ◮ == compares references, .equals compares state.

◮ Lastly, object behaviour can be expressed by using class and instance methods.

slide-69
SLIDE 69

Reading

No further reading yet.