java - Keep out of reach of Children: Removing protected fields from inheritance -


in spirit of designed oo, class extending has marked 1 of fields protected. class has generously provided public setter, yet no getter.

i extending class base class in turn extended several children. how can restrict access protected variable children while still being able manipulate privately , set publicly?

see example below:

public abstract class thirdpartyclass {   protected map propertymap;    public void setpropertymap(map propertymap){     this.propertymap= propertymap;   }    // other methods use propertymap. }  public abstract class mybaseclass extends thirdpartyclass{ // accessor methods entries in propertymap.   public getfoo(){     propertymap.get("foo");   }    public getbar(){     propertymap.get("bar");   }   // etc... }  public class oneofmanychildren extends mybaseclass { // should access propertymap via methods in mybaseclass. } 

i have found can revoke access making field private final in mybaseclass. hinders using setter provided super class.

i able circumvent limitation "cleverness" below yet results in maintaining 2 copies of same map o(n) operation copy on every element.

public abstract class mybaseclass extends thirdpartyclass{    private final map propertymap = new hashmap(); // revokes access children.    /** sets parent & grandparent maps. */   @override   public final void setpropertymap(map propertymap){     super.setpropertymap(propertymap);     this.propertymap.clear();     this.propertymap.putall(propertymap);   } } 

are there better ways of accomplishing this?

note: 1 example of real question: how restrict access protected fields without maintaining multiple copies?

note: know if field made private in first place protected accessor, non-issue. sadly have no control on that.

note: is-a relatonship (inheritance) required.

note: apply collection, dto, or complex object.

metaphor misunderstanding question:

this akin grandparent having cookie jar leave accessible family members , else in house (protected). parent, young children, enters house and, reasons of own, wishes prevent children digging cookie jar ad nauseam. instead, child should ask parent chocolate chip cookie , see magically appear; likewise sugar cookie or oreo. need never know cookies stored in same jar or if there jar (black box). accomplished if jar belonged parent, if grandparent convinced put away cookies, or if grandparents did not need access. short of creating , maintaining 2 identical jars, how can access restricted children yet unimpeded parent & grandparent?

this might not possible you, if derive interface thirdpartyclass , make thirdpartyclass implement ?

then have mybaseclass act decorator implementing interface delegating private member thirdpartyclassimpl.

i.e.

public interface thirdparty ...   public class thirdpartyclass implements thirdparty    public class mybaseclass implements thirdparty {      private thirdparty decorated = new thirdpartyclass();     public class subclassone extends mybaseclass.... 

etc


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -