Method and Instance Variables
Method and Instance Variables are described such as Methods are a collection of statements that are group together to operate. Method and Instance Variables are described below:
a.Method
Methods are used in Java to describe the behavior of an object. Methods are a collection of statements that are group together to operate. In Java, it is possible to create methods that have the same name, but different argument lists in various definitions, i.e., method overloading is possible in Java, which is one of the unique features of Object-Oriented Programming (OOP). In this chapter, we will learn about how method overloading is written and how it helps us within a Java program.
A method is the equivalent of a function in object-oriented programming. A noun is to a verb what a variable is to a method — the methods are the actions that perform operations on a variable. A method accepts parameters as arguments, manipulates these, and then produces an output when the method is called on an object. Methods are similar to functions, but methods are also classified according to their purpose in the class design. In classes, variables are called attributes, so methods often operate on attributes.
What is Method Overloading?
If a class of a Java program has a plural number of methods, and all of them have the same name but different parameters (with a change in type or number of arguments), and programmers can use them to perform a similar form of functions, then it is known as method overloading. If programmers what to perform one operation, having the same name, the method increases the readability of the program.
Let us take an example where you can perform multiplication of given numbers, but there can be any number of arguments a user can put to multiply, i.e., from the user point of view the user can multiply two numbers or three numbers or four numbers or so on. If you write the method such as multi(int, int) with two parameters for multiplying two values, multi(int, int, int), with three parameters for multiplying three values and so on. A programmer does method overloading to figure out the program quickly and efficiently. Method Overloading is applied in a program when objects are required to perform similar tasks but different input parameters. Every time an object calls a method, Java matches up to the method name first and then the number and type of parameters to decide what definitions to execute. This process of assigning multiple tasks to the same method is known as Polymorphism. There are different ways to overload a method in Java. They are:
- Based on the number of parameters: In this case, the entire overloading concept depends on the number of parameters that are placed within the parenthesis of the method.
- Based on the data type of the parameter: With the arrangement of data type, within the parameter, overloading of the method can take place.
- Based on the sequence of data types in parameters: The method overloading also depends on the ordering of data types of parameters within the method.
To create overloaded methods, programmers need to develop several different method definitions in the class, all have the same name, but the parameters list is different. Here is a sample code snippet:
b. Instance variables:-
The instance variable in java is used by Objects to store their states. Variables that are defined without the STATIC keyword and are Outside any method declaration are Object-specific and are known as instance variables. They are called so because their values are instance specific and are not shared among instances.
Variables defined inside methods, constructors, or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed. Instance variables are variables within a class but outside any method.
You may also like operator precedence
Leave a Reply