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

Extending protected abstract class? (Java OCA 8 Programmer I Study Guide, Sybex)

 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,




Gives compilation error "modifier protected not allowed here".

According the Boyarsky and Selikoff book(chapter 5, page 265 rule 4) abstract classes are allowed to be protected.

Can anybody give me an example of a protected abstract class in use?

thanks
Kind Regards
Ben
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ben Pittens wrote:According the Boyarsky and Selikoff book(chapter 5, page 265 rule 4) abstract classes are allowed to be protected.


When I look at the abstract class definition rules on page 265, rule 3 states: "Abstract classes may not be marked as private or final". So I guess this is the rule you are referring to (instead of rule 4).

Ben Pittens wrote:Can anybody give me an example of a protected abstract class in use?


Top-level classes (and other types) can only have public or default (package-private) access. Other access levels (private and protected) are not allowed. This rule applies to all classes (and other types), not only to abstract classes.

Your class test20 is a top-level class, so you have only two allowed access levelsWhen your class is nested inside another class, you can have one of the four access levels as illustrated in this code snippetAny of these outer or inner classes can be marked as abstract as well. Please note that nested classes are not on the OCA exam (but will be on the OCP exam). So I think you have discovered an errata item (and I have to say I'm a bit surprised it's not yet on the official errata overview). Rule 3 should read as: "Abstract classes may not be marked as protected, private or final".

Hope it helps!
Kind regards,
Roel
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

Thanks, This helps,


When I look at the abstract class definition rules on page 265, rule 3 states: "Abstract classes may not be marked as private or final". So I guess this is the rule you are referring to (instead of rule 4).



Yes I mean rule 3.



Top-level classes (and other types) can only have public or default (package-private) access. Other access levels (private and protected) are not allowed. This rule applies to all classes (and other types), not only to abstract classes



Lets look to the definition of top level classes:
Toplevel classes are all classes except innerclasses, right?
So all classes in the OCA exam are top level classes by design?

Kind regards
Ben
 
author
Posts: 4112
28
Google Web Toolkit Eclipse IDE Flex
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ben Pittens wrote:Lets look to the definition of top level classes:
Toplevel classes are all classes except innerclasses, right?
So all classes in the OCA exam are top level classes by design?



This thread might help: http://www.coderanch.com/t/410134/java/java/private-protected-class

The problem is if you had a top-level class that is private or protected, there would be no way to access it. For example, a private top-level class (if it existed) would not be visible to any other class in thr JVM, therefore there would be no way to call/load it.
 
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

Ben Pittens wrote:Lets look to the definition of top level classes:
Toplevel classes are all classes except innerclasses, right?
So all classes in the OCA exam are top level classes by design?


Yes! It's a pretty straightforward and simple rule. Any type which is not enclosed with the curly braces of another type is a top-level class. On the OCA exam you only have to deal with top-level types. I deliberately use "type" as this rule not only applies to classes but also to interfaces (and enums).

Using this code snippet you can try to spot all top-level (and nested) types

Although Scott already shared a topic about why protected and private top-level class are not allowed (and will result a compiler error), I tried to explain it as well in this post.

Hope it helps!
Kind regards,
Roel
 
Ben Pittens
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel and Scott,

Thanks for the links and explenation, it's clear to me now

Kind Regards
Ben
 
author & internet detective
Posts: 39435
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've added it to the errata. We didn't cover that because protected classes aren't on the exam (protected methods are.) But it is close enough to an exam topic that we should have mentioned it.
 
What are you doing? You are supposed to be reading this tiny ad!
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!