Java. Overloading of methods in classes. Overloading of constructors




Java. Overloading of methods in classes. Overloading of constructors


Contents


Search other websites:

1. What does the overloading of methods mean in the Java programming language? Example

The term “overload” means the use of the same method name several times when it is declared in the class. From the point of view of the syntax of a language, there can not be two identical names in a certain local space. However, it is allowed to declare methods (functions) with the same name but different parameters. If an overloaded method is called, then from several declared methods, the compiler automatically determines the desired method by its parameters, which are specified at the call.

Overloading of methods is an effective mechanism for implementing polymorphism in classes.

In general, the declaration of a class containing an overloaded method might look like this:

class CMyClass
{
    // ...

    OverloadedMethod(parameters1)
    {
        // ...
    }

    OverloadedMethod(parameters2)
    {
        // ...
    }

    // ...

    OverloadedMethod(parametersN)
    {
        // ...
    }
}

where

  • CMyClass – class name;
  • OverloadedMethod1() – the name of overloaded method;
  • parameters1, parameters2, …, parametersN – Parameters (parameter list) of any of the overloaded methods. Parameters (parameter list) of any of the overloaded methods. These parameters must be different each other types or quantities.

2. Example of a class containing overloaded methods

In the example, the Complex class is declared that implements a complex number consisting of the real and imaginary parts. The class implements:

  • internal variables of real type with names a, b. These variables correspond to the real and imaginary parts of the number: z = a + bi;
  • constructor of class Complex() without parameters;
  • access methods GetA(), GetB(), SetComplex();
  • the overloaded method Mult(), which implements various variants of multiplying the complex number implemented in the class.

The text of class declaration

public class Complex
{
    // hidden data of class
    private double a; // real part
    private double b; // imaginary part

    // class constructor
    Complex()
    {
        a = 1;
        b = 1;
    }

    // access methods
    double GetA() { return a; }
    double GetB() { return b; }

    void SetComplex(double a, double b)
    {
        this.a = a;
        this.b = b;
    }

    // Overloaded method Mult()
    // without parameters - squaring
    Complex Mult()
    {
        Complex c = new Complex();
        double A, B;

        A = a*a - b*b;
        B = a*b + b*a;
        c.SetComplex(A, B);

        return c;
    }

    // method Mult() with 1 parameter - multiplication of a complex number by an ordinary number
    Complex Mult(double d)
    {
        Complex c = new Complex();
        double A, B;

        A = a*d;
        B = b*d;
        c.SetComplex(A, B);

        return c;
    }

    // method Mult() with 2 parameters - multiplication of complex numbers
    Complex Mult(double a2, double b2)
    {
        Complex c = new Complex();
        double A, B;

        A = a*a2 - b*b2;
        B = a*b2 + b*a2;
        c.SetComplex(A, B);

        return c;
    }
}

Using the class in another method

...

Complex c1 = new Complex();
double A, B;

c1.SetComplex(2.0, 3.0);

// use of the overloaded Mult() method
Complex c2 = c1.Mult(); // squaring

A = c2.GetA(); // A = -5.0
B = c2.GetB(); // B = 12.0

c2 = c1.Mult(4); // multiplication by a number
A = c2.GetA(); // A = 8.0
B = c2.GetB(); // B = 12.0

c2 = c1.Mult(1, 3); // multiplication by a complex number
A = c2.GetA(); // A = -7.0
B = c2.GetB(); // B = 9.0

...

3. On what grounds the parameters of overloaded methods differ from each other?

If the method name is overloaded in the class, the parameters of this method may differ:

  • number of parameters;
  • types of parameters that the method receives.



4. How many methods can be overloaded in a class?

Any number of methods can be overloaded in a class.

5. How many overloaded implementations can a method have in the class?

In a class, an overloaded method can have any number of implementations. The main thing is that the parameters list of each overloaded method were unique.

6. How is implemented the constructor overloading in the class?

Overloading of constructors is implemented in the same way as overloading of conventional methods. Overloading the constructors allows you to set various ways to initialize the objects of the class.

Below is the class Circle that implements the circle. The class defines the following variables and methods:

  • hidden (private) variables x, y, r, defining the coordinates of the center of the circle and its radius;
  • four overloaded constructors;
  • methods for accessing to the external variables of class GetCircle(), GetX(), GetY(), GetR(), SetXYR();
  • the GetSquare() method that returns the area of a circle.

The program code of the class is the following

public class Circle 
{
    // class realizing a circle on the coordinate plane
    private int x, y; // the coordinates of the circle center
    private int r; // radius of circle

    // overloaded constructors
    // the constructor without parameters
    Circle()
    {
        x = y = 0;
        r = 1;
    }

    // constructor with 1 parameter
    Circle(int r)
    {
        this.r = r;
    }

    // constructor with 2 parameters
    Circle(int x, int y)
    {
        this.x = x;
        this.y = y;
        r = 1;
    }

    // constructor with 3 parameters
    Circle(int x, int y, int r)
    {
        this.x = x;
        this.y = y;
        this.r = r;
    }

    // access methods
    // returns a Circle object
    Circle GetCircle()
    {
        // create a new object of type Circle, be sure to allocate memory for it
        Circle c = new Circle(x, y, r);
        return c;    // return the object
    }

    // return the coordinates and the radius
    int GetX() { return x; }
    int GetY() { return y; }
    int GetR() { return r; }

    // set a new values of x, y, r
    int SetXYR(int nx, int ny, int nr)
    {
        x = nx;
        y = ny;
        r = nr;
    }

    // a method that calculates the area of a circle
    double GetSquare()
    {
        return (double)(3.1415*r*r);
    }
}

Using a class in another program code

// use of the Circle class in another program code
Circle c0 = new Circle(); // call the constructor without parameters
Circle c1 = new Circle(5); // call the constructor with 1 parameters
Circle c2 = new Circle(20, 15); // call the constructor with 2 parameters
Circle c3 = new Circle(10, 8, 5); // call the constructor with 3 parameters
int d;

// test
d = c0.GetR(); // d = 1
d = c1.GetX(); // d = 0
d = c2.GetY(); // d = 15
d = c3.GetR(); // d = 5

double s = c3.GetSquare(); // s = 62.830000000000005

The above code overloads 4 constructors:

// constructor without parameters
Circle()
{
    // ...
}

// constructor with 1 parameter
Circle(int r)
{
    // ...
}

// constructor with 2 parameters
Circle(int x, int y)
{
    // ...
}

// constructor with 3 parameters
Circle(int x, int y, int r)
{
    // ...
}

These constructors differ in a number of parameters.






7. Is it possible to declare two methods in a class that have the same signatures of input parameters, but which return different types of values?

Yes, it is. The following example demonstrates the use of an overloaded Get() method that does not receive parameters, but returns different types of values (int, double).

The class Demo is declared, which implements:

  • internal variable d;
  • an overloaded Get() method without parameters, which returns different types of values.

Class text:

public class Demo
{
    private int d;

    // class constructor
    Demo()
    {
        d = 0;
    }

    // access methods
    int GetD() { return d; }
    double GetD() { return (double)d; }
    void SetD(int nd) { d = nd; }
}

Demonstration of using the Get() method of the Demo class in another program code

Demo d1 = new Demo();
int t;
double x;

d1.SetD(8);

t = d1.Get(); // t = 8 - will be called: int Get()
x = d1.Get(); // x = 8.0 - will be called: double Get()


Related topics