0% found this document useful (0 votes)
23 views18 pages

Method in Java

The document discusses different types of methods in Java including predefined, user-defined, static, instance, accessor, mutator, and abstract methods. It also covers method declaration components and encapsulation in Java.

Uploaded by

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

Method in Java

The document discusses different types of methods in Java including predefined, user-defined, static, instance, accessor, mutator, and abstract methods. It also covers method declaration components and encapsulation in Java.

Uploaded by

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

METHOD IN JAVA

What is a method in Java?

 A method is a block of code or collection of statements or a set of code grouped together to perform a
certain task or operation.
 It is used to achieve the reusability of code.
 It also provides the easy modification and readability of code, just by adding or removing a chunk of code.
 The method is executed only when we call or invoke it.
Method Declaration

 The method declaration provides information about method attributes, such as visibility, return-type, name,
and arguments.
Method Declaration

 It has six components that are known as method header.


 Method Signature
 Access Specifier 
 Public
 Private
 Protected
 Default
 Return Type
 Method Name
 Parameter List
 Method Body
Naming a Method

 Method name must be a verb and start with a lowercase letter. 


 If the method name has more than two words, the first name must be a verb followed by adjective or noun.
 In the multi-word method name, the first letter of each word must be in uppercase except the first word.

 Single-word method name: sum(), area()


 Multi-word method name: areaOfCircle(), stringComparision()
Types of Method

 There are two types of methods in Java:


 Predefined Method
 User-defined Method
Predefined Method

 Predefined methods are the method that is already defined in the Java class libraries is known as predefined
methods.
 It is also known as the standard library method or built-in method.
 Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
 Each and every predefined method is defined inside a class. Such as print() method is defined in
the java.io.PrintStream class.
User-defined Method

 The method written by the user or programmer is known as a user-defined method.


 These methods are modified according to the requirement.
Static Method

 A method that has static keyword is known as static method. In other words, a method that belongs to a class
rather than an instance of a class is known as a static method.
 The main advantage of a static method is that we can call it without creating an object.
 It is invoked by using the class name.
 The best example of a static method is the main() method.
Instance Method

 The method of the class is known as an instance method.


 It is a non-static method defined in the class.
 Before calling or invoking the instance method, it is necessary to create an object of its class.
Instance Method

 There are two types of instance method:


 Accessor Method
 Mutator Method
Accessor Method

 Accessor Method: The method(s) that reads the instance variable(s) is known as the accessor method. It is
also known as getters.
 Example:
public int getId()    
{    
return Id;    

Mutator Method

 Mutator Method: The method(s) read the instance variable(s) and also modify the values. It is also known
as setters or modifiers.
 Example:
public void setRoll(int roll)   
{  
this.roll = roll;  
}  
Abstract Method

 The method that does not has method body is known as abstract method. In other words, without an
implementation is known as abstract method. It always declares in the abstract class. It means the class
itself must be abstract if it has abstract method. To create an abstract method, we use the keyword abstract.
 Syntax:
abstract void method_name();
 Example:
abstract class Demo //abstract class
{
//abstract method declaration
abstract void display();
}
Encapsulation in Java
Encapsulation in Java

 Encapsulation in Java is a process of wrapping code and data


together into a single unit, for example, a capsule which is mixed
of several medicines.
 We can create a fully encapsulated class in Java by making all the
data members of the class private. Now we can use setter and
getter methods to set and get the data in it.
 The Java Bean class is the example of a fully encapsulated class.
Advantage of Encapsulation in Java

 By providing only a setter or getter method, you can make the class read-only or write-only. In other
words, you can skip the getter or setter methods.
 It provides you the control over the data.
 It is a way to achieve data hiding in Java.
 The encapsulate class is easy to test.
 The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to
create an encapsulated class in Java.
THANK
YOU

You might also like