About Question enthuware.ocajp.i.v8.2.1449 :

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

Moderator: admin

Post Reply
nikitos
Posts: 21
Joined: Mon Oct 24, 2016 6:55 am
Contact:

About Question enthuware.ocajp.i.v8.2.1449 :

Post by nikitos »

A package must have more than one class.
Explanation: A package may have just one class as well.

I think more correct: package may have zero or more classes.

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

Not really. You can't have a package without any class. Try importing a package with no class :)

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

RealJench
Posts: 3
Joined: Mon Nov 21, 2016 5:47 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by RealJench »

Hi

The sentence "A class may inherit from another class." sounds false, would be better with "A class may extend from another class.", no?

Thanks

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

Both mean the same thing.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

phusztek
Posts: 6
Joined: Tue Nov 22, 2016 11:04 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by phusztek »

admin wrote:Not really. You can't have a package without any class. Try importing a package with no class :)

HTH,
Paul.
Hi,

So if I create a class:

package a.b.c;
//valid class definition here

and there is nothing in the a or in the a.b then those ones are not considered as package?
Thanks.

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

At least not by the compiler :)
If you like our products and services, please help us by posting your review here.

fanfa86
Posts: 3
Joined: Mon Sep 04, 2017 1:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by fanfa86 »

Why "A class may inherit from another class." is correct answer?

At least it inherit methods from Object (like equals(), toString(), ecc.), so "may" word can be confusing, "always" could be better?

Maybe my english is poor

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

It is referring to the explicit subclassing using the extends keyword. You are not forced to extend a class from another class. Yes, every class extends from Object but you don't do that explicitly. You don't really write "extends Object" (although you can). Also, Object class doesn't extend any other class, so "always" would also be wrong if you apply strict logic as you seem to be suggesting.
If you like our products and services, please help us by posting your review here.

Antonio
Posts: 3
Joined: Tue May 15, 2018 2:54 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by Antonio »

Hello,

I've noticed that another question (Test 3, Q: 69) say that there is a default package automatically imported in every class with not specified package name. Also in the forum user 'thchuong' quotes Java spec which confirms that there will be current package imported.
And in this question description says that "the class will not be in any package" and ""default" is not really a package".

Could someone explain this contradiction to me please?

Thanks!

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

You should remember that a package is required to be imported only if the importing class does not belong to the package that you are trying to import. In that sense, the package to which a class belongs is always "imported" by default. But technically, no import is necessary here.

Second, classes that belong to the "default" package i.e. classes that do not have any package statement in them, cannot be imported. In other words, it is not possible for a class that belongs to some package to access a class that does not belong to any package (i.e. the default package).

Now, coming to Test 3 Q 69 i.e. qid 2.894: The explanation clearly says that only in this specific case the default package is imported because the class given in the question does not have a package statement. Here is what the explanation says,
If there is no package statement in the source file, the class is assumed to be created in a default package that has no name. In this case, all the types created in this default package will be available to this class without any import statement. However, note that this default package cannot be imported in classes that belong to any other package at all, not even with any sort of import statement. So for example, if you have a class named SomeClass in package test, you cannot access TestClass defined in the problem statement (as it is defined in the default package) at all because there is no way to import it.
Let me know if you still feel there is a contradiction.

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

Antonio
Posts: 3
Joined: Tue May 15, 2018 2:54 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by Antonio »

Thank you for quick response, Paul!

Unfortunately I'm still in the dark.

The two contradicting statements (summary) for me are following:
  • 1. Not every class belongs to a package (Test 2, Q: 47)
    2. Classes without defined package have automatically imported package with no name (default package) (Test 3, Q: 69)
Those lead me to following conclusions:
  • 1st statement means that classes without package doesn't belong to default package.
    2nd statement confirms that the fact that Java automatically imports default package means that the class belongs to the default package?
Could you please point out where my conclusions are wrong?

Thank you for help!

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

Your question made me dig into the JLS and here is what I found -
1. Section 7.4.2 says, "A compilation unit that has no package declaration is part of an unnamed package."

So clearly, the statement "Not every class belongs to a package" in Qid 2.1449 (T2 Q47) is incorrect because JLS calls unnamed package a package. Must be fixed.

2. Further, section 7.3 and 7.5 say (respectively):
Every compilation unit implicitly imports every public type name declared in the predefined package java.lang , as if the declaration import java.lang.*; appeared at the beginning of each compilation unit immediately after any package statement. As a result, the names of all those types are available as simple names in every compilation unit.
and
Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name


These two statements imply that classes of the same package are accessible to each other without any import statements.

So the statement in Qid 2.894 (T2 q69) is correct because all classes of the noname package can indeed access each other. The explanation in the question also states, "However, note that this default package cannot be imported in classes that belong to any other package at all, not even with any sort of import statement.", which is also correct.


In summary, I think the explanations to use the term "unnamed" package instead of "default" package because that is the terminology used by JLS. In other words, class that don't have a package statement belong to the "unnamed"
package.

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

Antonio
Posts: 3
Joined: Tue May 15, 2018 2:54 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by Antonio »

Thanks a lot for you help and time, Paul.
Now it's clear.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by swarna pakeer »

"Every class belongs to a package" , to which Object oriented feature of Java this belongs to ?

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

It has nothing to do with OO. It is just a feature of java. The second alternate form of question is not good but is mentioned because of the reason explained there.
If you like our products and services, please help us by posting your review here.

Seán Kennedy
Posts: 12
Joined: Wed Feb 17, 2021 6:55 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by Seán Kennedy »

Hi, the first option "Every class must have a main method so that it can be tested individually from command line." confuses me. The answer states "The main method is required only if you want to execute that class directly from a command line.". Fully agree that not every class needs a main() method but if you want to test it "individually from command line" as the question asks, you do, don't you? In other words, should this not be a correct option also i.e. if you want to test your class individually from the command line, you need a main() method. Thanks, Seán.

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

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by admin »

It is really an ambiguous option, to be honest. The way I am interpreting "individually" is that the class is not part of a library or some other set of classes. Since "testing" does not mean executing the main method, "testing from command line" doesn't necessarily mean that that class is to be executed "directly". For example, a class's may have several business methods and that class still can be tested from command line using whatever testing methodology one might be using such as using JUnit. Further, executing the main method is no guarantee that its business methods are tested!

Overall, I guess if one selects this as a right option, it should be alright. It is a badly framed option.
If you like our products and services, please help us by posting your review here.

Seán Kennedy
Posts: 12
Joined: Wed Feb 17, 2021 6:55 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1449 :

Post by Seán Kennedy »

Thanks for that. Appreciate the prompt response. /Seán.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 52 guests