Core Java - SPLessons
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.....)
[java]public void sum(double d1, double d2)[/java] 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. [java] 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); } } [/java] Where developer did not use return type, directly written the add() as follows. [java]public int add()[/java] Output When compile the code following is the result will be generated. [java]100[/java]

Method with return type

In the following Java Methods example, method with return type with arguments is as follows. [java] 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); } } [/java] Where developer used return type by passing parameters as follows. [java]public void add(int a, int b)[/java] Output When compile the code following is the result will be displayed. [java]3[/java]

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. [java] 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; } } [/java] Output When compile the code following is the output will be generated. [java] Minimum Value = 7 Minimum Value = 8.4 [/java]

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. [java]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(); } [/java] 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: [java]It will provide all tutorials[/java] 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.