java - Hide a field from a superclass in a subclass -
let's have 3 classes:
class foo { protected string var; public void dosomething() { // var } } class extends foo { public void dosomething() { // uses var differently foo } } class b extends foo { protected int var; // hides foo's var field public void dosomething() { // b's var, still in similar fashion foo } } in case, know , want b's var field hide foo's , still used in similar way (printing value or performing calculation or whatever).
intentionally hiding fields can make code hard read, exception that, or still poor design?
edit:
want inherit 'var' foo i'd b inherit except var.
edit 2:
forgot add access modifiers var.
you can't "override" fields in same way methods can overridden. you'll need use method overriding implement behavior. example:
class foo { private string var; object getvar() { return var; } } class b extends foo { private int var; @override object getvar() { return var; } } edit: based on comment:
foo'svarof typelist<string>, ,b'svarof typelist<item>itemseparate class together. can tell, both use lists different template types, , both still same thing (go through list , elements)
it sounds classes should generic, example:
abstract class foo<t> { list<t> list; // common logic } class extends foo<string> { } class b extends foo<item> { } don't take code literally - it's suggest possible design.
Comments
Post a Comment