Method in Java

Method is an action, In another word you can say the method is a behavior. It is defined in the class and it is accessible by instances of the class or by the class name. And you can overload and override the method. In Java, there are four types of methods.

  • Abstract method
  • Concrete method
  • Static method
  • Final method

Abstract method

An abstract method declared in the abstract class using abstract keyword. It does not have the method body. You should override the abstract method in the child class. And it is accessible using the child class object at runtime. You can see the example in https://samplecoder.com/abstraction/

Concrete method

The method which has the body its called concrete method. The body of method start from open curly brace “{” and ends by close curly brace “}”. And it is accessible by an object at runtime.

public void sampleConcreteMethod() {
		
   System.out.println("sampleConcreteMethod");
}

Static method

The static method created by using the static keyword. It is accessible by the class name, so the object is not required to access the static method at runtime. If you are using one method frequently in your code, you can make that method as static. This is way of memory management. In java most of the utility methods are static method, because it is accessible by the class name.

package com.samplecoder;

public class Sample{
	public static void print(String message) {
		System.out.println(message);
	}
}

package com.samplecoder;
public class SampleMain{
	public static void main(String...arg) {
		Sample.print("Hi");
		Sample.print("This is static method example program");
	}
}

Final method

The final method created using the final keyword. It cannot overridden in child classes.

public final void sampleFinalMethod() {
		System.out.println("This is final method. You cannot override this method");
	}
Method in Java