• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Sybex 816 Chapter 11 Assessment question 13 tb617624.JaSE11PrgIISG.c11.13 possible Erratum

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which of the following are true statements about a class Camel with a single instance variable List<String> species? (Choose all that apply.)



A. If Camel is well encapsulated, then it must have restricted extensibility.
B. If Camel is well encapsulated, then it must be immutable.
C. If Camel has restricted extensibility, then it must have good encapsulation.
D. If Camel has restricted extensibility, then it must be immutable.
E. If Camel is immutable, then it must have good encapsulation.
F. If Camel is immutable, then it must restrict extensibility.



Option A is incorrect because good encapsulation requires private state rather than declaring the class final. Option B is incorrect because the well-encapsulated Camel class can have a getter that exposes the species variable to be modified. Options C and D are incorrect because a class can be final while having public variables and be mutable. Option E is correct because methods that expose species could change it, which would prevent immutability. Option F is correct because you cannot enforce immutability in a subclass.



I think F is not a correct answer. It would have been correct if the word should was used instead of must



The class Person is immutable because its age can never change after instantiation. It also does not place any restriction on extensibility.

Oracle's Guideline 6-1 / MUTABLE-1: Prefer immutability for value types states,

Immutable classes should not be subclassable.
Immutable classes themselves should declare fields final

 
author & internet detective
Posts: 40487
823
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On Oracle's page about immutability, they say "Don't allow subclasses to override methods."

This is restricting extensibility. (Not necessarily by preventing subclasses, but by preventing some methods like getters)
 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think an example will better demonstrate what Oracle was trying to say. At least I didn't understand it until I came up with this example.



Class Person is technically immutable. You can't modify its state, at least without using reflection. But you can supply a mutable subclass (or override getter method), and it can break your program.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic