0% found this document useful (0 votes)
8 views98 pages

05-Access Modifiers

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views98 pages

05-Access Modifiers

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

CS 102

Object Oriented Programming

Access Modifiers
Bank Account – version 9
2

Ozyegin University - CS 102 - Object Oriented Programming


Bank Account – version 9
3

Ozyegin University - CS 102 - Object Oriented Programming


Definition of deposit
4

¨ We should not allow depositing negative amount of


money.
¨ How?
Source: http://www.merriam-webster.com/dictionary/deposit

Ozyegin University - CS 102 - Object Oriented Programming


deposit function
5

Ozyegin University - CS 102 - Object Oriented Programming


deposit function
6

Ozyegin University - CS 102 - Object Oriented Programming


deposit function
7

Ozyegin University - CS 102 - Object Oriented Programming


Bank Account
8

¨ Can you think of any other controls that we should


have?

Ozyegin University - CS 102 - Object Oriented Programming


Bank Account
9

¨ Can you think of any other controls that we should


have?
¨ A bank account should get a number during
initialization.
¨ A bank account should not have negative initial
balance.

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
10

¨ Assume that we don’t have the interest rate


¨ We have the following constructors:

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
11

¨ A bank account should get a number during


initialization.

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
12

¨ A bank account should get a number during


initialization.
¨ Remove the following constructor.

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
13

¨ A bank account should get a number during


initialization.

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
14

¨ A bank account should not have negative initial


balance.

Ozyegin University - CS 102 - Object Oriented Programming


Negative Initial Balance
15

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
16

¨ A bank account should not have negative initial


balance.
¨ We should have check the initial balance.

Ozyegin University - CS 102 - Object Oriented Programming


Constructors
17

¨ A bank account should not have negative initial


balance.
¨ We should have check the initial balance.
¨ If it is negative, the balance should be 0.

Ozyegin University - CS 102 - Object Oriented Programming


What is the output?
18

Ozyegin University - CS 102 - Object Oriented Programming


What is the output?
19

Ozyegin University - CS 102 - Object Oriented Programming


So, are we done?
20

¨ With changing the constructor and the deposit


function, are we sure that balance will not be a
negative amount?

Ozyegin University - CS 102 - Object Oriented Programming


What is the output?
21

Ozyegin University - CS 102 - Object Oriented Programming


What is the output?
22

Ozyegin University - CS 102 - Object Oriented Programming


Class instances
23

¨ The class instances need to be protected.


¨ We need to keep the control of how these instances
are accessed.
¨ How?

Ozyegin University - CS 102 - Object Oriented Programming


Class instances
24

¨ The class instances need to be protected.


¨ We need to keep the control of how these instances
are accessed.
¨ How?
¤ Encapsualtion: A mechanism that provides protection of
some features of the class from the users.

Ozyegin University - CS 102 - Object Oriented Programming


Class instances
25

¨ The class instances need to be protected.


¨ We need to keep the control of how these instances
are accessed.
¨ How?
¤ Encapsualtion: A mechanism that provides protection of
some features of the class from the users. à Through
using access modifiers.

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifier
26

¨ They are used to set access levels for classes,


variables, and other entries.

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
27

¨ Access modifier
¤ For the top level classes it can be either
n public or : visible to the earth
n default (no keyword) : visible only within the same package

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
28

¨ These variables don’t have any particular access


modifier, therefore they are visible …?

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
29

¨ These variables don’t have any particular access


modifier, therefore they are visible and accessible
from only within the same package (package-
private).

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
30

¨ Let’s try to use them from outside the package

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
31

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
32

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
33

Ozyegin University - CS 102 - Object Oriented Programming


Access Specification
34

number, balance and currency


are visible in everywhere!

No access
related errors!

Ozyegin University - CS 102 - Object Oriented Programming


Important!!!
35

¨ However, making everything public is not the solution.


¤ When something is public, it can be accessed and also can
be modified from everywhere!
¨ It is also not a good idea to leave it package-private.
¤ In default case (without any access modifier) that
information can be accessed and modified everywhere
within the package.
¨ These are not optimum solutions.
¨ You should encapsulate that information and limit its
access and make sure that it can be modified only
within your control.
Ozyegin University - CS 102 - Object Oriented Programming
Controlling Access to Entries
36

¨ Each entry (class, class instance, member function) in


a Java class is marked with one of the following
keywords to control which classes have access to
that entry:
¤ public
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Controlling Access to Entries
37

¨ Each entry (class, class instance, member function) in


a Java class is marked with one of the following
keywords to control which classes have access to
that entry:
¤ public: the entry is accessible from everywhere
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Controlling Access to Entries
38

¨ Each entry (class, class instance, member function) in


a Java class is marked with one of the following
keywords to control which classes have access to
that entry:
¤ public
¤ private: the entry is accessible only within the class,
invisible everywhere outside the class
¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Controlling Access to Entries
39

