Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

OCA 8 - Compilation errors review

 
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

  In OCA for Java8 book by Jeanne Boyarsky and Scott Selikoff, we have following assessment question:



Above code fail to compile successfully  

Compilation errors are:


/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:4: error: getTailLength() in Puma cannot implement getTailLength() in HasTail
 protected int getTailLength() { return 4; }
               ^
 attempting to assign weaker access privileges; was public
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:7: error: getTailLength() in Puma cannot implement getTailLength() in HasTail
public class Sample extends Puma {
      ^
 attempting to assign weaker access privileges; was public
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:9: error: Puma is abstract; cannot be instantiated
   Puma puma = new Puma();
               ^
3 errors


Here, I am curious to know why Java compiler spits out same error in different places. Isn't it enough to give compilation error at line 4 and end compilation.

Once it knows that abstract class is not implemented correctly, there is no point in going further compiling.

Please let us know your views regarding this.


Thanks.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Omkar Shetkar wrote:Here, I am curious to know why Java compiler spits out same error in different places. Isn't it enough to give compilation error at line 4 and end compilation.

Once it knows that abstract class is not implemented correctly, there is no point in going further compiling.


The compiler simply provides you with all valuable information it is aware of. And because your code snippet has several errors, the compiler gives you several compiler errors. The compiler also has no clue about what the developer tries to accomplish, the compiler can only check the huge set of rules to verify you don't have violated any of these rules.

1/ in the abstract Puma class you have an invalid method override of the getTailLength() method of the HasTail interface because it has a more strict access modifier (protected vs public) => compiler error 1

2/ because class Sample extends from Puma, Sample IS-A Puma and Sample IS-A HasTail. Sample is a concrete class, so it must implement all abstract methods and because Puma doesn't provide a valid implementation for the getTailLength() method, Sample must define a valid implementation for this method but it doesn't => compiler error 1

3/ Puma is an abstract class and you can't create an instance of an abstract class (even if it would have a valid implementation of the getTailLength() method) => compiler error 3

And it's a good thing you get as many compiler errors as possible at once, because now you can fix them all at once instead of having to go through the cycle "compile & fix compiler error" many times.

Hope it helps!
Kind regards,
Roel
 
Omkar Shetkar
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

And it's a good thing you get as many compiler errors as possible at once, because now you can fix them all at once instead of having to go through the cycle "compile & fix compiler error" many times.



I agree with the fact that Java compiler tries to give as much information to user as possible at once.

But in some cases it doesn't.

For example, in the above code snippet after sysout suppose I miss out semicolon.

Then compiler greets with:

/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:10: error: ';' expected
   System.out.println(puma.getTailLength())


Here, it obviously not given any hint about other errors related with abstract class.

IMHO, I would regard this as "kind of inconsistency" from Java compiler.

OR

Is it the case that compiler follows some kind of rules which prioritizes compilation errors?

OR

Am I missing something here ?

Thanks.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Omkar Shetkar wrote:IMHO, I would regard this as "kind of inconsistency" from Java compiler.

OR

Is it the case that compiler follows some kind of rules which prioritizes compilation errors?

OR

Am I missing something here ?


It is not an inconsistency of the Java compiler. It simply depends on the kind of error the developer has made. Because you forgot the semicolon, you have an invalid source code file. So the compiler can't check the class declarations until you have a valid source code file. And I assume you have only 1 source code file with 2 class and 1 interface declarations. Even if you would have a seperate source code file for every class and interface declaration, you'll get the same behavior (so that's very consistent as well ). So the compiler definitely has different set of rules which are verified.

Try renaming the variable puma to a Java keyword (e.g. int) and see what happens...
 
Omkar Shetkar
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I rename Puma to int keyword, then I get following errors:

/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:3: error: <identifier> expected
abstract class int implements HasTail {
             ^
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:3: error: illegal start of type
abstract class int implements HasTail {
                  ^
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:3: error: ';' expected
abstract class int implements HasTail {
                                    ^
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:4: error: illegal start of expression
 protected int getTailLength() { return 4; }
 ^
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:4: error: ';' expected
 protected int getTailLength() { return 4; }
                            ^
/Users/oshetkar/Personal/Learn/JavaCertification/Practice/Introduction/Sample.java:14: error: reached end of file while parsing
}
^
6 errors


Here, I see compiler doesn't stop at line 3 where the error originates. It goes on compiling the class and gives out error where Java class doesn't seem to follow the rules.

I think, difference between missing a semicolon and replacing class name with int is, one is syntactical error and the other is semantic one.
If syntactical error occurs Java compiler halts as soon as one occurs and no point in semantic check.
If syntactically correct and semantic error occurs, Java tries to compile full class file and spits out as many error messages.

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Omkar Shetkar wrote:If I rename Puma to int keyword, then I get following errors


Oh, you have renamed the class name Puma to int. I meant renaming variable puma to int (or another Java keyword), so you would get Puma puma = new Puma();. But that's not really an issue! You'll see that the compiler behaves differently depending on which mistake you have made. And that's probably because the compiler does different steps to verify the source code.
 
Omkar Shetkar
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel for clarifying on this query.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!