Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Access Modifiers Question

 
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, during studying I faced this question:

Given the following my.school.ClassRoom and my.city.School class definitions, which
line numbers in main() generate a compiler error? (Choose all that apply)




A. None, the code compiles fine.
B. Line 5
C. Line 6
D. Line 7
E. Line 8
F. Line 9




The correct answer is B,D,F,C

I inderstand that:

B causes compile error because globalKey  access set to default meaning that only the classes in the same package can view it
D causes compile error because roomNumberis is private
F causes compile error because teacherName is protected and School does not extend ClassRoom

I cannot understand why C causes compile error.
Classroom room = new Classroom(101, ""Mrs. Anderson");
It just creates a new instance of the class.
 
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such questions come from, to avoid copyright problems and at allow us to assess the original.
Please look at the access modifier for the Classroom constructor.
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This comes from SYBEX book.

Ok my mistake, I didn't notice that the constructor has default access modifier, so it is only available in the same package

thanks
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure to help Which Sybex book, and which page, please?
 
Ioanna Katsanou
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:My pleasure to help Which Sybex book, and which page, please?




OCA: Oracle®
Certified Associate Java®
SE 8 Programmer I
Study Guide
Exam 1Z0-808

page 220
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Rancher
Posts: 4603
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity, but do these questions really have no indentation in them?
 
Sheriff
Posts: 7635
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:


Such line of code shouldn't compile, but check if you copied it right.

I have copied this topic to our OCA certification topic too.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:This comes from SYBEX book.

Ok my mistake, I didn't notice that the constructor has default access modifier, so it is only available in the same package

thanks



how is line 6 compiling, the String passed is not correct it has an extra quotation ???
 
Campbell Ritchie
Marshal
Posts: 69740
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answers given suggest that the code won't compile; that is C (I think).
 
Pankajkumar chanda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was fast reply, what i should have asked was if line 6 is not compiling how will line 8 compile, since its working on null object.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because line 8 (and 7 and 9) would be runtime errors, not compile time.  
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic