Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
367 views

Lab # 6 Method and Constructor Overloading: Introducing

Method and constructor overloading allows defining multiple methods or constructors with the same name but different parameters. This is known as overloading and implements polymorphism. The document provides examples of overloading methods and constructors in Java classes. It demonstrates defining multiple test methods that vary by parameters, and multiple constructors for a Sphere class that initialize fields differently. Overloading enables generating objects with different initial data sets.

Uploaded by

Hasnain Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views

Lab # 6 Method and Constructor Overloading: Introducing

Method and constructor overloading allows defining multiple methods or constructors with the same name but different parameters. This is known as overloading and implements polymorphism. The document provides examples of overloading methods and constructors in Java classes. It demonstrates defining multiple test methods that vary by parameters, and multiple constructors for a Sphere class that initialize fields differently. Overloading enables generating objects with different initial data sets.

Uploaded by

Hasnain Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Method and constructor overloading Assignment

LAB # 6

INTRODUCING METHOD AND


CONSTRUCTOR OVERLOADING

OBJECTIVE:
To Study method overloading, constructor overloading.

THEORY:

6.1 OVERLOADING METHODS

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.

Here is the example:

// Demonstrate method overloading.


class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {

Object Oriented Programming - OOPs 1


Method and constructor overloading Assignment

public static void main(String args[]) {


OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}

This program generates the following output:

6.2 CONSTRUCTORS OVERLOADING

Constructors are methods that can be overloaded, just like any other method
in a class. In most situations, you will want to generate objects of a class from
different sets of initial defining data. If you just consider the Sphere class, you
could conceive of a need to define a Sphere object in a variety of ways. You
might well want a constructor that accepted just the (x, y, z) coordinates of a
point, and have a Sphere object created with a default radius of 1.0. Another
possibility is that you may want to create a default Sphere with a radius of 1.0
positioned at the origin, so no arguments would be specified at all. This
requires two constructors in addition to the one you have already written. Let’s
try it then

Multiple Constructors for the Sphere Class


The code for the extra constructors is as follows:

Object Oriented Programming - OOPs 2


Method and constructor overloading Assignment

The statements in the default constructor that set three fields to zero are not really necessary,
as the fields would be set to zero by default. They are there just to emphasize that the primary
purpose of a constructor is to enable you to set initial values for the fields. If you add the
following statements to the Create Spheres class, you can test out the new constructors:

Now the program should produce the following output:


Number of objects = 0
Number of objects = 1
Number of objects = 2
Number of objects = 4
ball volume = 267.94666666666666
globe volume = 7234.559999999999
eightBall volume = 4.1866666666666665
oddBall volume = 4.186666666666666

Another Example:

Object Oriented Programming - OOPs 3


Method and constructor overloading Assignment

In addition to overloading normal methods, you can also overload constructor


methods.

/* Here, Box defines three constructors to initialize


the dimensions of a box various ways.
*/
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}

class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}

Object Oriented Programming - OOPs 4


Method and constructor overloading Assignment

The output produced by this program is shown here:

Object Oriented Programming - OOPs 5


Method and constructor overloading Assignment

LAB TASK

1. Write a program that creates two classes one is Employee and another
is the main method class. Employee class contains emp_code, name,
designation and salary fields, and three constructors; one is a default
constructor, the second constructor contains three field emp-code,
name, and designation and if the designation is clerk set salary of Rs.
5000 if the designation is peon set salary of Rs. 2000 and if manager
set salary of Rs. 10000 for others set the salary of Rs. 1000 and third
constructor contain four fields emp_code, name, designation, and
salary. Display information of employee using display method In Java.

2. Write a program that creates two classes one is Measurement, and


another is the main method class. Measurement class contain km,
meter, millimeter in decimal and a constructor and three methods; first
method will return the round off kilometer, the second method will
return the round off meter and third will return the round off millimeter.
Takes the values and display them.

ASSIGNMENT

1.List down 10 built-in methods in Java.


2.List down the valid and invalid cases of method and constructor overloading

Object Oriented Programming - OOPs 6

You might also like