TEST 3 SHORT ANSWER PART 1 Flashcards
(26 cards)
A “has‐a” relationship can exist between classes. What does this mean?
One class contains or aggregates another as a field
What is an “is‐a” relationship?
One class inherits from another
A program uses two classes: Animal and Dog. Which class is the superclass and which is the subclass?
Animal is the superclass; Dog is the subclass.
In the line public class Pet extends Dog, which is the superclass, and which is the subclass?
Dog is the superclass; Pet is the subclass.
What is the difference between a protected class member and a private class member?
protected: accessible within the same package and by subclasses (even in other packages).
private: accessible only within the defining class.
Can a subclass ever directly access the private members of its superclass?
No. Private members are hidden from subclasses
Which constructor is called first— that of the subclass or the superclass?
The superclass’s constructor runs before the subclass’s.
What is the difference between overriding a superclass method and overloading a superclass method?
Overriding - same signature
Overloading - same name, different signature
Reference variables can be polymorphic. What does this mean?
it can point to or refer to objects of different types, potentially at different times during the program’s execution.
When does dynamic binding take place?
At runtime, when an overridden method is invoked on a polymorphic reference, the JVM picks the actual object’s implementation.
What is an abstract method?
A declared method without an implementation.
What is an abstract class?
A class declared abstract that cannot be instantiated; it may contain abstract methods and/or concrete methods.
What are the differences between an abstract class and an interface?
Abstract class - single inheritance
Interface - supports multiple methods
When you instantiate an anonymous inner class, the class must do one of two things. What are they?
It must either extend an existing class or implement an interface.
What is a functional interface?
An interface with exactly one abstract method (used as the target for lambda expressions).
What is a lambda expression?
a concise way to define a short, single-use function within a larger program
What is meant when it is said that an exception is thrown?
an unexpected event or error has occurred during the execution of the code
What does it mean to catch an exception?
using a try-catch block to intercept and handle an error that might occur within a code block
What happens when an exception is thrown, but the try statement does not have a catch clause that is capable of catching it?
The exception goes up the call stack until it terminates
What is the purpose of a finally clause?
To execute cleanup code regardless of whether an exception was thrown or caught
Where does execution resume after an exception has been thrown and caught?
With the first statement after the entire try‑catch‑finally block.
When multiple exceptions are caught in the same try statement and some of them are related through inheritance, does the order in which they are listed matter?
Yes—more specific exceptions must be listed before more general ones, or the compiler reports an unreachable catch.
What types of objects can be thrown?
Any instance of Throwable (typically Exception or Error) or its subclasses.
When are you required to have a throws clause in a method header?
when a method may throw a “checked” exception, but it doesn’t explicitly handle it within its code block