AccessSpecifiers Methods Constructors (Ppt3)
AccessSpecifiers Methods Constructors (Ppt3)
LEVEL PRACTITIONER
Created By:
Sangeetha Mohan(139944)
Credential
Information:
Associate - Projects
Version and
Date:
Icons Used
Questions
Tools
Coding
Standard
s
Test Your
Understan
ding
Demonstrati
on
Best
Practices
& Industry
Standards
Hands on
Exercise
Case
Study
Worksho
p
Objectives
After completing this chapter you will be able to:
Apply Access Modifiers
Develop a java method
Explain encapsulation
Overload a method
Use static keyword
Understand Constructors
Overload the constructor
Access Modifier
FaceBookProfile
No access: Age
private access:
EmailId
Public access:
School
Access
Modifie
rs
Public
Private
Protect
ed
Default
Public access
What is public access?
Public access specifies that the class members (variables or
methods) are accessible to anyone, both inside and outside the
class and outside of the package.
Syntax: public <method/variable name>
Example:
Public variable:
public int x = 0;
Public Method:
public addNumbers(int x, int y){
// some code here
}
8
Public access
Illustration for public access:
Assume there are 3 java classes A, B, and C
Classes A & B are in the same package where as Class C is in a
different package
Class A has a public variable
Class B will be able to access the public variable in Class A
Class C will be able to access the public variable in Class A
C
public
Same Package
Another Package
9
Private access
What is private access?
Private access specifies that class member are only accessible by the
class in which they are defined. Cannot be accessed by any other class.
Syntax : private <variable/method name>
Example:
Private variable:
private int x = 0;
Private Method:
private addNumbers(int x, int y){
// some code here
}
10
Private access
Illustration for private access:
Taking the same example illustrated before
Class A has a private variable
Class B will not be able to access the private variable in Class A
Class C will not be able to access the private variable in Class A
Only methods in class A can access the private variable in Class
A.
C
privat
e
Same Package
Another Package
11
Protected access
What is protected access?
Protected access specifies that the class members/methods are
accessible to only the methods in that class, classes from same
package and the subclasses of the class. The subclass can be in
any package
Syntax: protected <variable/method name>
Example:
Protected variable:
protected int x = 0;
Protected Method:
protected addNumbers(int x, int y){
// some code here
}
12
Protected access
Illustration for protected access:
Class A has a protected variable.
Protected variable in Class A can be accessed by Class B in the
same package
It can be accessed by class D since Class D is a subclass of Class
A
It cannot be accessed by class C since class C is in a different
package and is not a sub class of class A.
B
A
extends A
protect
ed
Same Package
Another Package
13
Default access
What is default access?
Default/No access specifies that only classes in the same package
can have access to the variables and methods of the other class
No keyword is required for the default modifier and it is applied in the
absence of an access modifier.
B
A
C
defaul
t
Same Package
Another
Package
14
Code Example
Public access
Protected
to
school
Default/no
access
to
variable
access to
employerNam
Private
access
emailId
e variable
to age
variable
Public
access to
variable
getSchool
Protected
access to
method
getEmployerName
(Everyone
method can
(Packageaccess)
& subclass
Default/no
access to
access)
getEmailId method
(Package
accessto
Private access
only)
getAge
method
(Access
within class only)
15
Modifiers in a nutshell
Access
Modifiers
Same
Class
Same
Package
Subclass
Other
packages
public
Protected
No access
modifier
Private
Time To Reflect
Methods
What is a method?
A method is a set of statements to perform a desired functionality which
can be included inside a java class.
This set of statements can be called (invoked) at any point in the program
by using the method name.
Example: main method in class A invokes method
add in class B and method multiply in class C.
ClassB{
add(){
//write code
here
ClassA{
public static void
main{
Statement 1
add();
Statement 2
multiply();
}
}
}
}
ClassC{
multiply(){
// write code
here
}
}
18
Method Declaration
How to declare a method?
Syntax:
<modifier> <returnType> <methodName>(<parameter>*) {
<statement>*
}
Example:
public int add(int x, int y){
int sum = x+y;
return sum;
}
is the parameter or
arguments
passed to this
is the name
ofthe
thestatements
is
is the access method. These values
is themethod.
<returnType>
is
This
the
can
result
beinside
any
value the
that
present
<modifier> that
are
used
inside
the
that meaningful
indicatesisthe
being
word
datasent
that back
to
method
perform
specifies the visibility
ofAny that
method.
number
of
type describes
of the return
the
the
calling
value.
purpose
method
of
the actual
function
the method. arguments
can
be
the method
passed
19
Encapsulation
What is encapsulation?
20
What is Encapsulation?
What is Encapsulation?
Encapsulation is one of the fundamental OOP concepts
It is the protective barrier that prevents the data in a class from
being directly accessed by the code outside the class.
Access to the data and code is tightly controlled by using an
interface.
Example: In Face book people dont share their age information.
21
22
23
24
25
27
28
29
30
Time To Reflect
Method Overloading
What is method overloading?
Let us recollect what we learnt in the Introduction to OOPS session
Implementation 1:
makeSound()
{
Bark woof woof
}
Implementation 2:
makeSound(injured) // Added input
parameter, overloaded method.
{
Make a whining sound.
}
Method Overloading
What is method overloading?
Two different versions of the same method available in the same
class.
This can be done by changing the input parameter .
Example: The method add has been overloaded below,
33
34
35
Static keyword
Static keyword:
The static keyword is used before a method or variable (similar to an
access modifier).
Examples:
variable: private static int x = 0;
method: public static add(){
// some code here
}
36
Static keyword
What is a static member?
Static variables are global variables, which is shared by all the
instances of the class.
Static means that there will only be only one instance of that object
in the class.
It is not required to create an instance of the object to access the
static method or variable.
Where is it used?
Static variables are used in singleton implementation. You will
learn more about this during the design pattern session.
37
38
Reason:
Before s1 is created, X = 0 and Y = 3;
When s1 is created, X = 1 and Y = 8 (as per logic inside constructor)
When s2 is created, X = 2 since the value of X does not change with
every instance. The value of X is shared across all instances.
But Y = 8 (since a new object of Y is created and incremented by 5.
Similarly, when s3 is created, X = 3 and Y = 8
39
Where is it used?
Static methods are used in singleton implementation. You will
learn more about this during the design pattern session.
40
41
Time To Reflect
42
Constructor
What is a constructor?
A constructor is a special method used for creating an object of a
class and initializing its instance variables
Constructors should have the same name as the class
It should not have any return, not even void.
It is not mandatory for the developer to write a constructor.
If constructor is not defined by developer java will use the
default constructor to create objects.
43
Default Constructor
Default Constructor (no-arg constructor):
Default
Constructor
44
Overloading Constructors
What is overloaded constructor?
A default constructor with one or more arguments is called a
overloaded constructor
45
Overloaded Constructors
46
this Reference
The this keyword:
this refer to the current object instance itself.
It can be used for only instance variables and not for static or class
variables
It is used for also invoking the overloaded constructors.
It is used to access the instance variables shadowed by the parameters
in methods. Typically used with the encapsulated field.
Example: A method calculateTax has a parameter salary, also the
class has the instance variable salary. In the method if we refer using
this keyword it means we are referring to instance variable.
Syntax:
this.<name of instance variable>
this.<constructor Name><arguments>
47
48
49
50
Time To Reflect
51
Core Java