A class can have multiple method with same name, but they are differ only in total number of arguments or their data types or both. This is called method overloading.
To create an overloaded method, we have to write different methods with the same name but with different parameter list. The difference may be total number of parameters or their data types. The return data type of a method does not play any role in method overloading. Continue reading
“Constructor is a special method, which is called automatically when object is created. It has the same name as its class.”
ClassName( ) // This is a constructor.
The instance variables and methods can access inside a class directly. But it can not access the outside of a class directly.
The dot operator is used to access the instance variables and methods outside of a class. The general form of accessing class members are:
objectname . variablename
objectname . methodname(parameter-list) ;
Objects in java are created using the new operator. The new operator creates an object of the class and returns a reference to that object.
s1 = new sum( ) ;
The first statement (sum s1;) only declares a variable(s1) of a class(sum). A declaring a variable does not create an object.
The second statement (s1 = new sum();) actually assigns the object reference to the variable(s1). Now, the variable s1 is an object of the sum class.
A class with only data fields has no life. It is required to add methods that manipulating the data contained in the class.
Methods are declared inside the body of the class after declaration of instance variables. The general form of a method declaration is
returntype methodname (parameter-list)
//Body of a method