• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Constructor overloading

 
Greenhorn
Posts: 14
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaplan self test OCAJP7 practice set => Item: 5 (Ref:1Z0-803.6.5.6)


When i read question then i just mark option 5 as answer.
But reading question and all option carefully i choose option 1.
And correct answer was option 2.
I am unable to understand how?


Which statement is true about constructor overloading?

1. A default constructor can be overloaded in the same class.
2. A default constructor can be overloaded in a subclass.
3. The constructor must use a different name.
4. The constructor must use the this keyword.
5. None of these.


 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh kumar Gupta wrote:Which statement is true about constructor overloading?


The question suggests only one statement is correct.

So let's go through all the different options and see if the statement is correct or incorrect:
1. A default constructor can be overloaded in the same class. => Incorrect! Although constructor overloading can only occur in the same class, the default constructor is added by the compiler when you don't define an explicit constructor. So if you add an explicit constructor, no default constructor will be provided by the compiler and therefore only one constructor exists and there is no overloading taking place
2. A default constructor can be overloaded in a subclass. => Incorrect! Constructor overloading can only occur in the same class. You can't overload a constructor in the superclass by adding a constructor (with different parameter list) in the subclass. That's different with methods: if you have a method in a superclass (which is visible to the subclass), you can overload this method in the subclass by defining a method with a different parameter list in the subclass.
3. The constructor must use a different name. => Incorrect! All constructors must have the same name as the class, so overloaded constructors will have the same name (but a different parameter list).
4. The constructor must use the this keyword. => Incorrect! The overloaded constructor is not required to use the this keyword; it can use the super keyword as well.
5. None of these. => Correct! Because all the above are incorrect

So the correct answer (in my opinion) for this question should be option 5.

Note: if the first option/statement would have been: "A no-arg constructor can be overloaded in the same class.", then the correct answer would have been option 1. Because if a class has a no-arg constructor, you can define another constructor with a different parameter list in the same class to overload this no-arg constructor. If the second option/statement would have been: "A no-arg constructor can be overloaded in a subclass.", then the option/statement would still be incorrect for the same reason as mentioned with option 2 (constructor overloading can only occur in the same class).

Hope it helps!
Kind regards,
Roel
 
Rakesh kumar Gupta
Greenhorn
Posts: 14
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if you add an explicit constructor, no default constructor will be provided by the compiler and therefore only one constructor
exists and there is no overloading taking place


I understand when constructor overloading takes place.
But in

Kaplan Self test practice explain it as...



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.

 
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh kumar Gupta wrote:2. A default constructor can be overloaded in a subclass.


Constructors are not members, so they are not inherited.
 
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaplan Self test practice explain it as...

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.
[...]
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.


So they are saying you can overload the default constructor of a base class, say A, with an explicit constructor in a subclass, say B.

Any constructors in class A will have the name A and any constructors in class B will have the name B (which they acknowledge). But by definition an overloaded method or constructor must have the same name, so this cannot be an overload.
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaplan Self test practice explanation wrote: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.


I agree! That's exactly the same explanation I provided in my previous post.

Kaplan Self test practice explanation wrote: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.


True! Again similar to what I have said.

Kaplan Self test practice explanation wrote: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.


I totally disagree! That's complete BS! As I have explained, constructor overloading is only possible in the same class, but not in class hierarchies. And there are a few reasons why it's impossible to overload a superclass constructor in a subclass:
1/ constructors are not considered to be members of a class (like methods). So a subclass won't inherit the superclass' constructors and thus a subclass can't overload a superclass constructor.
2/ there are 2 very important rules for a valid overload: same name and different parameter list. A subclass has (almost*) always a different name than the superclass, therefore the subclass constructor has a different name then the superclass constructor. So the subclass constructor can't overload the superclass constructor. That's the reason why constructor overloading is only possible in the same class (because constructors in the same class all have the same name).

Kaplan Self test practice explanation wrote: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.


Again this make no sense at all! Constructor overloading is only possible in the same class, but not in class hierarchies. And that's different with methods, because a superclass method can be overloaded in the subclass. Both methods must have the same name and a different parameter list. So it's a key requirement to use exactly the same name (think about case sensitivity in Java) if you want to overload a method. And this applies to constructors as well and that's why it is impossible to overload a default constructor in a subclass. And that's also confirmed in the JLS: section 8.8.8. Constructor Overloading refers to 8.4.9. Overloading with the words "Overloading of constructors is identical in behavior to overloading of methods" (and that is exactly what I have described in my replies

Rakesh kumar Gupta wrote:I understand when constructor overloading takes place.
But in

Kaplan Self test practice explain it as...


You should definitely contact Kaplan SelfTest and mention the correct answer and explanation are definitely wrong. It's a (major) mistake on their side! You can always refer to this topic if you report this errata item to Kaplan SelfTest.

Hope it helps!
Kind regards,
Roel
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic