user2904726
user2904726

Reputation: 1

When the word "super" is used in a subclass, what does word super force the code to do?

For Java programming. I've read through my text book, but I can't find it.

Upvotes: 0

Views: 347

Answers (1)

Daniel Kaplan
Daniel Kaplan

Reputation: 67504

It depends on the context. If you do it like this:

public SubclassConstructor() {
    super("foo");
}

This is passing in these arguments to the parent's constructor. If you do it like this:

public void subclassMethod() {
    super.methodInBoth("foo");
}

@Overrides
public void methodInBoth(String var) {

}

This is calling the parent's method instead of the child's method. And, to solidify the lesson, if you did it like this:

public void subclassMethod() {
    this.methodInBoth("foo");
}

@Overrides
public void methodInBoth(String var) {

}

It would call the child's method instead of the parent's method. If you just called methodInBoth("foo");, that would also call the child's.

Upvotes: 1

Related Questions