Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ben Pittens
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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
 
Scott Selikoff
author
Saloon Keeper
Posts: 4031
18
Eclipse IDE Flex Google Web Toolkit
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic