Core Java - SPLessons

Java Methods

Chapter 11

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Methods

Java Methods

shape Description

Java Methods, Methods represent groups of statements to execute the task, Java Methods will have two parts they are method header or method prototype, method body. The method header contains return type, method name, parameters. Following is the syntax and example to represent method header.

returnType methodname(parameters…..)
public void sum(double d1, double d2)

Where public is the modifier and scope of this modifier is global, void is the return type, sum is the method name, double d1 and double d2 are the parameters.

Types of Methods

shape Description

Instance Methods:Instance methods will act upon instance variables of a class, these are two types as follows.

Static Method:If one declare static keyword before method name then the method is called static method and it will act upon static variables of a class and static methods will be called by classname.methodname();
Factory Method:While using factory method no need to use new operator to create an object.

Method Calling

shape Description

While using a method the method needs to be called, in Java two ways have specified by using method return a value and no returning value.

Method without return type

In the following Java Methods example, method without return type without arguments is as follows.

public class method {
	
		 
		public int add(){
		int a=50;
		int b=50;
		int c=a+b;
		return c;
		}

		public static void main(String args[]) // ->method prototype.
		{ 
		 
		method obj= new method();
		int splesson=obj.add(); 
		System.out.println(splesson);
		 
		 }
		}

Where developer did not use return type, directly written the add() as follows.

public int add()

Output
When compile the code following is the result will be generated.

100

Method with return type

In the following Java Methods example, method with return type with arguments is as follows.

public class method {
	
		 
		public void add(int a, int b){
		
		int c=a+b;
		System.out.println(c);
		}

		public static void main(String args[]) // ->method prototype.
		{ 
		 
		method obj= new method();
		obj.add(1,2); 
		
		 
		 }
		}

Where developer used return type by passing parameters as follows.

public void add(int a, int b)

Output
When compile the code following is the result will be displayed.

3

Method Overloading And Overriding

shape Description

Method overloading is a concept of polymorphism, where poly means many operations will be done by single function, for example calculator. Polymorphism will have two types as follows.

Method Overloading

Same method with different number of parameters in the same class then it is called as Method Overloading. Following is an example.

public class methodoverloading {
	public static void main(String[] args) {
	      int a = 14;
	      int b = 7;
	      double c = 9.3;
	      double d = 8.4;
	      int result1 = minFunction(a, b);
	      // same function name with different parameters
	      double result2 = minFunction(c, d);
	      System.out.println("Minimum Value = " + result1);
	      System.out.println("Minimum Value = " + result2);
	   }

	  // for integer
	   public static int minFunction(int n1, int n2) {
	      int min;
	      if (n1 > n2)
	         min = n2;
	      else
	         min = n1;

	      return min; 
	   }
	   // for double
	   public static double minFunction(double n1, double n2) {
	     double min;
	      if (n1 > n2)
	         min = n2;
	      else
	         min = n1;

	      return min; 
	   }
}

Output
When compile the code following is the output will be generated.

Minimum Value = 7
Minimum Value = 8.4

Method Overriding

If subclass has the same method as declared in the base class, it is known as Method Overriding in java. The following is an example.

class Splessons{  
void run(){System.out.println("Welcome to splessons");}  
}  
class Tutorials extends Splessons{  
void run(){System.out.println("It will provide all tutorials");}  
  
public static void main(String args[]){  
Tutorials obj = new Tutorials ();  
obj.run();  
}  

In the above example, run() method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes and as per the definition method overriding placed.
Output:

It will provide all tutorials

Note:
Overloading happens at compile-time while Overriding happens at runtime.The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.

Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.


Summary

shape Key Points

  • In Java four ways developer can create objects.
  • While using Factory method no need to use new operator to create an object.