24
1 Programming in Java: lecture 6 Objects, Classes and Instances Getters and setters Constructors and object initialization Wrapper Classes and Autoboxing Garbage collection and the heap Object oriented analysis and design Example Slides made for use with ”Introduction to Programming Using Java, Version 5.0” by David J. Eck Some figures are taken from ”Introduction to Programming Using Java, Version 5.0” by David J. Eck Lecture 6 covers Section 5.1 to 5.4

Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

1

Programming in Java: lecture 6

Objects, Classes and Instances Getters and setters Constructors and object initialization Wrapper Classes and Autoboxing Garbage collection and the heap Object oriented analysis and design Example

Slides made for use with ”Introduction to Programming Using Java, Version 5.0” by David J. EckSome figures are taken from ”Introduction to Programming Using Java, Version 5.0” by David J. EckLecture 6 covers Section 5.1 to 5.4

Page 2: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

2

Classes and Objects

A Class is a template Objects are objects Objects are instances of a given class

Integer Classstatic members

parseInt(String s)

Integer Objectnon-static member

equals(int i)

Page 3: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

3

Instances

Instance Instance variable Instance method Class Simple

class variable static int x

(only on copy)

Simple Object

int y

Simple Object

int y

class Simple { static int x; int y;}

class MyMain { public static void main (String[] args) {

Simple a = new Simple();Simple b = new Simple();

}

}

Page 4: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

4

Instance methods

Class Simple

class variable static int x

(only on copy)

Simple Object

int y

Simple Object

int y

class Simple { static int x; int y;

static int getX() { return x; }

int getY() {return y;

}}

class MyMain { public static void main (String[] args) {

Simple a = new Simple();Simple b = new Simple();

}

}

Page 5: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

5

Instance methods

Class Simple

class variable static int x

(only on copy)

Simple Object

int y

Simple Object

int y

class Simple { static int x; int y;

int getX() { return x; }

static int getY() {return y;

}}

class MyMain { public static void main (String[] args) {

Simple a = new Simple();Simple b = new Simple();

}

}

ERROR!

Page 6: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

6

null

References A variable does not contain an object

Heap

Main

Simple s = new Simple()

s = address

Simple Object

int y

Page 7: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

7

null

References A variable does not contain an object

Heap

Main

Simple s = new Simple()Simple s2 = s; // no new object

s = addresss2 = address

Simple Object

int y

Page 8: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

8

null

References A variable does not contain an object

Heap

Main

Simple s = new Simple()Simple s2 = s; // no new objectSimple s3; // no object at all

s = addresss2 = address

s3 = null

Simple Object

int y

Page 9: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

9

null

References A variable does not contain an object

Heap

Main

Simple s = new Simple()Simple s2 = s; // no new objectSimple s3; // no object at allSimple s4 = new Simple()

s = addresss2 = address

s3 = nulls4 = address

Simple Object

int y

Simple Object

int y

Page 10: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

10

Getter and Setters

Why not public variables Change all places where they have been used

class example { public int badIdea; int goodIdea;

public void setGoodIdea(int n) throws IllegalArgumentException { if (n > 5) {

throw new IllegalArgumentException(“n too large”);} goodIdea = n;

}}

Page 11: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

11

Getter and Setters

Why not public variables Change all places where they have been used

class example { public int badIdea; int goodIdea; int accessCount = 0;

public void setGoodIdea(int n) throws IllegalArgumentException { if (n > 5) {

throw new IllegalArgumentException(“n too large”);} goodIdea = n;

}

public int getGoodIdea() {accessCount++;return goodIdea;

}}

Page 12: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

12

Constructors

Object initialization Initializing instance variables Default constructor Same name as class Many constructors with different parameters

Page 13: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

13

Example

Page 14: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

14

Example2

Page 15: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

15

Calling a constructor

new <class-name> (<parameter-list>)

Page 16: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

16

Wrapper classes, Autoboxing

We have seen wrapper classes Integer x; int h = 20 x = h //Autoboxing x = x*20 //unboxing

Page 17: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

17

Garbage collection

Heap Object that no one knows exist Manual memory management Leaking memory Garbage collection

Page 18: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

18

StringBuffer

String b = “hej” b = b + “, goddag” b = b + “ og hallo” New object everytime StringBuffer b2 = “hej” b2.append(“, goddag”) b2.append(“ og hallo”)

Heap

hej

Page 19: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

19

StringBuffer

String b = “hej” b = b + “, goddag” b = b + “ og hallo” New object everytime StringBuffer b2 = “hej” b2.append(“, goddag”) b2.append(“ og hallo”)

Heap

hej

hej, goddag

Page 20: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

20

StringBuffer

String b = “hej” b = b + “, goddag” b = b + “ og hallo” New object everytime StringBuffer b2 = “hej” b2.append(“, goddag”) b2.append(“ og hallo”)

Heap

hej

hej, goddag

hej, goddagog hallo

Page 21: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

21

StringBuffer

String b = “hej” b = b + “, goddag” b = b + “ og hallo” New object everytime StringBuffer b2 = “hej” b2.append(“, goddag”) b2.append(“ og hallo”)

Heap

hej

hej, goddag

hej, goddagog hallo

StringBufferhej

Page 22: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

22

StringBuffer

String b = “hej” b = b + “, goddag” b = b + “ og hallo” New object everytime StringBuffer b2 = “hej” b2.append(“, goddag”) b2.append(“ og hallo”)

Heap

hej

hej, goddag

hej, goddagog hallo

StringBufferhej, goddag

Page 23: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

23

StringBuffer

String b = “hej” b = b + “, goddag” b = b + “ og hallo” New object everytime StringBuffer b2 = “hej” b2.append(“, goddag”) b2.append(“ og hallo”)

Heap

hej

hej, goddag

hej, goddagog hallo

StringBufferhej, goddag og hallo

Page 24: Programming in Java: lecture 6 - people.cs.aau.dkpeople.cs.aau.dk/~ulrik/teaching/F09/PJAVA/lecture6/slides6.pdf · 1 Programming in Java: lecture 6 Objects, Classes and Instances

24

OOA&D

Object Oriented Analysis and Design Programming with objects Reusable components