Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lectures / lecture3_1

.pdf
Скачиваний:
36
Добавлен:
14.10.2016
Размер:
887.32 Кб
Скачать

References to Different Object Types

The reference

The object

type is Shirt.

type is Shirt.

Shirt myShirt = new Shirt(); myShirt.display();

Trousers myTrousers = new Trousers(); myTrousers.display();

The reference

The object type is

type is Trousers.

Trousers.

References and Objects In Memory

int counter = 10;

Shirt myShirt = new Shirt(); Shirt yourShirt = new Shirt();

0x034009

Stack

 

12

shirtID

Heap

 

 

 

 

 

15.99

price

 

 

 

B

colorCode

 

counter

10

0x99f311

 

 

 

 

 

 

myShirt

0x034009

12

shirtID

 

 

 

15.99

price

 

yourShirt

0x99f311

B

colorCode

 

 

 

 

Assigning a Reference to Another Reference

myShirt = yourShirt;

12

15.99

B

counter

10

0x99f311

 

 

 

 

myShirt

0x99f311

12

shirtID

 

 

15.99

price

yourShirt

0x99f311

B

colorCode

 

 

Two References, One Object

Code fragment:

Shirt myShirt = new Shirt();

Shirt yourShirt = new Shirt();

myShirt = yourShirt;

myShirt.colorCode = 'R'; yourShirt.colorCode = 'G';

System.out.println("Shirt color: " + myShirt.colorCode);

Output from code fragment:

Shirt color: G

Assigning a Reference to Another Reference

myShirt.colorCode = 'R'; yourShirt.colorCode = 'G';

12

15.99

B

counter

10

0x99f311

 

 

 

 

myShirt

0x99f311

12

shirtID

 

 

15.99

price

yourShirt

0x99f311

G

colorCode

 

 

Quiz

Which of the following lines of code instantiates a Boat object and assigns it to a sailBoat object reference?

a.Boat sailBoat = new Boat();

b.Boat sailBoat;

c.Boat = new Boat()

d.Boat sailBoat = Boat();

Java Class Design

Using Access Control

You have seen the keywords public and private. There are four access levels that can be applied to data fields and methods. The following table illustrates access to a field or method marked with the access modifier in the left column.

Modifier

Same Class

Same

Subclass in

Universe

(keyword)

 

Package

Another

 

 

 

 

Package

 

 

 

 

 

 

 

 

 

 

 

private

Yes

 

 

 

 

 

 

 

 

default

Yes

Yes

 

 

 

 

 

 

 

protected

Yes

Yes

Yes *

 

 

 

 

 

 

 

 

public

Yes

Yes

Yes

Yes

 

 

 

 

 

Classes can be default (no modifier) or public.

Protected Access Control: Example

1

package demo;

 

2

public class Foo {

 

 

 

3

protected int result = 20;

subclass-friendly

4

int other = 25;

declaration

5

}

 

 

 

 

 

 

 

 

1package test;

2import demo.Foo;

3public class Bar extends Foo {

4private int sum = 10;

5public void reportSum () {

6

 

 

sum += result;

7

 

 

sum += other;

8

 

}

compiler error

9

}

 

 

 

Field Shadowing: Example

1package demo;

2public class Foo2 {

3protected int result = 20;

4}

1package test;

2import demo.Foo2;

3public class Bar2 extends Foo2 {

4private int sum = 10;

5private int result = 30;

6public void reportSum() {

7 sum += result;

8}

9}

result field shadows the parent's field.

Соседние файлы в папке Lectures