30.3k views
4 votes
Istilah di set ini (40)

Asli
Consider the following class definitions.
public class Bird
{
private int beakStrength;
public Bird(int input)
{
beakStrength = input;
}
public void setBeakStrength(int strength)
{
beakStrength = strength;
}
}
public class Hawk extends Bird
{
private int talonStrength;
public Hawk(int talon, int beak)
{
super(beak);
talonStrength = talon;
}
}
The following statement appears in a method in another class.
Bird b = new Hawk(5, 8);
Which of the following best describes the effect of executing the statement?
A
The Bird variable b is instantiated as a Hawk. The instance variable talonStrength is initialized with the value from the parameter talon. The Hawk constructor cannot set the instance variable beakStrength because a subclass does not have access to a private variable in its superclass.
B
The Bird variable b is instantiated as a Hawk. The call super(beak) returns a value from the instance variable beakStrength in the superclass and makes it accessible in the subclass. The instance variable talonStrength is then initialized with the value from the parameter talon.
C
The Bird variable b is instantiated as a Hawk. The instance variable talonStrength is initialized with the value from the parameter talon. No other initializations are made to any instance variables.
D
The Bird variable b is instantiated as a Hawk. The call super(beak) invokes the Bird constructor and initializes the instance variable beakStrength with the value from the parameter beak. The instance variable talonStrength is then initialized with the value from the parameter talon.
E
The code segment will not execute because the Bird variable b cannot be instantiated as a Hawk.

User SharkCop
by
7.2k points

1 Answer

5 votes

D

The Bird variable b is instantiated as a Hawk. The call to super(beak) invokes the constructor of the superclass, Bird, and initializes the instance variable beakStrength with the value from the parameter beak. The instance variable talonStrength is then initialized with the value from the parameter talon. Therefore, both the talonStrength and beakStrength instance variables are initialized in the Hawk class constructor.

User Danil Onishchenko
by
8.0k points