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 findalltogether;
  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. }

OUTPUT:

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

NOTE:

  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.

Constructor:

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.

Example:

  1. package findalltogether;
  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. }

OUTPUT:

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

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Java is object oriented and platform independent programming language which runs on JVM. Lets see advantages of java ...
Introduction to Java
In this tutorial, we will learn how to give/ take input/ output to console. What are classes, main() and comments? Wh...
First program in Java
Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, str...
Variables and data types in Java

Login or Sign up to leave comment.