d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Code, Explain Plz
Add Reply New Topic New Poll
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Dec 17 2018 08:01pm
I thought super was used to access parent class methods or constructors??


but i was told with inheritance even without super you can access parent class variables and methods?





Code
public void changeVelocity(int speed, int direction) {
move(speed, direction);
System.out.println("Car.changeVelocity() : Velocity " + speed + " direction " + direction);
}



public void changeVelocity(int speed, int direction) {
super.move(speed, direction);
System.out.println("Car.changeVelocity() : Velocity " + speed + " direction " + direction);
}





Basically wondering what's the difference between these 2.... (move) is a method in superclass...... changevelocity is subclass method





/edit I guess super is used for being explicit. so you don't always need to use it.... but it's also used for method overriding i was told.
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...

This post was edited by ferf on Dec 17 2018 08:29pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 17 2018 10:45pm
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
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Dec 18 2018 02:16pm
Quote (carteblanche @ Dec 18 2018 12:45am)
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);
}
}


Gotchya thanks :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll