Quote (ferf @ Dec 17 2018 09:01pm)
Which confuses me, if it's for method overriding, i thought the point of method overriding was to change the behavior of a super class method, to make it fit better for subclass overrided method.
If that's the case, why are you calling super class method with super......... so then it's doing both original method + modified behavior of super class method to fit subclass....
if that makes sense...
sometimes you want to replace it entirely, in which case you wouldn't call the parent method.
other times you want to do the parent method, but you also wanna add more stuff.
2 examples.
suppose you have an Entity class with delete() defined. suppose this deletes your record completely. (delete row from database, delete file, whatever).
well, customer complains that they don't know who deleted their stuff. so you decide to add a new feature to audit certain actions. now if your object is deleted, you wanna know who did it:
Code
class AuditableEntity extends Entity {
public void delete() {
createAuditTrail("deleted by ferf");
super.delete();
}
}
now you get support phone calls. some dude keeps accidentally deleting stuff, so it's completely gone. they ask if you can recover it. so you have an idea. instead of deleting it, you can just mark it inactive. then all your queries will skip inactive stuff and only show active. this way if the customer needs it recovered, you simply make it active.
Code
class ActiveEntity extends Entity {
public void delete() {
setActiveStatus(false);
}
}
This post was edited by carteblanche on Dec 17 2018 10:46pm