Methods in Java

 Methods in Java 

In Java, the Method Handling refers to the mechanism of passing arguments to a method and getting the return value from a method. Methods are re-usable blocks of code that can be called multiple times within a program, which helps to keep the code organized and eliminates the need for repetitive code.

Method handling in Java is done through the following steps:

Declaration/Method Header: Methods are declared with a specific return type, name, and parameter list. For example:

public static int product(int a, int b) {

    return a * b;

}

Invocation/calling a method: The methods can be invoked or called from anywhere within the program using the method name and passing the required arguments. For example:

int result = product(10, 20);

Argument Passing: When a method is invoked, the arguments passed to the method are stored in the parameter list of the method. The arguments must match the data type of the parameters in the method declaration.

Return Value: A method can return a value of the specified data type, which can be used by the calling method. The return keyword is used to return the value.

Java supports four types of method handling, they are:

Call by value: In this method, the original value is passed to the method and any changes made to the argument within the method do not reflect in the calling method.

Call by reference: In this method, a reference to the original object is passed to the method and any changes made to the object within the method reflect in the calling method.

Method Overloading: Java supports method overloading, which means that a class can have multiple methods with the same name, but with different parameters.

Method Overriding: Java also supports method overriding, which means that a subclass can provide a new implementation for a method defined in its superclass.

Overall, method handling in Java is an essential part of the Java programming language, as it helps to keep the code organized, eliminates the need for repetitive code, and supports reusability of code blocks.


Call By Value and Call By Reference in java with example

In Java, method arguments can be passed in two ways: call by value and call by reference.

Call by value:

In this method, the original value is passed to the method and any changes made to the argument within the method do not reflect in the calling method. When we pass a primitive data type (e.g. int, float, double, etc.), the value of the variable is passed to the method.

Here's an example to demonstrate the concept of call by value in Java:

public class Test {

    public static void changeValue(int x) {

        x = x + 10;

    }

    public static void main(String[] args) {

        int num = 10;

        changeValue(num);

        System.out.println("Value after method call: " + num);

    }

}

In the above example, the value of num is passed to the method changeValue, which increments the value of x by 10. But after the method call, the value of num remains unchanged, as the changes made to x within the method do not reflect in the calling method.


Call by reference:

In this method, a reference to the original object is passed to the method and any changes made to the object within the method reflect in the calling method. When we pass an object of a class, the reference to the object is passed to the method.

Here's an example to demonstrate the concept of call by reference in Java:

public class Test {

    public static void changeValue(Sample obj) {

        obj.num = obj.num + 10;

    }

    public static void main(String[] args) {

        Sample s = new Sample();

        s.num = 10;

        changeValue(s);

        System.out.println("Value after method call: " + s.num);

    }

}

class Sample {

    int num;

}

In the above example, the object s of class Sample is passed to the method changeValue, which increments the value of num by 10. After the method call, the value of s.num is changed, as the changes made to the object obj within the method reflect in the calling method.


Summary: In Java, when we pass a primitive data type, the value of the variable is passed to the method, which is known as call by value, and when we pass an object of a class, the reference to the object is passed to the method, which is known as call by reference.

Method Overload and Method Overriding in java with example

Method Overloading in Java:

Method Overloading in Java refers to the ability of a class to have two or more methods with the same name, but different parameters. This allows a class to have multiple methods with the same name, but with different arguments, thus providing a convenient way of reusing the same name for similar, but different, methods.

Here's an example of method overloading in Java:

class Adder {

    static int add(int a, int b) {

        return a + b;

    }

static double add(double a, double b) {

        return a + b;

    }

}

public class Main {

    public static void main(String[] args) {

        System.out.println(Adder.add(1, 2));

        System.out.println(Adder.add(1.0, 2.0));

    }

}

Method Overriding in Java:

Method Overriding in Java refers to the ability of a subclass to provide a specific implementation for a method that is already provided by its parent class. The purpose of method overriding is to change the behavior of a method inherited from the parent class in a subclass.

Here's an example of method overriding in Java:

class Shape {

    void draw() {

        System.out.println("Drawing Shape");

    }

}

class Circle extends Shape {

    void draw() {

        System.out.println("Drawing Circle");

    }

}

class Rectangle extends Shape {

    void draw() {

        System.out.println("Drawing Rectangle");

    }

}

public class Main {

    public static void main(String[] args) {

        Shape s = new Shape();

        s.draw();

        Circle c = new Circle();

        c.draw();

        Rectangle r = new Rectangle();

        r.draw();

    }

}


Comments