Re: access levels for private field which is inherited



Lothar Kimmeringer wrote:
thufir wrote:

Would someone provide an example where Foo extends Bar and there are private fields in Bar which Foo uses in its constructor(s)? Seems a conundrum...Using the following example:

Without reflection there is no way, but given your example
you just need to call setName(name) to achieve your intention.


Even with reflection, is it possible? I was just playing around with reflection and I noticed that the API only promises to return public methods and fields. Maybe there's a sneaky "getAll" that I missed...

Anyway, calling super here is probably the best choice. Note than in your example you must call super because Bar has no default constructor.

One can also provide a factory method such as newInstance below.

package attribs;

public class Foo extends Bar {
private String id;
public Foo(String name,String id){
super( name ); // New line here
//this.name = name; // Get rid of this one
this.id = id
}
static public Foo newInstance( String id ) {
Foo f = new Foo( "default", id );
f.setSomeOtherNonfinalProperty( "non" );
return f;
}

package attribs;

public class Bar {

private String name;

public setName(String name) {this.name = name;}
}

.



Relevant Pages

  • Re: static class inheritance, generalized
    ... private Bar _bar; ... private string _val; ... private Foo _container; ... horrible the more readability and clarity of the code goes out the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: static class inheritance, generalized
    ... private Bar _bar; ... private string _val; ... private Foo _container; ... internal Bar(Foo container) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: modular programming in Forth
    ... your proposal, of not only hiding the private ... VARIABLE bar2 ... I suppose, in an ideal world, LOCATE>BAR would open the module FOO ...
    (comp.lang.forth)
  • Re: Mismatched behaviour between regular compiler and refactoring compiler
    ... the equivalent to Bar is a private class. ... public class Foo ... private Foo m_foo; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: access levels for private field which is inherited
    ... private fields in Bar which Foo uses in its constructor? ... To call setNamefrom the constructor safely, ... Since Bar does not have a constructor that takes 'name', then its subclasses should not try to construct the with a parent 'name'. ... If Bar did have a 'public Bar' constructor, then Foo would invoke that: ...
    (comp.lang.java.help)