defination – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Thu, 18 Jun 2020 14:37:57 +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 defination – Programmerbay https://programmerbay.com 32 32 What are Methods in Java https://programmerbay.com/what-are-methods-in-java/ https://programmerbay.com/what-are-methods-in-java/#respond Sat, 14 Sep 2019 17:38:36 +0000 https://programmerbay.com/?p=5123 A method 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.

Syntax:

return_type method (parameter/ Argument)
{
// set of statements or body
}

Explanation:

return_type: It means what type of value would a method be returned.  It can be Boolean, integer, floating, char and even objects. If the method name is followed by void, then it would return nothing.

method_name: It is basically an identifier which represents the name of the method.

parameter or argument: These are nothing but a set of inputs where each input is separated by a comma and defined with a datatype preceded with the identifier.

Java program to define a method

class square{
int area;
int area(int side){
area = side*side;
return area;
}
void display(int area)
{
System.out.println(" Area of the Square = "+area);
}
}
public class Box {

public static void main(String[] args) {
square obj = new square();
int output;
output= obj.area(5);
obj.display(output);
}

}

Output:

output

]]>
https://programmerbay.com/what-are-methods-in-java/feed/ 0
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