📑

Difference between Java Constructors and Methods

2022/10/19に公開

Java is the programming language which helps us to create applications in real time and we can also say that Java is a completely object oriented programming language and a high- level language. So we can say Java supports object oriented programming concepts and follows them.

Java supports and uses classes which consist of both java constructors and java methods, all the data, statements and variables which are used in java will be present in class.

Scope of the Article:

  • In this article, we will learn about why java constructors and java methods are used in java.
  • We will know about the java constructors and java methods with examples for better understanding.
  • Then we will see the difference between java constructors and methods.

Introduction:

In Java, classes are used as the main part because classes are the building blocks of java, and we can say that class is an important component for object oriented programming language such as java which determines the characteristics of items. These building blocks in object oriented programming are called classes objects.

As we all know class is the template of an object and object is an instance of class. Classes are important in java for creating templates for an object and to support inheritance.

Java Constructors?

Java Constructors are used as a class of the java program, which is responsible for initializing objects and will not produce any return types. The name of the constructor must have the same name as the class.

Constructors are called special types of methods in java used for initializing objects. In constructors we include the blocks of codes which are similar to methods in java.

Syntax for the constructor:

class demo
{
	//Writing a Constructor 
	new demo()
	{
		
	}
}

After creation of a constructor we have to create an object, which calls the constructor.

demo d = new demo();

We can also overload or override the constructors in java,

Overloading is that which occurs between the methods in the same class and has the same name and same methods , Overriding is that which occurs between super class and subclass which has the same name and with different parameters.

→ We also have some other concepts in Java about the constructors such as Copy Constructor, Constructor Chaining.

Constructors in java are of different types,

  • No- argument constructor
  • Parameterized constructor

1. No- argument constructor

  • No- Argument Constructors are those which are used in java which may have parameters or which may not have arguments.
  • No- Argument Constructor can also be written as No- Arg Constructors and these constructors are also called Default Constructors.
  • Let us know more about the example,

Example Code:

import java.util.*;
import java.io.*;
class demo {
	int a;
	demo()
	{
		System.out.println(“Constructor is called here which is default Constructor”);
	}
}
class mainpro {
	public static void main(String[] args)
	{
		demo d = new demo();
		System.out.println(d.a);
	}
}

Output:

Constructor is called here which is default Constructor
0

2. Parameterized constructor

→ Parameterized constructors are those which have parameters for the constructor creating.
→ When we want to initialize fields of the class it is suggested to use parameterized constructors.
→ Let us check with the example,

import java.util.*;
import java.io.*;
class demo {
	int a;
	demo(int a)
	{
		System.out.println(“Constructor is called here which is Parameterized Constructor”);
this.a =a;
	}
}
class mainpro {
	public static void main(String[] args)
	{
		demo d = new demo(5);
		System.out.println(“Number: ”+d.a);
	}
}

Output:
Constructor is called here which is Parameterized Constructor
Number: 5

Java Methods?

Java methods are the collection of statements which performs particular or specific tasks without returning anything.

  • Methods in Java also allow us to reuse the code without typing it again and again.
  • Methods are sometimes used as functions which behave as an object.
  • We include the Java code in the methods and perform that particular task.
  • There various types of methods such as static methods, instance methods, etc.
    Syntax for declaring a method in java:
<access modifier> <any return type> <method name>(list of parameters)
{
	//statements to execute
}

→ Access modifiers include default, public, private and protected.
→ Return type can be the data type of method to be declared.
→ method name, here we have to specify our method name to be used.
→ list of parameters including the inputs which we want to give, are given as the parameters enclosed with parentheses, if no parameters are used we can use empty parentheses method().

Example code:

public int min(int x, int y)
{
	if(x>y)
		return y;
	else
		return x;
}
→ Methods in Java also have different types such as,
1. Predefined methods
2. User- Defined methods

1. Predefined methods

→ Predefined methods, as the name itself indicates predefined, these methods are already defined by the java class and Java libraries.
→ we can also call predefined methods as built- in methods or standard methods.
Example code:

public class premethod 
{
public class void main(String[] args)
{
System.out.println("Minimum of given numbers "+Math.min(5,2));
}

}

Output:
Minimum of given numbers 2

2. User- defined methods

→ User- defined methods are those methods which are created by users according to their requirements.
→ As these methods defined by users, will have scope to change the method according to our requirement.
Some advantages of methods in java are Code Reusability and Code Optimization.

Example code:

public int min(int x, int y)
{
	if(x>y)
		return y;
	else
		return x;
}

Now, let us discuss the difference between Java Constructors and Java Methods.

Difference between Java Constructors and Java Methods:

Java Constructors Java Methods
Java Constructors are those which has a block of code which initializes the new objects. Java Methods are those which have the block of code which can be directly executed.
Java Constructors does have any return values. Java methods have the return value.
Java Constructors cannot inherit by sub classes. Java Methods can be inherited by sub classes.
Java Constructors uses newly created objects for execution. Java Methods does not require any object creation for execution; it can directly execute and return values.
A class can have more than one constructor but the number of parameters should not be the same. A class can have more than one method but the number of parameters should not be the same.
The name of Java Constructor should be the same as the name of the class. The name of the Java Method might not be the same as the class name.
Java Constructors can be invoked implicitly by system. Java Methods are invoked by users or programmers.
Java Constructors are invoked when an object is created using a new keyboard. Java methods are invoked through the method calls.
Java Constructors creates an object which does not exist. Java methods use objects which are already existing.
Java constructors are of 2 types such as default constructors and parameterized constructors. Java methods are of 2 types such as predefined methods and user defined methods.

These are the differences between the Java constructors and Java methods, which gives us brief idea about both of them.

Conclusion

  1. Firstly, We had known why java constructors and java methods are useful.
  2. Then we saw what java constructors and java methods individually with the help of examples codes in java.
  3. Java Constructors are used as a class of the java program, which is responsible for initializing objects and will not produce any return types
  4. Java methods are the collection of statements which performs particular or specific tasks without returning anything.
  5. Then, we saw the differences between both Java Constructors and Java Methods.
  6. Some differences between both Java Constructors and Java Methods are,
  • Return values exist in java methods and don't exist in java constructors.
  • Objects can be created for execution of constructors whereas java methods doesn't require any creation of objects, it executes directly
  1. These are the main things which we focussed on in this article.

Discussion