Home arrow Java arrow Page 2 - A Closer Look at Methods and Classes

A Closer Look at Methods and Classes

Have you ever wanted to get an in-depth understanding of methods and classes in Java? Look no further. This article is excerpted from chapter 7 of Java: the Complete Reference, J2SE 5 Edition, written by Herbert Schildt (McGraw-Hill/Osborne, 2004; ISBN: 0072230738). 

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 22
June 30, 2005
  1. · A Closer Look at Methods and Classes
  2. · Overloading Constructors
  3. · Using Objects as Parameters
  4. · A Closer Look at Argument Passing
  5. · Returning Objects
  6. · Recursion
  7. · Introducing Access Control
  8. · Understanding static
  9. · Introducing final
  10. · Introducing Nested and Inner Classes
  11. · Exploring the String Class
  12. · Using Command-Line Arguments

print this article

A Closer Look at Methods and Classes - Overloading Constructors
(Page 2 of 12 )

In addition to overloading normal methods, you can also overload constructor methods. In fact, for most real-world classes that you create, overloaded constructors will be the norm, not the exception. To understand why, letís return to the Box class developed in the preceding chapter. Following is the latest version of Box:

class Box{
  double width;
  double height;
  double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
// compute and return volume
  double volume() {
    return width * height * depth;

As you can see, the Box( ) constructor requires three parameters. This means that all declarations of Box objects must pass three arguments to the Box( ) constructor. For example, the following statement is currently invalid:

Box ob = new Box();

Since Box( ) requires three arguments, itís an error to call it without them. This raises some important questions. What if you simply wanted a box and did not care (or know) what its initial dimensions were? Or, what if you want to be able to initialize a cube by specifying only one value that would be used for all three dimensions? As the Box class is currently written, these other options are not available to you.

Fortunately, the solution to these problems is quite easy: simply overload the Box constructor so that it handles the situations just described. Here is a program that contains an improved version of Box that does just that:

/* 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);

The output produced by this program is shown here:

Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0

As you can see, the proper overloaded constructor is called based upon the parameters specified when new is executed.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials