JAVA METHODS
JAVA METHODS
KIT-501
CLASS IN JAVA
CLASS NAME: The name should begin with an initial letter (capitalized by convention).
SUPERCLASS(IF ANY): The name of the class’s parent (superclass), if any, preceded by
the keyword extends. A class can only extend (subclass) one parent.
Here, fields (variables) and methods represent the state and behaviour of the
object respectively.
• Fields are used to store data
• Methods are used to perform some operations
JAVA OBJECTS
An object is called an instance of a class. A class is a template or blueprint from which objects
are created. So, an object is the instance(result) of a class. an object is created from a class.
• Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
• Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to
the external user. However, it is used internally by the JVM to identify each object uniquely.
• An object is a real-world entity. The object is an entity which has state and behavior. An
object is a runtime entity.
EXAMPLE
//Defining a Student class.
class Student{
//defining fields
int id; //field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student(); //creating an object of Student
//Printing values of the object
System.out.println(s1.id); //accessing member through reference variable
System.out.println(s1.name);
}
}
METHODS IN JAVA
• A method is a block of code or collection of statements or a set of code grouped together to perform a
• 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. You can pass data, known as parameters, into a
method.
• A method in Java refers to a block of code that is associated with an object or a class. In object-oriented
programming (OOP), methods are functions that belong to a specific class or instance of that class.
• Methods are typically used to define the behaviour of objects and can access the instance variables of the
class.
METHOD DECLARATION
accessModifier returnType methodName(parameters) {
// Method body
// Code to perform a specific task
}
• accessModifier: Determines the visibility of the method (e.g., public, private,
protected, or package-private).
• returnType: Specifies the data type of the value that the method returns. Use void if
the method doesn't return any value.
• methodName: A unique name for the method within its class.
• parameters: A list of input parameters enclosed in parentheses. You can have zero or
METHOD EXAMPLE
public int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
• ACCESS SPECIFIER: Access specifier or modifier is the access type of method. It specifies visibility of the method.
• RETURN TYPE: Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
• METHOD NAME: It is a unique name that is used to define the name of a method. A method is invoked by its name.
• PARAMETER LIST: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method has no parameter, left the parentheses blank.
• METHOD BODY: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed
within the pair of curly braces.
ACCESS SPECIFIERS
Public: The method is accessible by all classes when we use public specifier in our
application.
Private: When we use a private access specifier, the method is accessible only in the
classes in which it is defined.
Protected: When we use protected access specifier, the method is accessible within
the same package or subclasses in a different package.
Default: When we do not use any access specifier in the method declaration, Java
uses default access specifier by default. It is visible only from the same package only.
NAMING A METHOD
While defining a method, remember that the 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. For example:
• Single-word method name: sum(), area()
It is also possible that a method has the same name as another method name
in the same class, it is known as method overloading.
METHOD INVOCATION
• For using a method, it should be called. There are two ways in which a
method is called i.e., method returns a value or returning nothing (no
return value).
// return statement
return num * num;
}
1. INSTANCE METHOD: Access the instance data using the object name. Declared inside a class.
// Instance Method
void method_name(){
2. STATIC METHOD: Access the static data using class name. Declared inside class with static keyword.
//Static Method
}
STATIC METHODS
• 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. We can also create a static method by using the
keyword static before the method name.
• The main advantage of a static method is that we can call it without creating an object. It can access static data
members and also change the value of it. It is invoked by using the class name. The best example of a static
method is the main() method.
Abstract methods in Java do not have any code in them. This means that there is no
need to provide the implementation code while declaring it. Instead, it is possible to
declare the method body later in the program.
There is another hard rule to declare abstract methods, and it is that they can only be
declared within an abstract class. It always declares in the abstract class. It means the
class itself must be abstract if it has abstract method.
It is known that one can declare an abstract method by using the “abstract” keyword.
// Abstract class public class Main {
abstract class Animal {
// Abstract method (no implementation) public static void main(String[] args) {
public abstract void makeSound();
// Creating instances of Dog and Cat
}
Animal dog = new Dog();
// Concrete subclass
class Dog extends Animal { Animal cat = new Cat();
@Override
public void makeSound() {
System.out.println("Dog barks"); // Calling abstract method makeSound() on both objects
} dog.makeSound();
}
cat.makeSound();
// Concrete subclass
}
class Cat extends Animal {
@Override }
public void makeSound() {
System.out.println("Cat meows");
}
}
METHOD OVERLOADING
• Java allows you to define multiple methods with the same name but
different parameter lists. This is called method overloading. Java
determines which method to call based on the number and types of
arguments provided.
RETURN TYPE: The return type can be different for overloaded methods. However,
the return type alone is not sufficient to distinguish overloaded methods.
When you call an overloaded method, Java determines which method to execute based on the number
and types of arguments you pass. Method overloading is a useful feature in Java because it allows you to
provide multiple ways to perform similar operations, making your code more readable and flexible.
MathUtils math = new MathUtils();
String result4 = math.concatenate("Hello", " World"); // Calls concatenate(String str1, String str2)
In the code above, Java automatically selects the appropriate add method based on the argument types. Similarly, it
selects the concatenate method based on the argument types (strings in this case).