method overloading – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:06:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg method overloading – Programmerbay https://programmerbay.com 32 32 What is Method Overloading and Overriding? https://programmerbay.com/what-is-method-overloading-overriding/ https://programmerbay.com/what-is-method-overloading-overriding/#respond Sat, 14 Sep 2019 17:38:16 +0000 https://programmerbay.com/?p=5122 Method Overloading

Defining methods with the same name within the same class, but have a different number of parameters or same parameter with different datatypes are known as method overloading

Java uses the following aspects in order to call the correct method:-
1) Number of Arguments
2) Same Argument with different datatypes

However, the return type of a method isn’t sufficient to distinguish it from other versions. As a result, method overloading cannot be implemented using only the return type.

Program to demonstrate the working of method overloading 

class showoverload{

void display(int x)
{
System.out.println("Hey I am display method with a single argument with integer type num = "+x);

}
void display(int x,int y)
{
System.out.println("Hey I am display method with a two argument of integer types num = "+x+", "+y);

}
void display(char c)
{
System.out.println("Hey I am display method with two argument of character type = "+c);

}
void display()
{
System.out.println("Hey I am display method with a no argument");

}

}
public class Overloading {

public static void main(String[] args) {
showoverload obj = new showoverload();
obj.display(10);
obj.display();
obj.display('S');
obj.display(10,20);
}

}

Output:

method overloading in java

Method Overriding

When there is a method in subclass having the same name and type signature similar to its parent class with a specific implementation, it is said to be method overriding. Further, when an overridden method is called from the subclass, then Java always refers to subclass’s method, instead of its parent class’s method

Program to demonstrate the working of method overriding

class a{
void display()
{
System.out.println("Hi I m parent class");

}
}

class b extends a{
@Override
void display()
{
System.out.println("Subclass is being called");
}
}
public class overrideMe{

public static void main(String[] args) {
b obj = new b();
obj.display();
}

}

Output:

overriding

]]>
https://programmerbay.com/what-is-method-overloading-overriding/feed/ 0
Difference between method overloading and method overriding in Java https://programmerbay.com/difference-between-method-overloading-and-method-overriding-in-java/ https://programmerbay.com/difference-between-method-overloading-and-method-overriding-in-java/#respond Sat, 29 Jun 2019 10:30:30 +0000 https://www.programmerbay.com/?p=4107 Method:  It is a set of statements that collectively perform a specific task and can be invoked any number of times by using its name. In other words, these lines of code are grouped together to form a method and this defined method can be used any number of time. It reduces the length of the code and improves readability.

Polymorphism: It refers to ability to take multiple forms. In Java, method overloading and overriding are the ways to implement polymorphism.

method overloading and method overriding 1

Method Overloading

Overloading is a feature that allows a class to have multiple methods having same name. In order to achieve this, one can change number of method arguments or argument datatypes.

Syntax:

class A {
void show () {
// body
}

// overloading 

void show (int x) {

// body
}
...

}

 

Method Overriding

Overriding is a feature that allows a subclass to have a method that is already defined in parent class, with same name and signature, but different implementation.

Syntax:

class A {
void show () {
// body
}
}

class B extends A {

// overriding

@override
void show () {

// body
}
...

}

 

Difference between method overloading and overriding in Java in tabular form

[wpsm_comparison_table id=”22″ class=”center-table-align”]

 

Java Overriding Example:

public class PUBG {

    void demageAWM (){

        System.out.println("1 shot damage 100 % ");
    }

}

class PUBGUpdatedVersion extends PUBG {
// overriding demageAWM()
    @Override
    void demageAWM() {
        System.out.println("1 shot damage 95%");
    }
}

class test {

    public static void main(String[] args) {
        PUBGUpdatedVersion gun = new PUBGUpdatedVersion();
        gun.demageAWM();
    }
}

Output:

1 shot damage 95%

Java Overloading Example

public class PUBG {
// overloading attachments()
    void attachments (String scope){

        System.out.println("This is pistal with "+scope);
    }

    void attachments (String scope, String mag,String extender){

        System.out.println("This is Auto Gun with "+scope+","+mag+", "+extender);
    }

}


class test {

    public static void main(String[] args) {
        PUBG gun = new PUBG();
        gun.attachments("Red dot");
        gun.attachments("6x","AWM mag","AWM extender");
    }
}

Output:

This is pistal with Red dot
This is Auto Gun with 6x,AWM mag, AWM extender

]]>
https://programmerbay.com/difference-between-method-overloading-and-method-overriding-in-java/feed/ 0