196k views
0 votes
Suppose the class Value is partially defined below public class Value { private int number; public int getValue() { return number;} } A subclass of Value, LargerValue, is defined with a getValue method that returns twice the value of the parent. Which line is the body of LargerValue's getValue method?a. return super.number * 2;

b. return super.getValue() * 2;
c. return number * 2;
d. return getValue() * 2;

User Lumaskcete
by
3.5k points

2 Answers

1 vote

Answer:

b. return super.getValue() * 2;

Step-by-step explanation:

The above statement has a keyword super. This keyword is used to invoke method getValue() of the parent class Value which means this keyword is used to refer to the Value class where this method is being used.

super invokes a method implementation of the Value class from the LargerValue which is a child class of parent class Value. It allows to override the implementation of a getValue() of the Value class, but still execute the implementation of the Value class as well.

So this statement return super.getValue() * 2; calls the parent class Value method getValue() using super keyword.

This keyword is used when both the parent and child classes have same method names.

For example if LargerValue has a method named getValue() then if we call this method from LargerValue class it will call the getValue() method of LargerValue class but if we want to call the method of its parent class Value, then we use super keyword while calling this method from LargerValue.

User SamJackSon
by
3.8k points
1 vote

Answer:

b. return super.getValue() * 2;

Step-by-step explanation:

In order to use/call a variable, method, or constructor from a parent class, the super keyword can be used in subclass.

In this question, we need to override the getValue() method in Value class. That means, the body of the getValue() method in LargerValue should be return super.getValue()*2;

User Chris Kimpton
by
3.7k points