Set and get methods, method overloading and constructors in java

In last tutorial, we read about methods and its types. Now we will how to use these methods and what can we do more with them.

Set Method:

In every program, we have variables. Sometimes we give value directly to those variables, sometimes we take value from users (in console), but in mostly cases we have to pass arguments to initialize variables because we take value from user only once and use those values in whole program (large programs contains many classes). Set methods are those methods which takes arguments and set the value of the variables. For convince, we nomenclature such methods with prefix set like set_time, set_date, etc. because it is easy to understand in future what task that method is performing. Note that usually these methods are void type and return nothing.

Get Methods:

In last tutorial, we read about return type methods. Get methods are also return type methods which return private or local variables or some calculated / stored data. For convince, we nomenclature such methods with prefix 'get' like get_time, get_date, etc.

Method Overloading:

We can define methods with same name but different type or number of parameters. This is called method overloading.

  1. package flowkl;
  2. public class Tutorials
  3. {
  4. public void method()
  5. {
  6. System.out.println("first type method");
  7. }
  8. // overloading by changing parameter
  9. public void method(int a)
  10. {
  11. System.out.println("overloaded method, a = "+a);
  12. }
  13. // overloading by changing number of parameter
  14. public int method(int a, int b)
  15. {
  16. return a+b;
  17. }
  18. /*
  19. * invalid method =>
  20. * public int method(int b)
  21. * because method(int a) is already defined
  22. */
  23. public static void main(String args[])
  24. {
  25. //object of sub class
  26. Tutorials obj = new Tutorials();
  27. obj.method();
  28. obj.method(12);
  29. System.out.println(obj.method(12, 21));
  30. }
  31. }


  1. first type method
  2. overloaded method, a = 12
  3. 33


  1. In method overloading, number of parameter or type of parameters must be changed.
  2. We can also change return type in method overloading.
  3. We can not define two methods with same name and parameters (exact copy in term of type, number and sequence) and different return type because when we call any of these methods then computer can not identify which method is called as name, sequence and type of arguments are same. So, it is hard and fast rule that at least one of number, sequence or type of parameter must be changed.


Constructors are special type methods. These methods are also use for initialize class variables. These methods run when we create objects for that class. Constructor has following properties:

  • Name of the constructor is same as name of that class.
  • Construction has no return type.
  • It executes immediately when we create object of that class.
  • Constructor can be override by changing number or type of the parameter.
  • In constructor, we can call only those variables or methods which are already initialized before calling.


  1. package flowkl;
  2. class Test
  3. {
  4. // class variable
  5. private int number = 9;
  6. // constructor with no parameter
  7. public Test()
  8. {
  9. System.out.println("test constructor complete, number = "+number);
  10. }
  11. // constructor overloading
  12. public Test(int a)
  13. {
  14. number = a;
  15. System.out.println("constructor overloading, number = "+number);
  16. }
  17. }
  18. public class Tutorials
  19. {
  20. public static void main(String args[])
  21. {
  22. //object of test class
  23. Test obj = new Test();
  24. Test obj2 = new Test(8);
  25. }
  26. }


  1. test constructor complete, number = 9
  2. constructor overloading, number = 8

Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250