You are on page 1of 2

Examples of predefined java Methods tapos explain

Methods are an important part of object-oriented programming since they isolate functions to
individual objects. The methods within a class can only be called by objects created from the class.
Additionally, methods can only reference data known to the corresponding object. This helps isolate
objects from each other and prevents methods within one object from affecting other objects.

While methods are designed to isolate data, they can still be used to return values to other classes if
necessary. If a value needs to be shared with another class, the return statement can be used.

Method Overloading is a feature that allows a class to have two or more methods having same name, if
their argument lists are different

Method overloading

If two or more method in a class have same name but different parameters, it is known as method
overloading. Overloading always occur in the same class(unlike method overriding).

Method overloading is one of the ways through which java supports polymorphism. Method overloading
can be done by changing number of arguments or by changing the data type of arguments. If two or
more method have same name and same parameter list but differs in return type are not said to be
overloaded method

Method Overloading

The term method overloading refers to the fact that it is perfectly legal to have more than one method
in the same class with the same name, as long as they have different parameter lists. The difference can
be in the number of parameters, or in the types of parameters.

Example:

int process(double num) { } // method 1

int process(char letter) { } // method 2

int process(double num, int position) { } // method 3

Notice that although all three methods above have the same exact name, they each have a different
parameter list. Some of them differ in the number of parameters (2 parameters vs. 1 parameter), and
the first two differ in types (double vs. char). The compiler will distinguish which function to invoke
based on what is actually passed in when the function is called.

x = process(3.45, 12); // invokes the third function above

x = process('f'); // invokes the second function


3

Class: java.lang.Math

• Provides many useful mathematical functions

• static double sin(double a)

• static double sqrt(double a)

• static double pow(double a, double b)

• static int abs(int a)

• static int max(int a, int b)

To use the Scanner class in java.util, write:

• import java.util.Scanner;

You might also like