chapter 03 enscapsulation

13

Click here to load reader

Upload: nurhanna-aziz

Post on 26-May-2015

410 views

Category:

Travel


0 download

TRANSCRIPT

Page 1: Chapter 03 enscapsulation

Chapter 3: EncapsulationChapter 3: Encapsulation

Page 2: Chapter 03 enscapsulation

Data Hiding

• 1 of the important OOP techniques

• Hiding the data within the class

• Making it available ONLY through the methods• Making it available ONLY through the methods

• Also known as encapsulation

▫ It seals the data (and internal method) safely inside

the “capsule” of the class

▫ Can only be accessed by a trusted user (i.e. by the

method of the class)

Page 3: Chapter 03 enscapsulation

Why need encapsulation?

• To hide the internal implementation details of the

class

• Can safely modified the implementation without • Can safely modified the implementation without

worrying breaking the existing code that uses the

class

• Protect class against accidental/ willful stupidity

• Keeps class tidy by keeping the visible fields to a

minimum

• Easier to use and understand

Page 4: Chapter 03 enscapsulation

Access Control

• All the fields and methods of a class can always be

used within the body of the class itself.

• Java defines access control rules that restrict

members of a class from being used outside the members of a class from being used outside the

class.

• The public keyword, along with protected and

private, are accesscontrolmodifiers ; they specify

the access rules for the field or method.

Page 5: Chapter 03 enscapsulation

Access to packages• A package is always accessible to code defined within

the package. • Whether it is accessible to code from other packages

depends on the way the package is deployed on the host system. system. • When the class files that comprise a package are stored

in a directory, for example, a user must have read access to the directory and the files within it in order to have access to the package. • Package access is not part of the Java language itself. • Access control is usually done at the level of classes and

members of classes instead

Page 6: Chapter 03 enscapsulation

Access to classes

• By default, top-level classes are accessible within

the package in which they are defined.

• However, if a top-level class is declared public, it • However, if a top-level class is declared public, it

is accessible everywhere (or everywhere that the

package itself is accessible).

Page 7: Chapter 03 enscapsulation

Access to members

• By default, members are also accessible throughout

the package in which the class is defined.

• This default level of access is often called • This default level of access is often called

packageaccess.

• The other three levels of access are defined by the public, protected, and privatemodifiers.

Page 8: Chapter 03 enscapsulation

example

// People can use this class.

public class Laundromat {

// They cannot use this internal field,

private Laundry[] dirty; private Laundry[] dirty;

// but they can use these public methods

public void wash() { ... }

// to manipulate the internal field.

public void dry() { ... }

}

Page 9: Chapter 03 enscapsulation

Access rules:• If a member of a class is declared with the publicmodifier,

it means that the member is accessible anywhere the containing class is accessible. This is the least restrictive type of access control. • If a member of a class is declared private, the member is

never accessible, except within the class itself. This is the most restrictive type of access control. most restrictive type of access control. • If a member of a class is declared protected, it is

accessible to all classes within the package (the same as the default package accessibility) and also accessible within the body of any subclass of the class, regardless of the package in which that subclass is defined. This is more restrictive than public access, but less restrictive than package access. • If a member of a class is not declared with any of these

modifiers, it has the default package access: it is accessible to code within all classes that are defined in the same package, but inaccessible outside of the package.

Page 10: Chapter 03 enscapsulation

Another example

The publicmethods are

the access points to this

class.s fields from the outside

java world.

Normally these methods are

referred as getters and referred as getters and

setters.

Therefore any class that

wants to access the variables

should access them through

these getters and setters.

Page 11: Chapter 03 enscapsulation

~continuedThe variables of the EncapTest class can be access as below:

Output:

Page 12: Chapter 03 enscapsulation

Benefits of having encapsulation

• The fields of a class can be made read-only or write-

only

• A class can have total control over what is stored in

its fieldsits fields

• The users of a class do not know how the class

stores its data.

• A class can change the data type of a fields, and a

users of the class do not need to change any of their

code

Page 13: Chapter 03 enscapsulation

Question & answer