Say we have a class like this:
class Bar {
boolean b;
}
class Foo {
String zoo;
Bar bar;
}
and then we have a class that extends Foo:
class Stew extends Foo {
public Stew(Bar b, String z){
this.bar = b;
this.zoo = z;
}
}
my question is - is there any way to prevent Stew
from having any non-method fields that aren't in Foo
? In other words, I don't want Stew
to have any fields, I just want Stew
to implement a constructor and maybe a method or two.
Perhaps there is an annotation I can use, that can do this?
Something like:
@OnlyAddsMethods
class Stew extends Foo {
public Stew(Bar b, String z){
this.bar = b;
this.zoo = z;
}
}
purpose - I am going to serialize Stew
to JSON, but I don't want Stew to have any new fields. I want to let any developer working on this file to know that any additional fields will be ignored (or won't be recognized) etc.
Stew to extend Foo
and prior to that why do you needbar
andzoo
in Foo? You can always choose to have a constructor which doesn't need to initialize the properties of parent class. Is that what you're looking for?Stew
even extendingFoo
from a design perspective?Foo
itself. If that's not possible, I would have rather tried to solve that first.