0% found this document useful (0 votes)
232 views

Java Methods

This document discusses different types of methods in Java including instance methods, class methods, getter and setter methods, abstract methods, final methods, native methods, and synchronized methods. It also covers access specifiers like public, private, protected, and package private. Instance methods operate on instance variables of a class and can access class variables. Class methods can be called without creating an instance and use the static keyword. Getter and setter methods retrieve and set private variable values. Abstract and native methods are declared but not defined in a class. Final methods cannot be overridden. Synchronized methods ensure thread-safe access to data. The level of access is most restrictive for private and most open for public.

Uploaded by

Jack Presado
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views

Java Methods

This document discusses different types of methods in Java including instance methods, class methods, getter and setter methods, abstract methods, final methods, native methods, and synchronized methods. It also covers access specifiers like public, private, protected, and package private. Instance methods operate on instance variables of a class and can access class variables. Class methods can be called without creating an instance and use the static keyword. Getter and setter methods retrieve and set private variable values. Abstract and native methods are declared but not defined in a class. Final methods cannot be overridden. Synchronized methods ensure thread-safe access to data. The level of access is most restrictive for private and most open for public.

Uploaded by

Jack Presado
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

METHODS

JAVA
METHODS

METHODS
Used to access and manipulate variables.
Used to set, retrieve, or mutate values stored
in a variable.
It states the type and name of the incoming
parameters. The parameters become local
variables of the method. Local variables may
be primitives or object references.
Types of methods: Instance methods and
Class Methods

INSTANCE METHODS
Methods defined in a class operate on
instance variables of the current instance
of a class.
They can also access the class variables
of that class.
Apart from performing other activities for a
class. Instance method may get or set
values for instance variables.

INSTANCE METHODS
A getter method is used for getting the
value of an instance variable and it
normally begins with the word get.

// Getter Method to retrieve Value in an Instance Variable

INSTANCE METHODS
A setter method can be used to set the value
to private variables. Also called the mutator
method, it begins with the word set.

//Setter Method to set Value in an Instance Variable

// Getter Method to retrieve Value in an Instance Variable

//Setter Method to set Value in an Instance Variable

CLASS METHODS

Certain methods defined in a class can operate


only on class variables.
We can invoke these methods directly using the
class name without creating an instance.
Also called as static methods, it uses static
modifier in the declaration of a static method.
It cannot access instance variables or instance
methods.
Accessing non-static variables or methods from a
static method results in a compile-time error.

CLASS METHODS
It can be used independent of the object. We do
not have to instantiate a class to use its static
method. It can be invoked directly without any
reference to the object of that class.
The main method is a static method, as it should
be used before any object of the class is created.
Static methods can invoke other static methods.

// Usage of static methods defined in Math class

10
Prepared by: Jyr Marie
V. Reyes

11
Prepared by: Jyr Marie V. Reyes

12

OTHER JAVA METHODS


o ABSTRACT METHODS
o FINAL METHODS
o NATIVE METHODS
o SYNCHRONIZED METHODS

13

ABSTRACT METHODS

abstract modifier is used to denote that


the implementation of a method is not
provided in the class in which the method
is declared.
Abstract methods are declared only inside
an abstract class ( A class declared with
an abstract modifier

FINAL METHODS
A class can inherit variables and methods
defined in another class. The class from which
variables and methods are inherited is called the
super class. The class that inherits is called
the sub-class. A sub-class can provide a
different implementation to a method inherited
from its super class. This is known as method
overriding.
Use the final modifier in the
method declaration if you do not want the
method to be overridden by its sub-classes.

14

15

NATIVE METHODS
When we want to use a library of functions
modifier written in another language such
as C or C++, we use the native modifier to
specify that the implementation of this
method was not done in Java.

16

SYNCHRONIZED METHODS
In a multi-threaded environment, many
threads may be operating on the same
data simultaneously. The methods that
are used to operate on the data
simultaneously
are
declared
as
synchronized, to ensure that information
accessed by the threads is controlled and
is done in a thread-safe manner.

17

ACCESS SPECIFICATIONS
o

The public Access Modifier

The protected Access Modifier

The package Access Modifier

The private Access Modifier

18

ACCESS SPECIFICATIONS

PUBLIC ACCESS MODIFIER


public modifier places no restrictions on the
variables and methods. When variables and
methods of a class are declared as public,
then objects of any class can access them.
The accessing class need not be a subclass of the accessed class or present in
the same package as the accessed class. A
totally unrelated class, which is present in a
different package, can also access public
variables and methods of a class.

19

20

PACKAGE ACCESS MODIFIER


package modifier is used when no access level
is specified. If we do not specify the access level
of a variable or a method or a class using the
access modifier, then by default the access level
is assumed to be package access. This means all
the classes that are present in the same package
can access the variables and methods of the
accessed class. A package is similar to a module.
ADA was one of the first language to introduce
the concept of package in programming
languages.

PROTECTED ACCESS
MODIFIER

21

protected modifier allows variables and methods


of a class to be accessed by the class in which
they are defined, its sub-classes and other
classes present in that package.
Used if you want to share the information only to
a limited persons (such as your friends and
family) and you do not want anybody else to
know about it.

22
Prepared by: Jyr Marie
V. Reyes

23

PRIVATE ACCESS MODIFIER


private access modifier is used to protect
very confidential information. There may be
instances that you do not want methods or
variables from a particular class to access
certain variables or methods from another
specific class.
Using private variables and methods of a class
provides maximum protection.

You might also like