About Question enthuware.ocajp.i.v7.2.858 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
germanosouza
Posts: 1
Joined: Mon Apr 06, 2015 9:37 pm
Contact:

About Question enthuware.ocajp.i.v7.2.858 :

Post by germanosouza »

The correct answer is "It is provided by the compiler only if the class and any of its super classes does not define any constructor."

The code below will not compile, because it was defined a constructor and the compiler will not generate a default constructor for class C1. So, class C2 cannot call C1().

class C1 {
C1(int i){}
}

class C2 extends C1{ }

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.858 :

Post by admin »

Not sure what is your point. C1 doesn't get the default constructor because C1 defines one constructor explicitly. C2 will certainly get the default constructor. C2 still doesn't compile though, because the default constructor that it gets tries to call the default constructor of its super class C1, which is not available.
If you like our products and services, please help us by posting your review here.

Argyleghost
Posts: 1
Joined: Wed Apr 13, 2016 9:22 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.858 :

Post by Argyleghost »

Technically both answers are correct:
1. It takes no arguments
2. "It is provided by the compiler only if the class and any of its super classes does not define any constructor."


Reason why they are both correct:
1. Obvious, Yes, the default takes no args.
2. (Breaking up the statement)... Yes, it is provided by the compiler if it is not defined in the class, so:

Class test {
test(String s){};
}

Here, we provided a constructor that takes a String, so the compiler would NOT provide a default constructor. What germanosouza was trying to state that if you have this set up is that if you have this:

Class A {
A(String s){}
}

Class B extends A {
B(String s){}
}

these classes have both defined a constructor with 1 arg, so the compiler does not create a default constructor in either class A or B. Of course it will fail to compile if you removed class A's 1 arg constructor, cause the compiler will not generate a default no constructor for class B. So really the way it is worded "It is provided by the compiler only if THE CLASS and ANY OF ITS SUPER CLASSES does NOT define any constructor."

does that make sense?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.858 :

Post by admin »

The condition in the given statement for letting the compiler provide a constructor is, "only if the class and any of its super classes do not define any constructor".

Now, will a constructor be provided for a class if the class does not define a constructor but its super class does? The answer is yes. Hence, the given statement is wrong.

-Paul.
If you like our products and services, please help us by posting your review here.

myEtsUsername
Posts: 2
Joined: Sun Sep 30, 2018 12:03 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.858 :

Post by myEtsUsername »

Why is it incorrect that the default constructor "is always public"? Is it instead considered 'package private'? Is it public in some cases but not others?
Thanks

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.858 :

Post by admin »

Because, as the explanation to this option says, "The access type of a default constructor is same as the access type of the class. Thus, if a class is public, the default constructor will be public."
If the class has defaul access then the default construction will have default access.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 97 guests