My application class uses a library that has default two variables in it.
// A class from framework:
public clas开发者_JAVA百科s SuperClass implements Serializable {
private long id;
private long version;
// getter and setter methods for these variables...
}
I should extend this class, in order to access some of the functionality of the framework.
If I extend this class to my own class like this:
public class MyClassChild extends SuperClass {
private long myprimarykey;
private String some column;
private long myversion;
// getter and setter methods for these variables...
}
As per the programming model, those two variables are also accessible.
While implementing operation that requires an Object of type SuperClass
.
Is there any idea to extend that SuperClass which is not including those two variables (id, version)
?
I do not know what to do?
Any suggestions pls?
Thanks
You cannot remove these fields, but you can override the getters and setters to do something other than the default (this depends on their access modifiers, but they will probably be protected
or greater). This way you can stop the external assigning of these variables, for example. If you choose this route, I would suggest that you familiarize yourself with the ways these fields are used in the superclass so that you can anticipate any knock-on behavior.
In general JPA does not let you override superclass mappings to remove attributes, but depending on your provider it may be possible to do the following:
public class MyClassChild extends SuperClass {
private long myprimarykey;
private String some column;
private long myversion;
// Override superclass mappings
@Transient
long getId() { return super.getId(); }
@Transient
void setId(long id) { return super.setId(long id); }
// etc...
}
EDIT
In addition, it is also possible to override the values of individual columns in a mapped superclass by using the @AttributeOverride
annotation, e.g.
@AttributeOverride(name="id", column=@Column(name="EMP_ID"))
@Entity
public class MyClassChild extends SuperClass {
private String some column;
...
}
Nope. If you're inheriting, you get the entire class to come along. If you don't want to inherit all the fields, you're not designing a subclass.
Imagine what would happen if you could omit variables - what would inherited methods do about the now-missing state? Everything would break...
All you can do about that is to make that variable private to the super class so subclasses do not 'see' it.
When you extend a class you are supposedly adding more variables and methods, you cannot subtract.
精彩评论