¨ Each entry (class, class instance, member function) in


a Java class is marked with one of the following
keywords to control which classes have access to
that entry:
¤ public
¤ private

¤ no keyword (default): entry is accessible to classes


inside the same package, invisible to all the others.
package private.
¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Controlling Access to Entries
40

¨ Each entry (class, class instance, member function) in


a Java class is marked with one of the following
keywords to control which classes have access to
that entry:
¤ public
¤ private

¤ no keyword (default)

¤ protected: entry is accessible to the class itself, other


classes inside the same package and all subclasses.

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers
41

¨ Which one is the most restrictive one?


¤ public
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers
42

¨ Which one is the most restrictive one?


¤ public
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers
43

¨ Which one is the least restrictive one?


¤ public
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers
44

¨ Which one is the least restrictive one?


¤ public
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers
45

¨ Rank them in increasing order of restrictiveness?


¤ public
¤ private

¤ no keyword (default)

¤ protected

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers
46

¨ Rank them in increasing order of restrictiveness?


¤ public
¤ private

¤ no keyword (default)

¤ protected

¨ Answer:
¤ public, protected, default, private
n protected entities can be accessed by subclasses in other
packages

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers: Access levels
47

¨ private: the class itself


¨ default: private + classes inside the same package
¨ protected: default + all subclasses
¨ public: all classes

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers: Access levels
48

Class Package Subclass World


public
protected
default
private

Source: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Ozyegin University - CS 102 - Object Oriented Programming


Access Modifiers: Access levels
49

Class Package Subclass World


public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N

Source: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Ozyegin University - CS 102 - Object Oriented Programming


For most of the cases…
50

¨ Class instances should be private


¤ Only the class itself can access these variables
¤ They are visible only inside the class definition
n Only member functions of the class can access them
¤ They are invisible outside the class
¤ Therefore, the control is on the class itself only.

¨ There may be times for exceptions.


¤ Example: during inheritance

Ozyegin University - CS 102 - Object Oriented Programming


For most of the cases…
51

¨ Class methods should be public or private


¤ public if they will be used publicly
¤ private if they are useful for another class function but
not to be used by other classes directly
¨ There can be exceptions to these.

Ozyegin University - CS 102 - Object Oriented Programming


Bank Account – version 10
52

¨ Class instances

¨ Member functions were public already

Ozyegin University - CS 102 - Object Oriented Programming


No write access
53

Ozyegin University - CS 102 - Object Oriented Programming


No read access
54

Ozyegin University - CS 102 - Object Oriented Programming


Accessing Class Instances
55

¨ Since class instances are private, we won’t have


direct access to those instances
¤ no read or write access
¨ How can we access them?

Ozyegin University - CS 102 - Object Oriented Programming


Accessing Class Instances
56

¨ Since class instances are private, we won’t have


direct access to those instances
¤ no read or write access
¨ How can we access them?
¤ by using getters and setters

Ozyegin University - CS 102 - Object Oriented Programming


Getters and Setters
57

¨ Get and set methods allow customized access to


class instances
¤ getter for read access
n returns the class instance without modifying
¤ setter for write access
n modifies the class instance
n mostly assigns the function argument’s value to the class
instance

Ozyegin University - CS 102 - Object Oriented Programming


An example getter function
58

¨ getter for read access


¤ returns the class instance without modifying

Ozyegin University - CS 102 - Object Oriented Programming


An example getter function
59

¨ getter for read access


¤ returns the class instance without modifying

¤ What other getter functions do we need?

Ozyegin University - CS 102 - Object Oriented Programming


Getter Function
60

Ozyegin University - CS 102 - Object Oriented Programming


Setters
61

¨ Using private for class instances give more control to


the class.
¨ The class can enforce legal value assignments
through setters.

Ozyegin University - CS 102 - Object Oriented Programming


An example setter function
62

¨ setter for write access


¤ modifies the class instance
¤ mostly assigns the function argument’s value to the class
instance

Ozyegin University - CS 102 - Object Oriented Programming


An example setter function
63

¨ setter for write access


¤ modifies the class instance
¤ mostly assigns the function argument’s value to the class
instance

¤ Do we need other setter functions?

Ozyegin University - CS 102 - Object Oriented Programming


Setter Functions
64

¨ Do we need other setter functions?


¤ account number
n Initialized when an account is created
n Cannot be changed afterwards

¤ account balance
n We don’t use a set function but instead
n Deposit: to put money in a bank account
n Withdraw: to remove money from a bank account

Source:http://www.merriam-webster.com/dictionary

Ozyegin University - CS 102 - Object Oriented Programming


deposit and withdraw functions
65

¨ We already have the deposit function

Ozyegin University - CS 102 - Object Oriented Programming


deposit and withdraw functions
66

