Java Reference
In-Depth Information
Complete the missing code.
class
Student
extends
Person
{
char
grade ;
public
Student(String name,
char
grade)
{
...
}
}
3. Give an example of a protected method. Explain how protected methods can be
accessed and when methods should be defined as protected.
4. Give an example of polymorphism. Explain what dynamic binding is and why poly-
morphism requires dynamic binding.
5. Write the code to print the income of all objects in the
a
array, where
a
contains objects
of type
Employee
that have the
getSalary
method and objects of type
Student
that
have the
getStudentLoan
method. Employees and students inherit from the
Person
class. Use the
instanceof
keyword to determine the type of the objects in the array.
6. Write the body of a
PrintedLiterature
class that contains the variables:
String name
String author
and a constructor that initializes them. Then write the body of a
Book
class that
inherits from the
PrintedLiterature
class. The
Book
class should have in addition
the variable:
int isbn
and a constructor that initializes all the variables.
7. Consider the following code.
Person p =
new
Person(....) ;
Object o = p;
System.out. println(o. toString()) ;
What will be printed if the
Person
class has the appropriate
toString
method? What
will be printed if the
Person
class does not have the appropriate
toString
method?
If the
Person
class has a
toString
method, then which
toString
method will be
executed, the one for the
Person
class or the one for the
Object
class? Explain why.
8. Consider a
HockeyPlayer
class that inherits from the Player
class
.Writea
getSalary
method that returns 10 plus the value of calling
getSalary
for the su-
perclass (i.e., calling
getSalary
for the corresponding
Player
object).
public class
HockeyPlayer
extends
Player
{
public double
getSalary()
{
...
}
...
}