Java Reference
In-Depth Information
Chapter 9
1.
Code reuse is the practice of writing a single piece of code and using it many times
in different programs and contexts. Inheritance is useful for code reuse because it
allows you to write a class that captures common useful code and then extend that
class to add more features and behavior to it.
2.
Overloading a method involves creating two methods in the same class that have
the same name but different parameters. Overriding a method involves creating a
new version of an inherited method in a subclass that has identical parameters but
new behavior to replace the old.
3.
The following statements are legal:
(a)
Vehicle v = new Car();
(b)
Vehicle v = new SUV();
(c)
Car c = new SUV();
(d)
SUV s = new SUV();
4.
The
this
keyword refers to the current object, while the
super
keyword refers to
the current class's superclass. Use the
super
keyword when you call a method or
constructor from the superclass that you've overridden, and use the
this
keyword
when you access your object's other fields, constructors, and methods.
5.
UndergraduateStudent
can call the
setAge
method but cannot directly access
the
name
or
age
fields from
Student
.
6.
public UndergraduateStudent(String name) {
super(name, 18);
year = 0;
}
7.
public void setAge(int age) {
super.setAge(age);
year++;
}
8.
B 2
A
A 1
D 2
C
C 1
A 2
A
A 1
Search WWH ::
Custom Search