¨ Can you write down the withdraw function?

Ozyegin University - CS 102 - Object Oriented Programming


deposit and withdraw functions
67

¨ Can you write down the withdraw function?


¤ Do not let withdraw if
n withdraw amount is negative
n withdraw amount is larger than the balance

¤ Otherwise
n withdraw the money and update the balance

Ozyegin University - CS 102 - Object Oriented Programming


deposit and withdraw functions
68

Ozyegin University - CS 102 - Object Oriented Programming


deposit and withdraw functions
69

Ozyegin University - CS 102 - Object Oriented Programming


deposit and withdraw functions
70

Ozyegin University - CS 102 - Object Oriented Programming


setCurrency function
71

¨ Lets review setCurrency function

¨ 1 USD = 18. 22TL


¨ How should we modify the above function?

Ozyegin University - CS 102 - Object Oriented Programming


setCurrency function
74

Ozyegin University - CS 102 - Object Oriented Programming


What is the output?
75

Ozyegin University - CS 102 - Object Oriented Programming


Unknown currency?
77

¨ What happens in the following case?

Ozyegin University - CS 102 - Object Oriented Programming


Unknown currency?
78

¨ How can we fix this setCurrency function?

Ozyegin University - CS 102 - Object Oriented Programming


Fixing setCurrency Function
79

Ozyegin University - CS 102 - Object Oriented Programming


Unknown currency?
80

¨ The same thing can happen in constructor as well.

Ozyegin University - CS 102 - Object Oriented Programming


Unknown currency?
81

¨ The same thing can happen in constructor as well.


¨ In default we should set it to “TL”

Ozyegin University - CS 102 - Object Oriented Programming


Fixing Constructors (Account V12)
82

Ozyegin University - CS 102 - Object Oriented Programming


Code Repetition
83

Ozyegin University - CS 102 - Object Oriented Programming


Code Repetition
84

How can we write a


function for this check?

Ozyegin University - CS 102 - Object Oriented Programming


Private Function
85

Ozyegin University - CS 102 - Object Oriented Programming


Private Function
86

Ozyegin University - CS 102 - Object Oriented Programming


Private Function
87

Ozyegin University - CS 102 - Object Oriented Programming


Private Function
88

¨ Functions which are helper functions to other


member functions should be kept private.
¤ Private function can be accessed from within the class.
¤ Private function can not be accessed from outside the
class.

Ozyegin University - CS 102 - Object Oriented Programming


Get and Set Functions
89

¨ Setter methods usually begins with ‘set’ prefix.


¤ setCurrency
¨ Getter methods usually begins with ‘get’ prefix.
¤ getCurrency
¤ But there is an exception for Boolean values
n For Boolean values the prefix ‘is’ usually used.

Ozyegin University - CS 102 - Object Oriented Programming


Boolean Get Functions (Account V13)
90

¨ Getter methods usually begins with ‘get’ prefix.


¤ But there is an exception for Boolean values
n For Boolean values the prefix ‘is’ usually used.

¤ Assume that some accounts can be active while some of


them are not. They can be on hold.
n Keep active information within a boolean

Ozyegin University - CS 102 - Object Oriented Programming


Boolean Get Functions (Account V13)
91

¨ Getter methods usually begins with ‘get’ prefix.


¤ But there is an exception for Boolean values
n For Boolean values the prefix ‘is’ usually used.

Ozyegin University - CS 102 - Object Oriented Programming


Get Functions (Account V13)
92

Ozyegin University - CS 102 - Object Oriented Programming


Get Functions (Account V13)
93

¨ For set functions you can still use ‘set’ prefix


¤ setActive

Ozyegin University - CS 102 - Object Oriented Programming


Ways of printing out the object - 1
94

¨ get methods for accessing class instances one by one

Ozyegin University - CS 102 - Object Oriented Programming


Ways of printing out the object - 2
95

¨ report method for printing report of the account


Ways of printing out the object
96

¨ Similar to other primitive types, can we just use the


object inside System.out.println() function?

¨ What do you think the output will look like?

Ozyegin University - CS 102 - Object Oriented Programming


Ways of printing out the object
97

¨ Similar to other primitive types, can we just use the


object inside System.out.println() function?

Ozyegin University - CS 102 - Object Oriented Programming


Ways of printing out the object
98

¨ Similar to other primitive types, can we just use the


object inside System.out.println() function?

¨ In order to get smt meaningful, we need to override


toString method of the class.
Ozyegin University - CS 102 - Object Oriented Programming
toString method
99

¨ toString method tells Java how to display an object


of the class.
¨ It returns a String representation of the object.

Ozyegin University - CS 102 - Object Oriented Programming


toString method
100

Ozyegin University - CS 102 - Object Oriented Programming


103 Any Questions ?

Ozyegin University - CS 102 - Object Oriented Programming

You might also like