aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ambigous compiling errors. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ambigous compiling errors." Watch "Ambigous compiling errors." New topic
Author

Ambigous compiling errors.

samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello EveryBody,

I'm preparing myself for the SCJP 6, i was testing inheritance, and members accessibiliyty between subclass and superclass.
I have superclass Vehicle with 3 fields/3 methods (public, protected and private),



Obviously the public members are visible to the subclass, but what i didn't understand is why the error message for the private vehicle field is not the same as the protected one?
can someone explain to me this behavior please ?

farm rubbit hihihihihihi, be aware !!
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

It would be better if you post your Vehicle class and Car class code here..

And use CodeTags to quote your code..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Also....

samantha clarkson wrote:Compiling errors :

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The field Vehicle.protectedVehiclefield is not visible
privateVehicleField cannot be resolved or is not a field
The method protectedVehicleMethod() from the type Vehicle is not visible
The method privateVehicleMethod() from the type Vehicle is not visible


it would be better to give us the errors from the compiler, instead of running the code via an IDE (even though it has compile errors) and giving us the runtime errors.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Well to compile the whole project using javac made me crazy ,
here the structure of my project under Eclipse :

Name of Proect : Training
Training
|______src
|___scjpform.chapter1.part2
| |__Car.java
| |__Vehicle.java
|
|___scjpform.chapter1.part2.test
|__TestAll.java




Code for Vehicle class :



Code for Car class :



Code for TestAll class :




So i didnt succed to compile my class TestAll , what i did is to put the three classes together in another directory(classbuild), i removed package names and imported packages in class definitions, now i can compile my Class TestAll.java easily :
- inside classbuild directory
- javac -cp . Vehicle.java --> good
- javac -cp . Car.java --> good
- javac -cp . TestAll.java --> 2 errors


And now it becomes more stranger than before , only the private members of the class Vehicle are not found and no errors about the protected ones !!!

Another situation if i use a main method directly in my Car class :



with Eclipse i have errors :



and with CMD line Windows i have :



Well i didnt understand the rules of error messages , why they are different when i use my objects directly in the subclass or in another class TestClass.
When we can say not found and when we can say not visible, and there is any influence if the structure include packages and no direct access? Im confused
is there a documentation about error compilation rules?

Thaks for the reply
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

samantha clarkson wrote:
Well i didnt understand the rules of error messages , why they are different when i use my objects directly in the subclass or in another class TestClass.
When we can say not found and when we can say not visible, and there is any influence if the structure include packages and no direct access? Im confused
is there a documentation about error compilation rules?


A little bit about compilers -- and this is true for all (that I know of) compiled languages. ie. not specific to Java.

When a compiler finds an error, it reports on it, and then if the compiler is in a useable state, continue compiling. As an analogy, let's say you are reading a book, and you encountered something you don't understand, because someone ripped out a page. You report this confusion to the librarian, that there may be content missing, then you check to see if there is a another chapter, and you continue reading.... do you now understand the possible problem?

The compiler continues to the next line, or next block, etc., under the assumption that the error reported wasn't important. It is possible that this assumption is correct. And of course, it is possible that it is not. The error can trigger errors (false positives) where there isn't any because of missing context. The compiler can also miss errors because of the missing context. This is why an experienced programmer always fix the issues in order, going down the list of errors, until the messages don't make sense, then recompile it to get an updated list of errors.... an experienced programmer will also constantly compile to keep this list small.

Anyway, why should you never run a program that didn't compile?? Because the list is *not* in order (actually, there may not be a list). The error message that is printed is the first one encountered when the program is ran -- it may be the first error encountered during compilation, or it may be one of the false positives that was found late in the compilation process.... to repeat, never run code that didn't compile properly.

Henry
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

samantha clarkson wrote:
And now it becomes more stranger than before , only the private members of the class Vehicle are not found and no errors about the protected ones !!!

Another situation if i use a main methode directly in my Car class :



A class cannot see a private member of another class, that is why we say it's private right??
Now as for protected members, they are visible in the derived class through inheritance, but you still can't access it directly..
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Henry Wong wrote:
samantha clarkson wrote:
Well i didn't understand the rules of error messages , why they are different when i use my objects directly in the subclass or in another class TestClass.
When we can say not found and when we can say not visible, and there is any influence if the structure include packages and no direct access? I'm confused
is there a documentation about error compilation rules?


A little bit about compilers -- and this is true for all (that I know of) compiled languages. ie. not specific to Java.

When a compiler finds an error, it reports on it, and then if the compiler is in a useable state, continue compiling. As an analogy, let's say you are reading a book, and you encountered something you don't understand, because someone ripped out a page. You report this confusion to the librarian, that there may be content missing, then you check to see if there is a another chapter, and you continue reading.... do you now understand the possible problem?

The compiler continues to the next line, or next block, etc., under the assumption that the error reported wasn't important. It is possible that this assumption is correct. And of course, it is possible that it is not. The error can trigger errors (false positives) where there isn't any because of missing context. The compiler can also miss errors because of the missing context. This is why an experienced programmer always fix the issues in order, going down the list of errors, until the messages don't make sense, then recompile it to get an updated list of errors.... an experienced programmer will also constantly compile to keep this list small.

Anyway, why should you never run a program that didn't compile?? Because the list is *not* in order (actually, there may not be a list). The error message that is printed is the first one encountered when the program is ran -- it may be the first error encountered during compilation, or it may be one of the false positives that was found late in the compilation process.... to repeat, never run code that didn't compile properly.

Henry


First thank you for reply, well i think that i was misunderstood, i know that running this code will generate errors, my objective was to see compiler errors, and to observe the behavior between using superclass fields in the contract of the subclass and using superclass fields for an object of type Car. I thought for the private field to find something like field not found (not "not visible"), because the subclass shouldn't have access to private field of the superclass so shouldn't look for visibility at all for the private fields
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
R. Jain wrote:
samantha clarkson wrote:
And now it becomes more stranger than before , only the private members of the class Vehicle are not found and no errors about the protected ones !!!

Another situation if i use a main method directly in my Car class :



A class cannot see a private member of another class, that is why we say it's private right??
Now as for protected members, they are visible in the derived class through inheritance, but you still can't access it directly..


100% with you, the subclass cannot see the private member, but what i didn't know is for the protected one, that it could be accessed just in the Class Car and not in other class using an object type Car.
does scjp exams contain question about expecting error messages?
Thank you
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

samantha clarkson wrote:
100% with you, the subclass cannot see the private member, but what i didn't know is for the protected one, that it could be accessed just in the Class Car and not in other class using an object type Car.
does scjp exams contain question about expecting error messages?
Thank you


Don't forget that protected members can also be accessed by classes in the same package as the class where the member was declared in.

Henry
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Thank you Henry
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

samantha clarkson wrote:
does scjp exams contain question about expecting error messages?
Thank you


So many of 'em
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ambigous compiling errors.