JAVA: Overloading & Overriding Methods

In java, it’s possible to define two or more methods with same name but different parameter list in the same class, the method is said to be overloaded and the process is known as method overloading. When an overloaded method is invoked, Java uses type and/ or number of arguments to decide which version of the overloaded method to actually call. Overloaded methods may have different return type but the return type alone is insufficient to distinguish two versions of a method. Thus, overloaded method must vary in type and/ or the number of arguments.

When an overloaded method is called, Java will try to match between the arguments used to call the method and the method’s parameters. If this match is not perfect then Java’s automatic type conversion will play a role in solving the problem and calling the correct overloaded method. The following program illustrates the use of overloaded method:

class OverloadDemo
{
    void test()
    {
        System.out.println("No methods are overloaded");
    }
    void test(int num, int num1)
    {
        System.out.println("The value in num and num1 are: " + num + " " +num1);
    }
    void test(double count)
    {
        System.out.println("Inside the test(double) method: " +count);
    }
    public static void main(String args[])
    {
        OverloadDemo odObj = new OverloadDemo();
        odObj.test();
        odObj.test(10,20);
        odObj.test(25);
        odObj.test(25.5);
    }
}

The output of the program is:

Method overloading supports polymorphism because its one way that java implements “one interface, multiple methods” paradigm. Constructors can also be overloaded.

In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from the subclass it will always call the version which is present in the subclass, and the method in the superclass will be hidden.

Overloaded methods are a form of static polymorphism, and overridden methods are a form of dynamic polymorphism.

The following program shows the use of overriding methods:

class Parent
{
    int num, num1;
    Parent(int num, int num1)
    {
        this.num = num;
        this.num = num1;
    }
    void show()
    {
        System.out.println("The values in num and num1 are: " +num+" " +num1);
    }
}

class Child extends Parent  //inherited class
{
    int count;
    Child(int num, int num1, int num2)
    {
        super(num, num1);
        count = num2;
    }
    void show()
    {
        System.out.println("The values in count is: " +count);
    }
}

class OverrideDemo
{
    public static void main(String args[])
    {
        Child sObj = new Child(5, 10, 15);
        sObj.show();
    }
}

The program will generate the following output:

Access Specifires for Methods

Encapsulation links data with code that manipulates it. It also provides access control. Through encapsulation one can control which parts of program can access the members of a class. The access specifier determines how a member can be accessed. Some aspects of access control are related mostly to inheritance or packages.

Java provides the following access specifiers:

public

When a member of the class has public access specifier, then that member can be accessed by any other code in the program as well as it can be viewed from any package or any class.

private

When a member of the class has the specifier, private, then other members of its class can only access that member.

protected

When a member of the class has the specifier, protected, then that member can be accessed by any class that extends the current class in the same package or in any other package.