In Java, there are several types of methods that you can use based on their purpose and functionality. Here are some commonly used types of methods along with examples:

  1. Static Methods: These methods are associated with the class itself rather than an instance of the class. You can call static methods directly using the class name.
  2. To Create a Static method use the <static> keyword in the method.
  3. We can not override the static method.
java
public class MathUtils
public static int add(int a, int b) {
return a + b; 
 }
 } 
// Calling the static method without Object
int sum = MathUtils.add(5, 3);
  1. Instance Methods: These methods belong to an instance of a class and can be accessed through an object of that class.
  2. Instance methods also called a non-static method
  3. To call a non-static/instance method Object is required.
java
public class Circle
private double radius; 
public double calculateArea()
return Math.PI * radius * radius; }
 } 
// Creating an instance and invoking the instance method 
Public static void main(String args[]){
Circle myCircle = new Circle();
 myCircle.setRadius(3.5);
double area = myCircle.calculateArea();
}
}
  1. Constructor: Constructors are special methods used to create and initialize objects of a class. They have the same name as the class and do not have a return type.
java
public class Person
private String name; 
public Person(String name)
this.name = name; } 
}
// Creating an instance using the constructor
Person person = new Person("John");
  1. Getter and Setter Methods: These methods are used to get and set the values of private variables (encapsulated fields) in a class.
java
public class Car
private String color; 
public String getColor() {
return color;
 } 
public void setColor(String color)
this.color = color; 
 } 
}
Public static void main(String args[]){
// Using getter and setter methods
Car myCar = new Car(); 
myCar.setColor("Red");
String carColor = myCar.getColor();
}
}
  1. Method Overloading: In Java, you can have multiple methods with the same name but different parameters. This is known as method overloading.
java
public class MathUtils
public int add(int a, int b)
return a + b;
 }
public double add(double a, double b)
return a + b; 
 }
 }
Public static void main(String args[]){
// Calling the overloaded methods
MathUtils math = new MathUtils(); 
int sumInt = math.add(5, 3); 
double sumDouble = math.add(2.5, 4.7);
}
}

These are just a few examples of different types of methods in Java. There are other types as well, such as recursive methods, abstract methods, and more.

6.Method Overriding:

Method overriding in Java allows a subclass to provide a different implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameter list as the method in the superclass. Here's an example to demonstrate method overriding in Java:

java
class Animal
public void makeSound()
 System.out.println("Animal makes a sound"); }
 } 
class Dog extends Animal
@Override public void makeSound()
 System.out.println("Dog barks"); 
 } }
class Cat extends Animal
@Override public void makeSound()
 System.out.println("Cat meows"); 
 } } 
public class Main
public static void main(String[] args)
Animal animal = new Animal(); 
 animal.makeSound(); // Output: Animal makes a sound
Dog dog = new Dog();
 dog.makeSound(); // Output: Dog barks 
Cat cat = new Cat();
 cat.makeSound(); // Output: Cat meows } }

In the above example, the Animal class has a method called makeSound(). The Dog and Cat classes extend the Animal class and override the makeSound() method with their own implementations.

When we create an instance of the Animal class and invoke the makeSound() method, it prints "Animal makes a sound". However, when we create instances of the Dog and Cat classes and invoke the makeSound() method, it prints "Dog barks" and "Cat meows" respectively. The overridden methods in the subclasses provide a specialized implementation based on the type of animal.


How to create method in java?