Methods in Java

Syntax for Method:

AccessSpecifier AccessModifier ReturnType methodName(DataType var1, DataType var2,... ) throws  Exception {
        //Body of the method
}

Access Specifiers:
  1. default: accessible to the package
  2. public: accessible to all
  3. protected: accessible to package and subclasses
  4. private: Accessible to class only
Access Specifiers: 
  • static: creating class level methods and class level variables
  • abstract: creating abstract classes and methods
  • final class: cannot inheritable
  • final method: cannot be overridden
  • final variable: After initialize cannot change the value
  • synchronized and volatile: used in Multi-Threading
Return Types: 
  • boolean
  • byte, short, int and long
  • char and String
  • float and double
  • void
e.g.
public static int add(int num1, int num2){
        int sum = num1 + num2;
        return sum;
}

Method Overloading:

Method Overloading is a feature that allows a class to have two or more methods having same name but argument lists are different.

Rules of Method Overloading:
  1. No of  arguments is different
  2. Data type of the arguments is different
  3. Order of the arguments is different
Example 1: Create overloaded methods to find addition of 2 or 3 numbers various data types?
package com.java.basics;

public class MethodOverloading {

    public static void main(String[] args) {
        add();
        add(10, 20);
        add(100l, 200l);
        add(10, 20, 30);
        add(12.5f, 12.5f);
        add(25.5, 24.5);
        add(2.5f, 7.5);
        add(14.5, 5.5f);

    }

    public static void add(){
        System.out.println("Add method with no arguments");
    }
    
    public static int add(byte a, byte b){
        System.out.println("Add method with 2 arguments of type byte");
        return a + b;
    }
    
    public static int add(short a, short b){
        System.out.println("Add method with 2 arguments of short short");
        return a + b;
    }
    
    public static int add(int a, int b){
        System.out.println("Add method with 2 arguments of type int");
        return a + b;
    }
    
    public static float add(float a, float b){
        System.out.println("Add method with 2 arguments of type float");
        return a + b;
    }
    
    public static double add(double a, double b){
        System.out.println("Add method with 2 arguments of type double");
        return a + b;
    }
    
    public static double add(double a, float b){
        System.out.println("Add method with 2 arguments of type double and float");
        return a + b;
    }
    
    public static double add(float a, double b){
        System.out.println("Add method with 2 arguments of type float and double");
        return a + b;
    }
    
    public static int add(int a, int b,int c){
        System.out.println("Add method with 3 arguments of type int");
        return a + b + c;
    }
}

Output:
Add method with no arguments
Add method with 2 arguments of type byte
Add method with 2 arguments of type float
Add method with 3 arguments of type int
Add method with 2 arguments of type float
Add method with 2 arguments of type double
Add method with 2 arguments of type float and double
Add method with 2 arguments of type double and float

Example 2: Create overloaded methods to find area of Geometrical figure with different signature?
package com.java.basics;

class AreaOfGeometricFigure{

    void area(){
        System.out.println("Area");
    }
    
        
    int area(int s){
        return s * s;
    }    
    
    float area(float s){
        return s * s;
    }
    
    double area(double s){
        return s * s;
    }
    
    void area(int b, int h){
        System.out.println("Area of Triangle is " + (b * h)/2);
    }
    
    void area(int b, float h){
        System.out.println("Area of Triangle is " + (b * h)/2);
    }
    
    void area(float b, int h){
        System.out.println("Area of Triangle is " + (b * h)/2);
    }
    
    void area(float b, float h){
        System.out.println("Area of Triangle is " + (b * h)/2);
    }
    
    double area(int b, double h){
        return (b * h)/2;
    }
    
    double area(double b, int h){
        return (b * h)/2;
    }
    
    double area(double b, double h){
        return (b * h)/2;
    }
    
    void area(float b, double h){
        System.out.println("Area of Rectangle is " +  (b * h));
    }
    
    void area(double b, float h){
        System.out.println("Area of Rectangle is " +  (b * h));
    }
    
    void area(int l, int b, int h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(float l, float b, float h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(double l, double b, double h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    void area(int l, float b, int h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(int l, int b, float h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(float l, int b, int h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(double l, int b, int h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(int l, double b, int h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(int l, int b, double h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(int l, float b, float h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(float l, int b, float h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    void area(float l, float b, int h){
        System.out.println("Area of Cube is " +  2*(l*b + b*h + h*l));
    }
    
    
}

public class OverLoading{
    public static void main(String[] args){
        AreaOfGeometricFigure obj = new AreaOfGeometricFigure();
        
        obj.area();
        System.out.println("Area of Square is: " + obj.area(3));
        System.out.println("Area of Square is: " + obj.area(5.0));
        System.out.println("Area of Square is: " + obj.area(6.0F));
        obj.area(3,5);
        obj.area(1,2,3);
        obj.area(1.0F,2.0F,3.0F);
        obj.area(1.0,2.0,3.0);
        obj.area(1.0,2,3);        
    }
}

Output:
Area
Area of Square is: 9
Area of Square is: 25.0
Area of Square is: 36.0
Area of Triangle is 7
Area of Cube is 22
Area of Cube is 22.0
Area of Cube is 22.0
Area of Cube is 22.0

No comments:

Post a Comment