In Java it is possible to define two or more methods within the same class that share
the same name, as long as their parameter declarations are different. When this is
the case, the methods are said to be overloaded, and the process is referred to as
method overloading. Method overloading is one of the ways that Java implements
polymorphism.
When an overloaded method is invoked, Java uses the type and/or number of
arguments as its guide to determine which version of the overloaded method to
actually call. Thus, overloaded methods must differ in the type and/or number of
their parameters. While overloaded methods may have different return types, the
return type alone is insufficient to distinguish two versions of a method. When Java
encounters a call to an overloaded method, it simply executes the version of the
method whose parameters match the arguments used in the call.

Read more


// This program uses a parameterized method.

class Box {
            double width;
           double height;
           double depth;
// compute and return volume
            double volume() {
                                     return width * height * depth;
                                      }
// sets dimensions of box
           void setDim(double w, double h, double d) {
                                     width = w;
                                     height = h;
                                    depth = d;
                                    }
}
class BoxDemo5 {
                  public static void main(String args[]) {
                  Box mybox1 = new Box();
                  Box mybox2 = new Box();
                  double vol;
// initialize each box
                  mybox1.setDim(10, 20, 15);
                  mybox2.setDim(3, 6, 9);
// get volume of first box
                 vol = mybox1.volume();
                 System.out.println("Volume is " + vol);
// get volume of second box
                  vol = mybox2.volume();
                  System.out.println("Volume is " + vol);
                  }
}

Read more


Classes usually consist of two things:
instance variables and methods.This is the general form of a method:

type name(parameter-list) {
// body of method
}

Here, type specifies the type of data returned by the method. This can be any valid type,
including class types that you create. If the method does not return a value, its return
type must be void. The name of the method is specified by name. This can be any legal
identifier other than those already used by other items within the current scope. The
parameter-list is a sequence of type and identifier pairs separated by commas. Parameters
are essentially variables that receive the value of the arguments passed to the method when
it is called. If the method has no parameters, then the parameter list will be empty.
Methods that have a return type other than void return a value to the calling routine
using the following form of the return statement:

return value;

Here, value is the value returned.

Read more


When you create a class, you are creating a new data type. You can
use this type to declare objects of that type. However, obtaining objects of a class is a
two-step process. First, you must declare a variable of the class type. This variable does
not define an object. Instead, it is simply a variable that can refer to an object. Second,
you must acquire an actual, physical copy of the object and assign it to that variable. You
can do this using the new operator. The new operator dynamically allocates (that
is, allocates at run time) memory for an object and returns a reference to it. This
reference is, more or less, the address in memory of the object allocated by new.It has
this general form:

Read more


The most important thing to understand about a class is that it defines a
new data type. Once defined, this new type can be used to create objects of that type.
Thus, a class is a template for an object, and an object is an instance of a class.A class is declared by use of the class keyword.

Read more

Powered by Blogger.