• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Failure to compile with protected method  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the example on IntellijIDE but I am not getting any compile time error as stated in option D. Please help me understand, if there is actually an error in the Cougar class declaration.
Thanks
error.PNG
[Thumbnail for error.PNG]
 
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am sure you have got a compiler error warning there, otherwise why have you got the red underlining?
You have doubtless been told that protected members and constructors are accessible in the same package and in subclasses. The truth is that they are accessible in the object of the subclass, and the main method isn't part of the object.
Please explain what option D is. Does the code come from a cert exam book? If so please tell us the details of the book, including author and page number.
 
Rancher
Posts: 3757
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see two errors there, underlined in red.

Maybe the text of those errors might help you understand the issue?
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:I can see two errors there, underlined in red. . . .

And I have read the wrong error Sorry
 
KaKu dhawan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the 3rd question of Assessment test from the book "OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0–808" by  Jeanne Boyarsky and Scott Selikof.
Correct answers given are C,D and E.
C and E makes complete sense to me and the respective lines show as error in the IDE but D choice(line number 5) doesn't show as error, and I don't see anything wrong.
What am I missing here?

Thanks
Assesment-Test-Question-3.PNG
[Thumbnail for Assesment-Test-Question-3.PNG]
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you have given us the some more details, the problem has become clearer. You expected to see three compile‑time errors and there are only two of them. It is difficult to run your code because I can't copy'n'paste anything from a screenshot. Nor can I check the errata page because I don't know the page number. Please check that errata link. You may find some more explanation here.
 
KaKu dhawan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is 3rd question in the Assessment Test (Page xxxi) from SYBEX Certified Associate Java® SE 8 Programmer I, Study Guide.
Above is the code. The compiler shows error on line 3 and line 7, but not on line no. 5 (as shown in the screenshot).
(choice C)3rd line compiler error is because of illegal restrictive access from public to protected for getTailLength() method, so the compiler error is understandable.
(choice E)7th line compiler error is because we are creating an instance of abstract class , which again is illegal.
But the book says choice D is also correct but I am not getting any compiler error on line 5.
Is this an error in the book or am I missing something here? If it's an error it's not on the errata.
error_3.PNG
[Thumbnail for error_3.PNG]
 
Dave Tolls
Rancher
Posts: 3757
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just want to point out that the thing showing an error there is the IDE, not necessarily the compiler.
Do you get the same issue when compiling from the command line?
 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

I am sure you have got a compiler error warning there, otherwise why have you got the red underlining?
You have doubtless been told that protected members and constructors are accessible in the same package and in subclasses. The truth is that they are accessible in the object of the subclass, and the main method isn't part of the object.
Please explain what option D is. Does the code come from a cert exam book? If so please tell us the details of the book, including author and page number.


i think interface methods are by default public abstract and in overriding it has given protected means accessibility cannot be decreased so will not compile
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . i think interface methods are by default public abstract and in overriding it has given protected means accessibility cannot be decreased so will not compile

That is one of the compile‑time errors. The problem is, what are the other two? The screenshot from the IDE shows a second, but the book says there are three. This thread, which I linked to earlier, seems to explain the third error.

Campbell's computer wrote:javac Cougar.java
Cougar.java:6: error: getTailLength() in Puma cannot implement getTailLength() in HasTail
 protected int getTailLength() {     //compiler error
               ^
 attempting to assign weaker access privileges; was public
Cougar.java:11: error: getTailLength() in Puma cannot implement getTailLength() in HasTail
public class Cougar extends Puma {   //COMPILER ERROR
      ^
 attempting to assign weaker access privileges; was public
Cougar.java:13: error: Puma is abstract; cannot be instantiated
       new Puma();
       ^
3 errors

Right: 3 errors. The line numbers are different. As you said, you can't implement an interface method with protected access. You can't directly instantiate the abstract class. The inherited tail length method in Cougar interferes with its being able to implement the interface, which makes three errors. Any more explanation about that, please?
If you make the following two changes, the code will compile:-
  • 1: Change the access to public in line 6
  • 2: Convert the abstract class to an anonymous class in line 13: new Puma(){};
  • [edit]remove two comments showing location of compile time errors from the code block. One was in the wrong place.
     
    sohail hussain
    Greenhorn
    Posts: 8
    1
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:. . . . The inherited tail length method in Cougar interferes with its being able to implement the interface, which makes three errors. Any more explanation about that, please? . . .


    if first method which is overridden is not compiled then same error is propogated  to the next implementing class so others will not compile
     
    Campbell Ritchie
    Marshal
    Posts: 61766
    193
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please don't quote the whole of a previous post, which I have removed most of.

    Well done explaining that error so clearly.
     
    KaKu dhawan
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    sohail hussain wrote:

    Campbell Ritchie wrote:. . . . The inherited tail length method in Cougar interferes with its being able to implement the interface, which makes three errors. Any more explanation about that, please? . . .


    if first method which is overridden is not compiled then same error is propogated  to the next implementing class so others will not compile



    This "error propagation" from interface to class is what I was confused about. The error is not self  explanatory. But Thanks for clearing things.
     
    Campbell Ritchie
    Marshal
    Posts: 61766
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    KaKu dhawan wrote:. . . . The error is not self  explanatory. But Thanks for clearing things.

    Thkat's a pleasure
    It is actually very difficult to write compiler error messages so as to be easily understood.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!