Chapter 10
Additional Web Questions
Please answer the following true or false questions:
1. Any classes that you create must be treated specially by Java.
2. To create a subclass, use the keyword extends.
3. A class inherits from its subclass.
4. Classes inherit both fields and methods that are not private.
5. A class's constructor should call its superclass's constructor.
6. Methods and fields defined as public as accessible to the public.
7. A class's name and filename should match.
8. A variable defined as static can never be changed.
9. If the class Car is a subclass of Vehicle, then a Car can still be considered a Vehicle.
10. A subclass cannot change the behavior of any of its superclass's mehods.
Answes:
1. false.
Everything is the same in Java. The JVM cannot tell the difference between a class you wrote, and one that came with the Java distribution.
2. true.
The syntax for creating a new class, MyClass, from an existing class, OldClass, would be as follows:
public class MyClass extends OldClass
3.false.
It’s the opposite. A subclass inherits from its superclass.
4. true.
Fields and methods defined as public or protected are inherited.
5. true.
While not required, it's probably a good idea to, unless you have a good reason for not calling the superconstructor. Also, the super() constructor must be called first in the class's constructor.
6. true.
This is not a trick question. Some times the answer is as easy as it seems.
7. true.
A class called MyClass should have a source code file names MyClass.java.
8. false.
A variable defined as static can change. However, its location is static, so the value is shared by all instances of that class.
9. true.
Any class is also considered to be any and all of its parent classes. Logically, this makes sense. All cars car vehicles, but not all vehicles are cars. Some are boats, trains, bikes, etc.
10. false.
Subclasses can, and often, do override methods from their parent classes. This allows the subclass to behave in a unique fashion with respect to its parent class.