Posts

13 Java Fundamentals | Overloaded Methods in Java | By Dummy for Dummies

Image
  INTRODUCTION It is not necessary that different methods must have different names. Methods are actually recognized by signature rather than just name. Signature of method includes name of method and it's parameters in which they recieve arguments. So you can have methods by same names yet they will receive different arguments and perform different functions. METHOD OVERLOADING Imagine the boss monkey sometime needs to add 2 numbers, while sometime it needs to add 3 numbers but the different number of arguments passed cannot be recieved by the same method. In that case you can create another method by same name and set it to recieve the desired numbers of arguments. As I said methods are recognized by signature rather than just names, so even though two methods have same name, if they receive different numbers or different types (datatype) of arguments they are considered different and java treat them as completely different methods. Here is a demonstration: public class Main { ...

12 Java Fundamentals | Methods in Java | By Dummy for Dummies

Image
  INTRODUCTION What if I tell you, you can have an assistant in your program that will do a specific task assigned to it everytime you ask. Everytime you call the assistant, give it the data, it will do the assigned task. All you have to do is just code it for once, and then call it unlimited times to do your tasks. METHODS  In Java, a method is a block of code enclosed in curly brackets. There is a main method always, and you can create multiple other methods if you want. Think of these like you have one boss method and many assistants. Boss Method   → Always execute, code execution starts from here. Assistant Methods   → You can chose to execute them multiple times, or not even once depends on you, executes when you call them. Let's simulate a situation where boss monkey will ask to see 2 bananas plus 3 bananas how much total bananas? To make assistant do the calculations we need to send it the data. In programming that data we send are called Arguments . As we kno...