-2

This is question (and answer) from OCJP test exam. And I was very confused about it.

Here is a question review (on Kaplan SelfTest site):

Reviewing Answered Item 64 of 90Ref: 1Z0-803.6.5.6

Which statement is true about constructor overloading?

  • A default constructor can be overloaded in the same class.
  • A default constructor can be overloaded in a subclass.
  • The constructor must use a different name.
  • The constructor must use the this keyword.

Explanation:

A default constructor can be overloaded in a subclass. If no constructor is defined for a class, then the compiler will automatically provide the default constructor. Because a subclass can define its own constructors without affecting the superclass, a constructor with parameters can be defined that invokes the superclass constructor, implicitly or explicitly.

A default constructor cannot be overloaded in the same class. This is because once a constructor is defined in a class, the compiler will not create the default constructor. Thus, an attempt to overload the default constructor will effectively remove it from the class.

The constructor must not use a different name. In the same class, an overloaded constructor uses the same name. Because subclasses differ in name from their superclass, an overloaded constructor will have a different name.

The constructor does not need to use the this keyword. The this keyword allows a constructor to reference other constructor methods and/or instance context. Using the this keyword is not required in an overloaded constructor.

Objective:

Working with Methods and Encapsulation

Sub-Objective:

Create and overload constructors

Ilya Bystrov
  • 2,902
  • 2
  • 14
  • 21
  • 1
    Your statement tells you exactly what to do. Class without a constructor, subclass with a constructor. Why can't you just do it? – Sotirios Delimanolis Jun 21 '14 at 22:14
  • And, no. `A default constructor can be overloaded in a subclass`. That statement does not make sense. constructor overloading only makes sense in the context of a single class, not across object hierarchies. – Sotirios Delimanolis Jun 21 '14 at 22:23
  • 1
    @SotiriosDelimanolis I think so, too. But this is question (and answer) from OCJP test exam. And I was very confused about it. – Ilya Bystrov Jun 21 '14 at 22:33
  • 1
    @login So please could you add this to the question without this information the questions seems pretty pointless. – idmean Jun 21 '14 at 22:56

3 Answers3

5

You are getting your terminology wrong.

Overloading only takes place for methods with the same name but different parameters within a specific class.

Overriding takes place for subclasses which have methods with the same "signature" (same name and parameters) as a method in the Superclass.

Note that constuctors aren't "Overridden" (by definition they have different signatures than methods in the superclass).

Instead, the compiler makes a constructor implicitly call the default constructor of its superclass (if one exists) unless you explicitly call a specific constructor of a superclass using the super keyword.

Similarly you cannot "Overload" the constructor of a superclass in a subclass (overloading doesn't work across inheritance), but you can "overload" another constructor within your specific class.

Here is an example of implicit and explicit calls to superclass constructors and overloading constructors:

public class X {

    // no default constructor defined so
    // compiler adds implicit default constructor here: public X(){}

}

public class Y extends X {

    // explicitly declared default-constructor for class Y
    public Y() {
         // compiler automatically calls implicit X default constructor here
         System.out.println("constructing a Y instance");
    }

    // explicitly declared constructor which overloads the default constructor for Y
    public Y(String s) {
        // compiler automatically calls implicit X default constructor here
        System.out.println("constructing a Y instance with param " + s);
    }

}

public class Z extends Y {

    // explicitly declared default constructor for Z
    public Z() {
        super("Z");    // explicitly call the non-default constructor in Y here
        System.out.println("constructing a Z instance"); 
    }

}

thus constructing a Z ends up calling X(), then Y("Z") and results in this output:

Z z = new Z(); 

// outputs: constructing a Y instance with param Z
//          constructing a Z instance

And from the docs: (http://docs.oracle.com/javase/tutorial/java/IandI/super.html)

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.

Hope that helps clarify things for you. If in doubt read the docs, write some code, and use System.out.println() to see what's happening.

Matt Coubrough
  • 3,739
  • 2
  • 26
  • 40
0

Constructors are not technically member functions, so they aren't really inherited, and thus cannot be "overloaded". However, they can still be called from the subclass using the super keyword.

//the class being inherited
public class superclass
{

    //the constructor
    public superclass(some_parameter)
    {
        //do stuff
    }

}

public class subclass extends superclass
{

    public subclass(some_parameter)
    {

        //the super keyword here is used to access the inherited class
        super(some_parameter); //this calls the constructor of the inherited class;

    }

}
cnsumner
  • 515
  • 4
  • 17
0

First, overload is not the same as override. Example:

class Foo {
  void overloadedMethod() {}

  void overloadedMethod(int i) {}

  void overridenMethod() {}
}

class Bar extends Foo {
  @Override
  void overridenMethod() {}
}

Constructor can be overloaded, just as ordinary methods, but cannot be overriden.

For more info see here: link

Community
  • 1
  • 1
kajacx
  • 12,361
  • 5
  • 43
  • 70